/*********************************************************************
* Author : Samson
* Date : 01/22/2014
* Test platform:
* 3.6.10-4.fc18.i686.PAE
* GNU bash, version 4.2.39
* *******************************************************************/
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <resolv.h>
#include <stdlib.h>
#include <unistd.h>
#include <openssl/ssl.h>
#include <openssl/err.h>
#include <openssl/evp.h>
#include <openssl/x509.h>
#define EVP_MAX_MD_SIZE 64
#define MD5_RESULT_LEN 16
#define DATA_LEN 8
char msg[]="\x61\x62\x63\x64\x65\x66\x67\x68";
int main()
{
unsigned char md_value[EVP_MAX_MD_SIZE];
unsigned int md_len,i;
EVP_MD_CTX mdctx;
OpenSSL_add_all_algorithms();
EVP_MD_CTX_init(&mdctx);
EVP_DigestInit_ex(&mdctx, EVP_md5(), NULL);
EVP_DigestUpdate(&mdctx, msg, DATA_LEN);
EVP_DigestFinal_ex(&mdctx, md_value, &md_len);
EVP_MD_CTX_cleanup(&mdctx);
printf("MD5:");
for(i = 0; i < md_len; i++)
{
printf("%02x ", md_value[i]);
}
printf("\r\n");
return 0;
}
编译:
[ufo@localhost fuck]$ gcc testhash.c -Wall -g -lssl -lcrypto -o hash
运行结果:
[ufo@localhost fuck]$ ./hash
MD5:e8 dc 40 81 b1 34 34 b4 51 89 a7 20 b7 7b 68 18