MD5 报文摘要算法源码 联系客服

发布时间 : 星期二 文章MD5 报文摘要算法源码更新完毕开始阅读1a4d446ca300a6c30c229f90

unsigned int len; {

unsigned int i;

for (i = 0; i < len; i++)

((char *)output)[i] = (char)value; }

A.4 mddriver.c

/* MDDRIVER.C - MD2, MD4 and MD5测试程序 */

/* RSA数据安全公司(RSA Data Security, Inc.)从来没有出于任何特定目的陈述过关于此软件的可买性和实用性,它提供了“as is”,没有表达或暗示过任何理由。此声明必须在任何此文件和软件的任何拷贝中保留。*/ /* 如果没有定义C编译标志的值,则MD5缺省状态下为MD5 */ #ifndef MD

#define MD MD5 #endif

#include #include #include #include \#if MD == 2

#include \#endif

#if MD == 4

#include \#endif

#if MD == 5

#include \#endif

/* 测试分组长度和数量 */ #define TEST_BLOCK_LEN 1000 #define TEST_BLOCK_COUNT 1000

static void MDString PROTO_LIST ((char *)); static void MDTimeTrial PROTO_LIST ((void)); static void MDTestSuite PROTO_LIST ((void)); static void MDFile PROTO_LIST ((char *)); static void MDFilter PROTO_LIST ((void));

static void MDPrint PROTO_LIST ((unsigned char [16]));

#if MD == 2

#define MD_CTX MD2_CTX #define MDInit MD2Init #define MDUpdate MD2Update #define MDFinal MD2Final #endif

#if MD == 4

#define MD_CTX MD4_CTX #define MDInit MD4Init #define MDUpdate MD4Update #define MDFinal MD4Final #endif

#if MD == 5

#define MD_CTX MD5_CTX #define MDInit MD5Init #define MDUpdate MD5Update #define MDFinal MD5Final #endif

/* 主程序. 变量:

-sstring – 摘要字符串 -t - 运行时间测试 -x - 运行测试脚本 filename – 摘要文件 (none) - 摘要标准输入 */

int main (argc, argv) int argc;

char *argv[]; {

int i;

if (argc > 1)

for (i = 1; i < argc; i++)

if (argv[i][0] == '-' && argv[i][1] == 's') MDString (argv[i] + 2);

else if (strcmp (argv[i], \ MDTimeTrial ();

else if (strcmp (argv[i], \ MDTestSuite (); else

MDFile (argv[i]);

else

MDFilter ();

return (0); }

/* 计算字符串的摘要并打印其值 */ static void MDString (string) char *string; {

MD_CTX context;

unsigned char digest[16];

unsigned int len = strlen (string);

MDInit (&context);

MDUpdate (&context, string, len); MDFinal (digest, &context);

printf (\ MDPrint (digest); printf (\}

/* 测试计算 TEST_BLOCK_COUNT TEST_BLOCK_LEN-byte 分组摘要的时间 */

static void MDTimeTrial () {

MD_CTX context;

time_t endTime, startTime;

unsigned char block[TEST_BLOCK_LEN], digest[16]; unsigned int i;

printf

(\ TEST_BLOCK_LEN, TEST_BLOCK_COUNT);

/* 初始化分组*/

for (i = 0; i < TEST_BLOCK_LEN; i++) block[i] = (unsigned char)(i & 0xff);

/* 开始时钟 */ time (&startTime);

/* 摘要分组 */

MDInit (&context);

for (i = 0; i < TEST_BLOCK_COUNT; i++) MDUpdate (&context, block, TEST_BLOCK_LEN); MDFinal (digest, &context);

/* 停止时钟 */ time (&endTime);

printf (\ printf (\ MDPrint (digest);

printf (\ printf

(\

(long)TEST_BLOCK_LEN * (long)TEST_BLOCK_COUNT/(endTime-startTime)); }

/* 计算一个参考组件串的摘要并打印结果*/ static void MDTestSuite () {

printf (\

MDString (\ MDString (\ MDString (\

MDString (\

MDString (\ MDString

(\ MDString

(\1234567890123456789012345678901234567890\}

/*计算一个文件的摘要并打印结果 */ static void MDFile (filename) char *filename; {

FILE *file; MD_CTX context; int len;

unsigned char buffer[1024], digest[16];

if ((file = fopen (filename, \