参考openssl中的md5test.c
#include <stdio.h> #include <string.h> #include <stdlib.h> #include <openssl/evp.h> #include <openssl/md5.h> #include <openssl/err.h> static char *test[] = { "", "a", "abc", "message digest", "abcdefghijklmnopqrstuvwxyz", "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789", "12345678901234567890123456789012345678901234567890123456789012345678901234567890", NULL, }; static char *ret[] = { "d41d8cd98f00b204e9800998ecf8427e", "0cc175b9c0f1b6a831c399e269772661", "900150983cd24fb0d6963f7d28e17f72", "f96b697d7cb7938d525a2f31aaf161d0", "c3fcd3d76192e4007dfb496cca67e13b", "d174ab98d277d9f5a5611c2c9f419d9f", "57edf4a22be3c955ac49da2e2107b67a", }; static char *pt(unsigned char *md) { int i; static char buf[80]; for (i = 0; i < MD5_DIGEST_LENGTH; i++) sprintf(&(buf[i * 2]), "%02x", md[i]); return (buf); } int main(void) { int i; char **P, **R; char *p; unsigned char md[MD5_DIGEST_LENGTH]; /* 遍历预设值校验 */ P = test; R = ret; while (*P != NULL) { printf("calculating MD5 on '%s'\n", *P); EVP_Digest(&(P[0][0]), strlen((char *)*P), md, NULL, EVP_md5(), NULL); p = pt(md); printf("MD5: %s\n", p); if (strcmp(p, (char *)*R) != 0) { printf("got %s instead of %s\n", p, *R); } else printf("test ok\n"); printf("\n"); R++; P++; } /* 命令行使用md5sum、openssl md5命令的时候会多计算一个'\n'换行符 */ printf("calculating MD5 on '%s'\n", "abc\\n"); printf("strlen %d\n", strlen("abc\n")); EVP_Digest("abc\n", strlen("abc\n"), md, NULL, EVP_md5(), NULL); p = pt(md); printf("%s\n", p); printf("\n"); printf("echo abc | md5sum\n"); system("echo abc | md5sum"); printf("\n"); /* 循环分段计算 */ P = test + 5; printf("calculating MD5 on '%s'\n", *P); EVP_MD_CTX ctx; EVP_MD_CTX_init(&ctx); EVP_DigestInit_ex(&ctx, EVP_md5(), NULL); for (i = 0; i < strlen((char *)*P); i++) { EVP_DigestUpdate(&ctx, *P + i, 1); } EVP_DigestFinal_ex(&ctx, md, NULL); EVP_MD_CTX_cleanup(&ctx); p = pt(md); printf("%s\n", p); EVP_MD_CTX_init(&ctx); EVP_DigestInit_ex(&ctx, EVP_md5(), NULL); EVP_DigestUpdate(&ctx, "ABCDEFGHIJKLMNOPQRSTUVWXYZ", strlen("ABCDEFGHIJKLMNOPQRSTUVWXYZ")); EVP_DigestUpdate(&ctx, "abcdefghijklmnopqrstuvwxyz", strlen("abcdefghijklmnopqrstuvwxyz")); EVP_DigestUpdate(&ctx, "0123456789", strlen("0123456789")); EVP_DigestFinal_ex(&ctx, md, NULL); EVP_MD_CTX_cleanup(&ctx); p = pt(md); printf("%s\n", p); ERR_load_crypto_strings(); ERR_print_errors_fp(stderr); return 0; }
运行结果
$ gcc -Wall -lcrypto md5.c $ ./a.out calculating MD5 on '' MD5: d41d8cd98f00b204e9800998ecf8427e test ok calculating MD5 on 'a' MD5: 0cc175b9c0f1b6a831c399e269772661 test ok calculating MD5 on 'abc' MD5: 900150983cd24fb0d6963f7d28e17f72 test ok calculating MD5 on 'message digest' MD5: f96b697d7cb7938d525a2f31aaf161d0 test ok calculating MD5 on 'abcdefghijklmnopqrstuvwxyz' MD5: c3fcd3d76192e4007dfb496cca67e13b test ok calculating MD5 on 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789' MD5: d174ab98d277d9f5a5611c2c9f419d9f test ok calculating MD5 on '12345678901234567890123456789012345678901234567890123456789012345678901234567890' MD5: 57edf4a22be3c955ac49da2e2107b67a test ok calculating MD5 on 'abc\n' strlen 4 0bee89b07a248e27c83fc3d5951213c1 echo abc | md5sum 0bee89b07a248e27c83fc3d5951213c1 - calculating MD5 on 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789' d174ab98d277d9f5a5611c2c9f419d9f d174ab98d277d9f5a5611c2c9f419d9f