一.安裝OpenSSL庫
把下面的東西一股腦兒的裝上,以免編譯時間出現連結錯誤
apt-get install openssl libssl-dev libssl-doc libcurl4-openssl-dev
二.實現MD5加密函數,接受一個字串,返回它的常規32位小寫MD5值
/* md5_encrypt.h*/#ifndef _ENCRYPT_H_#define _ENCRYPT_H_char *md5_encrypt(const unsigned char*);#endif
/* md5_encrypt.c*/#include <stdio.h>#include <stdlib.h>#include <openssl/md5.h>#include <string.h>#include <strings.h>#include "md5_encrypt.h"char *md5_encrypt(const unsigned char *str){unsigned char md[16];char tmp[3] = {'\0'};char *buf = (char*)malloc(33);bzero(buf,33);MD5(str,strlen(str),md);int i = 0;for(i=0;i<16;i++){sprintf(tmp,"%2.2x",md[i]);strcat(buf,tmp);}return buf;}
編譯此函數: gcc md5_encrypt.c -c -lcrypto
上面實現的MD5加密函數char *md5_encrypt(const unsigned char *str)接受一個const unsigned char字串,返回它的32位常規小寫字元md5值。
注意返回的md5值儲存在堆區,佔33個位元組,使用後記得用free釋放。
還有一種用庫中MD5_Init等函數實現的方法。以後附上。順便研究一下OpenSSL庫,沒準以後用的著。
OpenSSL是跨平台的,而且提供了命令列工具。