分享

c 获取文件 MD5 值

 leon0821 2013-12-06

网上有 md5.c , md5.h, 但是里面只有 MD5Init(), MD5Update(), MD5Final() 三个函数, 

只可以直接对字符进行操作, 而没有直接求文件md5的接口. 以下是我的实现, 可计算32位和16位的md5值.

  1. /***************************************************************************** 
  2.  *  Copyright          :  All Rights Reserved. 
  3.  * 
  4.  *  Date               :  2012-09-15 22:37:49 
  5.  *  Author/Corporation :  Dengzhaoqun 
  6.  *  Email              :  dengzhaoqun@163.com 
  7.  *****************************************************************************/  
  8. #include <stdio.h>  
  9. #include <stdlib.h>  
  10. #include <string.h>  
  11.   
  12. #include "md5.h"  
  13.   
  14. char *MD5_file (char *path, int md5_len)  
  15. {  
  16.     FILE *fp = fopen (path, "rb");  
  17.     MD5_CTX mdContext;  
  18.     int bytes;  
  19.     unsigned char data[1024];  
  20.     char *file_md5;  
  21.     int i;  
  22.   
  23.     if (fp == NULL) {  
  24.         fprintf (stderr, "fopen %s failed\n", path);  
  25.         return NULL;  
  26.     }  
  27.   
  28.     MD5Init (&mdContext);  
  29.     while ((bytes = fread (data, 1, 1024, fp)) != 0)  
  30.     {  
  31.         MD5Update (&mdContext, data, bytes);  
  32.     }  
  33.     MD5Final (&mdContext);  
  34.       
  35.     file_md5 = (char *)malloc((md5_len + 1) * sizeof(char));  
  36.     if(file_md5 == NULL)  
  37.     {  
  38.         fprintf(stderr, "malloc failed.\n");  
  39.         return NULL;  
  40.     }  
  41.     memset(file_md5, 0, (md5_len + 1));  
  42.       
  43.     if(md5_len == 16)  
  44.     {  
  45.         for(i=4; i<12; i++)  
  46.         {  
  47.             sprintf(&file_md5[(i-4)*2], "%02x", mdContext.digest[i]);  
  48.         }  
  49.     }  
  50.     else if(md5_len == 32)  
  51.     {  
  52.         for(i=0; i<16; i++)  
  53.         {  
  54.             sprintf(&file_md5[i*2], "%02x", mdContext.digest[i]);  
  55.         }  
  56.     }  
  57.     else  
  58.     {  
  59.         fclose(fp);  
  60.         free(file_md5);  
  61.         return NULL;  
  62.     }  
  63.       
  64.     fclose (fp);  
  65.     return file_md5;  
  66. }  
  67.   
  68. int main(int argc, char *argv[])  
  69. {  
  70.     char *md5;  
  71.       
  72.     md5 = MD5_file("temp", 16);  
  73.     printf("16: %s\n", md5);  
  74.     free(md5);  
  75.       
  76.     md5 = MD5_file("temp", 32);  
  77.     printf("32: %s\n", md5);  
  78.     free(md5);  
  79.     return 0;  
  80. }  


