I have a program which takes a user input, sends that user input as an argument to a function which makes a calculation, then returns the char array to the main() function to be output there.
The return (char *)&buf;
works fine when a printf()
statement is run.
However, when there is no printf()
, the return does not seem to work, as the main()
function cannot output the returned value.
Here's the code:
#include <stdio.h>
#include <string.h>
#include <openssl/sha.h>
using namespace std;
char* hash_function(char* input_string)
{
int i = 0;
unsigned char temp[SHA_DIGEST_LENGTH]; //where we will store the SHA digest. length = 20
char buf[SHA_DIGEST_LENGTH*2];
memset(temp, 0x0, SHA_DIGEST_LENGTH); //array of size 20 to store SHA1 digest
memset(buf, 0x0, SHA_DIGEST_LENGTH*2); //array of size 2*20 to store hex result?
SHA1((unsigned char *)input_string, strlen(input_string), temp);
for(i=0; i<SHA_DIGEST_LENGTH; i++){
sprintf((char*)&(buf[i*2]), "%02x", temp[i]);
//element in temp[i] formatted with %02x and stored in buf[i*2]
}
//printf("In FUNCTION: %s\n", buf); //*************************************
return (char *)&buf;
}
int main(int argc, char * argv[])
{
if(argc != 2)
{
printf("Usage: %s <string>\n", argv[0]);
return -1;
}
char *hash = hash_function(argv[1]);
printf("Plaintext:\t%s\nSHA-1:\t\t%s\n\n", argv[1], hash);
//FILE *file = fopen("temp_file.txt", "a+"); //open file to write to
//fprintf(file, "Plaintext: %s\nSHA-1: %s\n\n", argv[1], buf);
return 0;
}
The line which I've marked with asterisks is the print()
line i'm referring to.
In order to compile, use g++ [file_name] -lcrypto -o [output]
You may have to download the openssl/sha.h package.