So I have been playing around with SHA hashing, but I have run up against a bit of a wall. For some reason, shasum from the command line and SHA1() from openssl in my program produce two difference results.
Code:
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <sys/wait.h>
#include <openssl/sha.h>
int main(int argc, char* argv[]){
int i;
char data[] = "README";
size_t length = sizeof(data);
unsigned char hash[50];
SHA1(data, length, hash);
printf("shasum of \"README\" is \n");
for(i = 0; i<SHA_DIGEST_LENGTH; i++){
printf("%x", hash[i]);
}
printf("\n");
/*Using shasum utility*/
int rc = fork();
if(rc < 0){
fprintf(stderr, "Fork failed. \n");
exit(1);
}
else if(rc == 0){
char* myargs[3];
myargs[0] = strdup("shasum");
myargs[1] = strdup("README");
myargs[2] = NULL;
execvp(myargs[0], myargs);
}
wait(NULL);
return 0;
}
Output:
shasum of "README" is
aabc5996b4a0256158cbaa6eb4e01b3aa992d942
827cc372e1a3c99b66f6ad820b223939c68bd389 README
The first hash is from SHA1 and the second hash is from shasum (fork/exec from within program). What is causing this?