以下是 md5.c的源码

  1. #include "md5.h"  
  2.   
  3. /* 
  4.  ********************************************************************** 
  5.  ** md5.c                                                            ** 
  6.  ** RSA Data Security, Inc. MD5 Message Digest Algorithm             ** 
  7.  ** Created: 2/17/90 RLR                                             ** 
  8.  ** Revised: 1/91 SRD,AJ,BSK,JT Reference C Version                  ** 
  9.  ********************************************************************** 
  10.  */  
  11.   
  12. /* 
  13.  ********************************************************************** 
  14.  ** Copyright (C) 1990, RSA Data Security, Inc. All rights reserved. ** 
  15.  **                                                                  ** 
  16.  ** License to copy and use this software is granted provided that   ** 
  17.  ** it is identified as the "RSA Data Security, Inc. MD5 Message     ** 
  18.  ** Digest Algorithm" in all material mentioning or referencing this ** 
  19.  ** software or this function.                                       ** 
  20.  **                                                                  ** 
  21.  ** License is also granted to make and use derivative works         ** 
  22.  ** provided that such works are identified as "derived from the RSA ** 
  23.  ** Data Security, Inc. MD5 Message Digest Algorithm" in all         ** 
  24.  ** material mentioning or referencing the derived work.             ** 
  25.  **                                                                  ** 
  26.  ** RSA Data Security, Inc. makes no representations concerning      ** 
  27.  ** either the merchantability of this software or the suitability   ** 
  28.  ** of this software for any particular purpose.  It is provided "as ** 
  29.  ** is" without express or implied warranty of any kind.             ** 
  30.  **                                                                  ** 
  31.  ** These notices must be retained in any copies of any part of this ** 
  32.  ** documentation and/or software.                                   ** 
  33.  ********************************************************************** 
  34.  */  
  35.   
  36. /* -- include the following line if the md5.h header file is separate -- */  
  37. /* #include "md5.h" */  
  38.   
  39. /* forward declaration */  
  40. static void Transform ();  
  41.   
  42. static unsigned char PADDING[64] = {  
  43.   0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,  
  44.   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,  
  45.   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,  
  46.   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,  
  47.   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,  
  48.   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,  
  49.   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,  
  50.   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00  
  51. };  
  52.   
  53. /* F, G and H are basic MD5 functions: selection, majority, parity */  
  54. #define F(x, y, z) (((x) & (y)) | ((~x) & (z)))  
  55. #define G(x, y, z) (((x) & (z)) | ((y) & (~z)))  
  56. #define H(x, y, z) ((x) ^ (y) ^ (z))  
  57. #define I(x, y, z) ((y) ^ ((x) | (~z)))   
  58.   
  59. /* ROTATE_LEFT rotates x left n bits */  
  60. #define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))  
  61.   
  62. /* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4 */  
  63. /* Rotation is separate from addition to prevent recomputation */  
  64. #define FF(a, b, c, d, x, s, ac) \  
  65.   {(a) += F ((b), (c), (d)) + (x) + (UINT4)(ac); \  
  66.    (a) = ROTATE_LEFT ((a), (s)); \  
  67.    (a) += (b); \  
  68.   }  
  69. #define GG(a, b, c, d, x, s, ac) \  
  70.   {(a) += G ((b), (c), (d)) + (x) + (UINT4)(ac); \  
  71.    (a) = ROTATE_LEFT ((a), (s)); \  
  72.    (a) += (b); \  
  73.   }  
  74. #define HH(a, b, c, d, x, s, ac) \  
  75.   {(a) += H ((b), (c), (d)) + (x) + (UINT4)(ac); \  
  76.    (a) = ROTATE_LEFT ((a), (s)); \  
  77.    (a) += (b); \  
  78.   }  
  79. #define II(a, b, c, d, x, s, ac) \  
  80.   {(a) += I ((b), (c), (d)) + (x) + (UINT4)(ac); \  
  81.    (a) = ROTATE_LEFT ((a), (s)); \  
  82.    (a) += (b); \  
  83.   }  
  84.   
  85. void MD5Init (MD5_CTX *mdContext)  
  86. {  
  87.   mdContext->i[0] = mdContext->i[1] = (UINT4)0;  
  88.   
  89.   /* Load magic initialization constants. 
  90.    */  
  91.   mdContext->buf[0] = (UINT4)0x67452301;  
  92.   mdContext->buf[1] = (UINT4)0xefcdab89;  
  93.   mdContext->buf[2] = (UINT4)0x98badcfe;  
  94.   mdContext->buf[3] = (UINT4)0x10325476;  
  95. }  
  96.   
  97. void MD5Update (MD5_CTX *mdContext, unsigned char *inBuf, unsigned int inLen)  
  98. {  
  99.   UINT4 in[16];  
  100.   int mdi;  
  101.   unsigned int i, ii;  
  102.   
  103.   /* compute number of bytes mod 64 */  
  104.   mdi = (int)((mdContext->i[0] >> 3) & 0x3F);  
  105.   
  106.   /* update number of bits */  
  107.   if ((mdContext->i[0] + ((UINT4)inLen << 3)) < mdContext->i[0])  
  108.     mdContext->i[1]++;  
  109.   mdContext->i[0] += ((UINT4)inLen << 3);  
  110.   mdContext->i[1] += ((UINT4)inLen >> 29);  
  111.   
  112.   while (inLen--) {  
  113.     /* add new character to buffer, increment mdi */  
  114.     mdContext->in[mdi++] = *inBuf++;  
  115.   
  116.     /* transform if necessary */  
  117.     if (mdi == 0x40) {  
  118.       for (i = 0, ii = 0; i < 16; i++, ii += 4)  
  119.         in[i] = (((UINT4)mdContext->in[ii+3]) << 24) |  
  120.                 (((UINT4)mdContext->in[ii+2]) << 16) |  
  121.                 (((UINT4)mdContext->in[ii+1]) << 8) |  
  122.                 ((UINT4)mdContext->in[ii]);  
  123.       Transform (mdContext->buf, in);  
  124.       mdi = 0;  
  125.     }  
  126.   }  
  127. }  
  128.   
  129. void MD5Final (MD5_CTX *mdContext)  
  130.   
  131. {  
  132.   UINT4 in[16];  
  133.   int mdi;  
  134.   unsigned int i, ii;  
  135.   unsigned int padLen;  
  136.   
  137.   /* save number of bits */  
  138.   in[14] = mdContext->i[0];  
  139.   in[15] = mdContext->i[1];  
  140.   
  141.   /* compute number of bytes mod 64 */  
  142.   mdi = (int)((mdContext->i[0] >> 3) & 0x3F);  
  143.   
  144.   /* pad out to 56 mod 64 */  
  145.   padLen = (mdi < 56) ? (56 - mdi) : (120 - mdi);  
  146.   MD5Update (mdContext, PADDING, padLen);  
  147.   
  148.   /* append length in bits and transform */  
  149.   for (i = 0, ii = 0; i < 14; i++, ii += 4)  
  150.     in[i] = (((UINT4)mdContext->in[ii+3]) << 24) |  
  151.             (((UINT4)mdContext->in[ii+2]) << 16) |  
  152.             (((UINT4)mdContext->in[ii+1]) << 8) |  
  153.             ((UINT4)mdContext->in[ii]);  
  154.   Transform (mdContext->buf, in);  
  155.   
  156.   /* store buffer in digest */  
  157.   for (i = 0, ii = 0; i < 4; i++, ii += 4) {  
  158.     mdContext->digest[ii] = (unsigned char)(mdContext->buf[i] & 0xFF);  
  159.     mdContext->digest[ii+1] =  
  160.       (unsigned char)((mdContext->buf[i] >> 8) & 0xFF);  
  161.     mdContext->digest[ii+2] =  
  162.       (unsigned char)((mdContext->buf[i] >> 16) & 0xFF);  
  163.     mdContext->digest[ii+3] =  
  164.       (unsigned char)((mdContext->buf[i] >> 24) & 0xFF);  
  165.   }  
  166. }  
  167.   
  168. /* Basic MD5 step. Transform buf based on in. 
  169.  */  
  170. static void Transform (UINT4 *buf, UINT4 *in)  
  171. {  
  172.   UINT4 a = buf[0], b = buf[1], c = buf[2], d = buf[3];  
  173.   
  174.   /* Round 1 */  
  175. #define S11 7  
  176. #define S12 12  
  177. #define S13 17  
  178. #define S14 22  
  179.   FF ( a, b, c, d, in[ 0], S11, 3614090360); /* 1 */  
  180.   FF ( d, a, b, c, in[ 1], S12, 3905402710); /* 2 */  
  181.   FF ( c, d, a, b, in[ 2], S13,  606105819); /* 3 */  
  182.   FF ( b, c, d, a, in[ 3], S14, 3250441966); /* 4 */  
  183.   FF ( a, b, c, d, in[ 4], S11, 4118548399); /* 5 */  
  184.   FF ( d, a, b, c, in[ 5], S12, 1200080426); /* 6 */  
  185.   FF ( c, d, a, b, in[ 6], S13, 2821735955); /* 7 */  
  186.   FF ( b, c, d, a, in[ 7], S14, 4249261313); /* 8 */  
  187.   FF ( a, b, c, d, in[ 8], S11, 1770035416); /* 9 */  
  188.   FF ( d, a, b, c, in[ 9], S12, 2336552879); /* 10 */  
  189.   FF ( c, d, a, b, in[10], S13, 4294925233); /* 11 */  
  190.   FF ( b, c, d, a, in[11], S14, 2304563134); /* 12 */  
  191.   FF ( a, b, c, d, in[12], S11, 1804603682); /* 13 */  
  192.   FF ( d, a, b, c, in[13], S12, 4254626195); /* 14 */  
  193.   FF ( c, d, a, b, in[14], S13, 2792965006); /* 15 */  
  194.   FF ( b, c, d, a, in[15], S14, 1236535329); /* 16 */  
  195.   
  196.   /* Round 2 */  
  197. #define S21 5  
  198. #define S22 9  
  199. #define S23 14  
  200. #define S24 20  
  201.   GG ( a, b, c, d, in[ 1], S21, 4129170786); /* 17 */  
  202.   GG ( d, a, b, c, in[ 6], S22, 3225465664); /* 18 */  
  203.   GG ( c, d, a, b, in[11], S23,  643717713); /* 19 */  
  204.   GG ( b, c, d, a, in[ 0], S24, 3921069994); /* 20 */  
  205.   GG ( a, b, c, d, in[ 5], S21, 3593408605); /* 21 */  
  206.   GG ( d, a, b, c, in[10], S22,   38016083); /* 22 */  
  207.   GG ( c, d, a, b, in[15], S23, 3634488961); /* 23 */  
  208.   GG ( b, c, d, a, in[ 4], S24, 3889429448); /* 24 */  
  209.   GG ( a, b, c, d, in[ 9], S21,  568446438); /* 25 */  
  210.   GG ( d, a, b, c, in[14], S22, 3275163606); /* 26 */  
  211.   GG ( c, d, a, b, in[ 3], S23, 4107603335); /* 27 */  
  212.   GG ( b, c, d, a, in[ 8], S24, 1163531501); /* 28 */  
  213.   GG ( a, b, c, d, in[13], S21, 2850285829); /* 29 */  
  214.   GG ( d, a, b, c, in[ 2], S22, 4243563512); /* 30 */  
  215.   GG ( c, d, a, b, in[ 7], S23, 1735328473); /* 31 */  
  216.   GG ( b, c, d, a, in[12], S24, 2368359562); /* 32 */  
  217.   
  218.   /* Round 3 */  
  219. #define S31 4  
  220. #define S32 11  
  221. #define S33 16  
  222. #define S34 23  
  223.   HH ( a, b, c, d, in[ 5], S31, 4294588738); /* 33 */  
  224.   HH ( d, a, b, c, in[ 8], S32, 2272392833); /* 34 */  
  225.   HH ( c, d, a, b, in[11], S33, 1839030562); /* 35 */  
  226.   HH ( b, c, d, a, in[14], S34, 4259657740); /* 36 */  
  227.   HH ( a, b, c, d, in[ 1], S31, 2763975236); /* 37 */  
  228.   HH ( d, a, b, c, in[ 4], S32, 1272893353); /* 38 */  
  229.   HH ( c, d, a, b, in[ 7], S33, 4139469664); /* 39 */  
  230.   HH ( b, c, d, a, in[10], S34, 3200236656); /* 40 */  
  231.   HH ( a, b, c, d, in[13], S31,  681279174); /* 41 */  
  232.   HH ( d, a, b, c, in[ 0], S32, 3936430074); /* 42 */  
  233.   HH ( c, d, a, b, in[ 3], S33, 3572445317); /* 43 */  
  234.   HH ( b, c, d, a, in[ 6], S34,   76029189); /* 44 */  
  235.   HH ( a, b, c, d, in[ 9], S31, 3654602809); /* 45 */  
  236.   HH ( d, a, b, c, in[12], S32, 3873151461); /* 46 */  
  237.   HH ( c, d, a, b, in[15], S33,  530742520); /* 47 */  
  238.   HH ( b, c, d, a, in[ 2], S34, 3299628645); /* 48 */  
  239.   
  240.   /* Round 4 */  
  241. #define S41 6  
  242. #define S42 10  
  243. #define S43 15  
  244. #define S44 21  
  245.   II ( a, b, c, d, in[ 0], S41, 4096336452); /* 49 */  
  246.   II ( d, a, b, c, in[ 7], S42, 1126891415); /* 50 */  
  247.   II ( c, d, a, b, in[14], S43, 2878612391); /* 51 */  
  248.   II ( b, c, d, a, in[ 5], S44, 4237533241); /* 52 */  
  249.   II ( a, b, c, d, in[12], S41, 1700485571); /* 53 */  
  250.   II ( d, a, b, c, in[ 3], S42, 2399980690); /* 54 */  
  251.   II ( c, d, a, b, in[10], S43, 4293915773); /* 55 */  
  252.   II ( b, c, d, a, in[ 1], S44, 2240044497); /* 56 */  
  253.   II ( a, b, c, d, in[ 8], S41, 1873313359); /* 57 */  
  254.   II ( d, a, b, c, in[15], S42, 4264355552); /* 58 */  
  255.   II ( c, d, a, b, in[ 6], S43, 2734768916); /* 59 */  
  256.   II ( b, c, d, a, in[13], S44, 1309151649); /* 60 */  
  257.   II ( a, b, c, d, in[ 4], S41, 4149444226); /* 61 */  
  258.   II ( d, a, b, c, in[11], S42, 3174756917); /* 62 */  
  259.   II ( c, d, a, b, in[ 2], S43,  718787259); /* 63 */  
  260.   II ( b, c, d, a, in[ 9], S44, 3951481745); /* 64 */  
  261.   
  262.   buf[0] += a;  
  263.   buf[1] += b;  
  264.   buf[2] += c;  
  265.   buf[3] += d;  
  266. }  
  267.   
  268. /* 
  269.  ********************************************************************** 
  270.  ** End of md5.c                                                     ** 
  271.  ******************************* (cut) ******************************** 
  272.  */  

