linux MD5使用
# define MD5_LONG unsigned int
# define MD5_CBLOCK 64
# define MD5_LBLOCK (MD5_CBLOCK/4)
# define MD5_DIGEST_LENGTH 16
typedef struct MD5state_st {
MD5_LONG A, B, C, D;
MD5_LONG Nl, Nh;
MD5_LONG data[MD5_LBLOCK];
unsigned int num;
} MD5_CTX;
int MD5_Init(MD5_CTX *c);
int MD5_Update(MD5_CTX *c, const void *data, size_t len);
int MD5_Final(unsigned char *md, MD5_CTX *c);
unsigned char *MD5(const unsigned char *d, size_t n, unsigned char *md);
举例
#include "openssl/md5.h"
int i = 0;
char *data = "hello";
unsigned char md[MD5_DIGEST_LENGTH] = {0};
MD5_CTX c;
MD5_Init(&c);
MD5_Update(&c, data, strlen(data));
MD5_Final(md, &c);
//MD5(data, strlen(data), md); //二选一
for(i = 0; i < MD5_DIGEST_LENGTH; i++)
{
printf("0x%02x ", md[i]);
}
printf("\n");
# gcc md5.c -lcrypto
# ./a.out
0x5d 0x41 0x40 0x2a 0xbc 0x4b 0x2a 0x76 0xb9 0x71 0x9d 0x91 0x10 0x17 0xc5 0x92
md5sum
用于生成和校验文件的md5值
-b:以二进制模式读入文件内容
-t:以文本模式读入文件内容
-c:根据已生成的md5值,对现存文件进行校验