免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 5826 | 回复: 2

怎样取一个文件的md5值? [复制链接]

论坛徽章:
0
发表于 2004-09-20 12:26 |显示全部楼层
bsd下怎样取一个文件的md5值?
man中大概提到:
The MD5Init(), MD5Update(), and MD5Final() functions are the core functions.
有没有例子?
多谢了

论坛徽章:
0
发表于 2004-09-20 15:08 |显示全部楼层

怎样取一个文件的md5值?

不知道下面的程序是否是你要的,最近我也在看这个东东,晕啊!

  1. /* MDDRIVER.C - test driver for MD2, MD4 and MD5
  2. */

  3. /* Copyright (C) 1990-2, RSA Data Security, Inc. Created 1990. All
  4. rights reserved.

  5. RSA Data Security, Inc. makes no representations concerning either
  6. the merchantability of this software or the suitability of this
  7. software for any particular purpose. It is provided "as is"
  8. without express or implied warranty of any kind.

  9. These notices must be retained in any copies of any part of this
  10. documentation and/or software.
  11. */

  12. /* The following makes MD default to MD5 if it has not already been
  13.   defined with C compiler flags.
  14. */
  15. #ifndef MD
  16. #define MD 5
  17. #endif

  18. #include <stdio.h>;
  19. #include <time.h>;
  20. #include <string.h>;
  21. #include "global.h"
  22. #if MD == 2
  23. #include "md2.h"
  24. #endif
  25. #if MD == 4
  26. #include "md4.h"
  27. #endif
  28. #if MD == 5
  29. #include "md5.h"
  30. #endif

  31. /* Length of test block, number of test blocks.
  32. */
  33. #define TEST_BLOCK_LEN 1000
  34. #define TEST_BLOCK_COUNT 1000

  35. static void MDString PROTO_LIST ((char *));
  36. static void MDTimeTrial PROTO_LIST ((void));
  37. static void MDTestSuite PROTO_LIST ((void));
  38. static void MDFile PROTO_LIST ((char *));
  39. static void MDFilter PROTO_LIST ((void));
  40. static void MDPrint PROTO_LIST ((unsigned char [16]));

  41. #if MD == 2
  42. #define MD5_CTX MD2_CTX
  43. #define MDInit MD2Init
  44. #define MDUpdate MD2Update
  45. #define MDFinal MD2Final
  46. #endif
  47. #if MD == 4
  48. #define MD5_CTX MD4_CTX
  49. #define MDInit MD4Init
  50. #define MDUpdate MD4Update
  51. #define MDFinal MD4Final
  52. #endif
  53. #if MD == 5
  54. #define MD5_CTX MD5_CTX
  55. #define MDInit MD5Init
  56. #define MDUpdate MD5Update
  57. #define MDFinal MD5Final
  58. #endif

  59. /* Main driver.

  60. Arguments (may be any combination):
  61.   -sstring - digests string
  62.   -t       - runs time trial
  63.   -x       - runs test script
  64.   filename - digests file
  65.   (none)   - digests standard input
  66. */
  67. int main (int argc, char* argv[])
  68. {
  69.   int i;

  70.   if (argc >; 1)
  71. for (i = 1; i < argc; i++)
  72.    if (argv[i][0] == '-' && argv[i][1] == 's')
  73.      MDString ((argv[i]+2));
  74.    else if (strcmp (argv[i], "-t") == 0)
  75.      MDTimeTrial ();
  76.    else if (strcmp (argv[i], "-x") == 0)
  77.      MDTestSuite ();
  78.    else
  79.      MDFile (argv[i]);
  80.   else
  81. MDFilter ();

  82.   return (0);
  83. }

  84. /* Digests a string and prints the result.
  85. */
  86. static void MDString (char *string)
  87. {
  88.   MD5_CTX context;
  89.   unsigned char digest[16];
  90.   unsigned int len = strlen (string);

  91.   MDInit (&context);
  92.   MDUpdate (&context, (unsigned char*)string, len);
  93.   MDFinal (digest, &context);

  94.   printf ("MD%d (\"%s\") = ", MD, string);
  95.   MDPrint (digest);
  96.   printf ("\n");
  97. }

  98. /* Measures the time to digest TEST_BLOCK_COUNT TEST_BLOCK_LEN-byte
  99.   blocks.
  100. */
  101. static void MDTimeTrial ()
  102. {
  103.   MD5_CTX context;
  104.   time_t endTime, startTime;
  105.   unsigned char block[TEST_BLOCK_LEN], digest[16];
  106.   unsigned int i;


  107.   printf
  108. ("MD%d time trial. Digesting %d %d-byte blocks ...", MD,
  109.   TEST_BLOCK_LEN, TEST_BLOCK_COUNT);

  110.   /* Initialize block */
  111.   for (i = 0; i < TEST_BLOCK_LEN; i++)
  112. block[i] = (unsigned char)(i & 0xff);

  113.   /* Start timer */
  114.   time (&startTime);

  115.   /* Digest blocks */
  116.   MDInit (&context);
  117.   for (i = 0; i < TEST_BLOCK_COUNT; i++)
  118. MDUpdate (&context, block, TEST_BLOCK_LEN);
  119.   MDFinal (digest, &context);

  120.   /* Stop timer */
  121.   time (&endTime);

  122.   printf (" done\n");
  123.   printf ("Digest = ");
  124.   MDPrint (digest);
  125.   printf ("\nTime = %ld seconds\n", (long)(endTime-startTime));
  126.   printf
  127. ("Speed = %ld bytes/second\n",
  128.   (long)TEST_BLOCK_LEN * (long)TEST_BLOCK_COUNT/(endTime-startTime));
  129. }

  130. /* Digests a reference suite of strings and prints the results.
  131. */
  132. static void MDTestSuite ()
  133. {
  134.   printf ("MD%d test suite:\n", MD);

  135.   MDString ("");
  136.   MDString ("a");
  137.   MDString ("abc");
  138.   MDString ("message digest");
  139.   MDString ("abcdefghijklmnopqrstuvwxyz");
  140.   MDString
  141. ("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789");
  142.   MDString
  143. ("1234567890123456789012345678901234567890\
  144. 1234567890123456789012345678901234567890");
  145. }

  146. /* Digests a file and prints the result.
  147. */
  148. static void MDFile (char *filename)
  149. {
  150.   FILE *file;
  151.   MD5_CTX context;
  152.   int len;
  153.   unsigned char buffer[1024], digest[16];

  154.   if ((file = fopen (filename, "rb")) == NULL)
  155. printf ("%s can't be opened\n", filename);

  156.   else {
  157. MDInit (&context);
  158. while (len = fread (buffer, 1, 1024, file))
  159.    MDUpdate (&context, buffer, len);
  160. MDFinal (digest, &context);

  161. fclose (file);

  162. printf ("MD%d (%s) = ", MD, filename);
  163. MDPrint (digest);
  164. printf ("\n");
  165.   }
  166. }

  167. /* Digests the standard input and prints the result.
  168. */
  169. static void MDFilter ()
  170. {
  171.   MD5_CTX context;
  172.   int len;
  173.   unsigned char buffer[16], digest[16];

  174.   MDInit (&context);
  175.   while (len = fread (buffer, 1, 16, stdin))
  176. MDUpdate (&context, buffer, len);
  177.   MDFinal (digest, &context);

  178.   MDPrint (digest);
  179.   printf ("\n");
  180. }

  181. /* Prints a message digest in hexadecimal.
  182. */
  183. static void MDPrint (unsigned char digest[16])
  184. {

  185.   unsigned int i;

  186.   for (i = 0; i < 16; i++)
  187. printf ("%02x", digest[i]);
  188. }

复制代码

PS:不是我不想注明出处,而是太久了,不记得是哪里搞的了,sorry。

论坛徽章:
0
发表于 2004-09-21 15:09 |显示全部楼层

怎样取一个文件的md5值?

RFC文档中有MD5的实现代码。
MD5是RFC文档规范之一。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP