I am trying to implement a straightforward code which return the respective MD5 for a given password:
#include <stdio.h>
//Returns the MD5 hash for a given password
char hash_password(char password){
FILE *fp;
char command [1024];
sprintf(command, "md5 <<< '%c'", password);
fp = popen(command, "r");
if (fp == NULL) {
printf("Failed to run command\n" );
}
char hashed[1024];
fgets(hashed , 1024 , fp);
pclose(fp);
return hashed;
}
int main(int argc, const char * argv[]) {
char hashed = hash_password("password");
printf("%s\n", hashed);
return 0;
}
My issues are the following:
- I get a warning at
return hashed;
saying: "Incompatible pointer to integer conversion returning char[1024] from a function with result type char" - I get a warning at
char hashed = hash_password("password");
saying: "Incompatible pointer to integer passing char[9] to a parameter of type char" - The program returns
\320
which is not the correct hash result.
My two days of experience with C says the function will never return what I need since hashed
will die with the end of the function, right? How can I get it fixed?