以下是 md5.h 的源码

  1. /* 
  2.  ********************************************************************** 
  3.  ** md5.h -- Header file for implementation of MD5                   ** 
  4.  ** RSA Data Security, Inc. MD5 Message Digest Algorithm             ** 
  5.  ** Created: 2/17/90 RLR                                             ** 
  6.  ** Revised: 12/27/90 SRD,AJ,BSK,JT Reference C version              ** 
  7.  ** Revised (for MD5): RLR 4/27/91                                   ** 
  8.  **   -- G modified to have y&~z instead of y&z                      ** 
  9.  **   -- FF, GG, HH modified to add in last register done            ** 
  10.  **   -- Access pattern: round 2 works mod 5, round 3 works mod 3    ** 
  11.  **   -- distinct additive constant for each step                    ** 
  12.  **   -- round 4 added, working mod 7                                ** 
  13.  ********************************************************************** 
  14.  */  
  15.   
  16. /* 
  17.  ********************************************************************** 
  18.  ** Copyright (C) 1990, RSA Data Security, Inc. All rights reserved. ** 
  19.  **                                                                  ** 
  20.  ** License to copy and use this software is granted provided that   ** 
  21.  ** it is identified as the "RSA Data Security, Inc. MD5 Message     ** 
  22.  ** Digest Algorithm" in all material mentioning or referencing this ** 
  23.  ** software or this function.                                       ** 
  24.  **                                                                  ** 
  25.  ** License is also granted to make and use derivative works         ** 
  26.  ** provided that such works are identified as "derived from the RSA ** 
  27.  ** Data Security, Inc. MD5 Message Digest Algorithm" in all         ** 
  28.  ** material mentioning or referencing the derived work.             ** 
  29.  **                                                                  ** 
  30.  ** RSA Data Security, Inc. makes no representations concerning      ** 
  31.  ** either the merchantability of this software or the suitability   ** 
  32.  ** of this software for any particular purpose.  It is provided "as ** 
  33.  ** is" without express or implied warranty of any kind.             ** 
  34.  **                                                                  ** 
  35.  ** These notices must be retained in any copies of any part of this ** 
  36.  ** documentation and/or software.                                   ** 
  37.  ********************************************************************** 
  38.  */  
  39.   
  40. /* typedef a 32 bit type */  
  41. typedef unsigned long int UINT4;  
  42.   
  43. /* Data structure for MD5 (Message Digest) computation */  
  44. typedef struct {  
  45.   UINT4 i[2];                   /* number of _bits_ handled mod 2^64 */  
  46.   UINT4 buf[4];                                    /* scratch buffer */  
  47.   unsigned char in[64];                              /* input buffer */  
  48.   unsigned char digest[16];     /* actual digest after MD5Final call */  
  49. } MD5_CTX;  
  50.   
  51. void MD5Init ();  
  52. void MD5Update ();  
  53. void MD5Final ();  
  54.   
  55. /* 
  56.  ********************************************************************** 
  57.  ** End of md5.h                                                     ** 
  58.  ******************************* (cut) ******************************** 
  59.  */  





    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多