How to use EVP_DigestInit() in Linux

Sep 9, 2008 at 10:33pm
Hello,
I have this project where I have to give the user the option to apply MD4, MD5,or SHA1 in C++ that will be ran on a Linux machine. Once the user selects one of the three specified hash functions, the program will apply the selected hash function to an arbitrary length local file named "input.txt". At his point, I am aware that the EVP_DigestInit (openss1/evp.h) will allow one to do this but I am not sure how to use such functions. I found an example program(listed below) online(http://www.openssl.org/docs/crypto/EVP_DigestInit.html) but I couldn't get it to complie:

This example digests the data ``Test Message\n'' and ``Hello World\n'', using the digest name passed on the command line.

#include <stdio.h>
#include <openssl/evp.h>

main(int argc, char *argv[])
{
EVP_MD_CTX mdctx;
const EVP_MD *md;
char mess1[] = "Test Message\n";
char mess2[] = "Hello World\n";
unsigned char md_value[EVP_MAX_MD_SIZE];
int md_len, i;

OpenSSL_add_all_digests();

if(!argv[1]) {
printf("Usage: mdtest digestname\n");
exit(1);
}

md = EVP_get_digestbyname(argv[1]);

if(!md) {
printf("Unknown message digest %s\n", argv[1]);
exit(1);
}

EVP_MD_CTX_init(&mdctx);
EVP_DigestInit_ex(&mdctx, md, NULL);
EVP_DigestUpdate(&mdctx, mess1, strlen(mess1));
EVP_DigestUpdate(&mdctx, mess2, strlen(mess2));
EVP_DigestFinal_ex(&mdctx, md_value, &md_len);
EVP_MD_CTX_cleanup(&mdctx);

printf("Digest is: ");
for(i = 0; i < md_len; i++) printf("%02x", md_value[i]);
printf("\n");
}


With this in mind, can anyone provide me with an example on how to apply a hash to a file or to a phrase. I just want to have some general idea of how to do this so I can complete the project!! Any advice will be greatly appreciated!!

Thanks In Advance
Last edited on Sep 9, 2008 at 11:08pm
Topic archived. No new replies allowed.