• C语言实现MD5校验


    根据网上资料,整理验证C程序代码。

    接口函数:

    1 /******************************************************
    2 *函数名称:Compute_data_md5
    3 *输   入:data 校验数据首地址
    4         len 校验数据长度
    5         md5_str 字符串形式的MD5值
    6 *输   出:无
    7 *功   能:计算数据MD5值,并以字符串形式返回
    8 *******************************************************/
    9 int Compute_data_md5(unsigned char *data, unsigned int len,unsigned char *md5_str)
    View Code

    MD5.h 的内容:

     1 #ifndef _MD5_H_
     2 #define _MD5_H_
     3 
     4 #define MD5_SIZE        16
     5 #define MD5_STR_LEN        (MD5_SIZE * 2)
     6 
     7 typedef struct
     8 {
     9   unsigned int count[2];
    10   unsigned int state[4];
    11   unsigned char buffer[64];   
    12 } MD5_CTX;
    13 
    14 #define F(x,y,z) ((x & y) | (~x & z))
    15 #define G(x,y,z) ((x & z) | (y & ~z))
    16 #define H(x,y,z) (x^y^z)
    17 #define I(x,y,z) (y ^ (x | ~z))
    18 #define ROTATE_LEFT(x,n) ((x << n) | (x >> (32-n)))
    19 
    20 #define FF(a,b,c,d,x,s,ac) 
    21 { 
    22   a += F(b,c,d) + x + ac; 
    23   a = ROTATE_LEFT(a,s); 
    24   a += b; 
    25 }
    26 #define GG(a,b,c,d,x,s,ac) 
    27 { 
    28   a += G(b,c,d) + x + ac; 
    29   a = ROTATE_LEFT(a,s); 
    30   a += b; 
    31 }
    32 #define HH(a,b,c,d,x,s,ac) 
    33 { 
    34   a += H(b,c,d) + x + ac; 
    35   a = ROTATE_LEFT(a,s); 
    36   a += b; 
    37 }
    38 #define II(a,b,c,d,x,s,ac) 
    39 { 
    40   a += I(b,c,d) + x + ac; 
    41   a = ROTATE_LEFT(a,s); 
    42   a += b; 
    43 }                                            
    44 void MD5Init(MD5_CTX *context);
    45 void MD5Update(MD5_CTX *context, unsigned char *input, unsigned int inputlen);
    46 void MD5Final(MD5_CTX *context, unsigned char digest[16]);
    47 void MD5Transform(unsigned int state[4], unsigned char block[64]);
    48 void MD5Encode(unsigned char *output, unsigned int *input, unsigned int len);
    49 void MD5Decode(unsigned int *output, unsigned char *input, unsigned int len);
    50 
    51 int Compute_data_md5(unsigned char *data, unsigned int len,unsigned char *md5_str);
    52 
    53 #endif
    View Code

    MD5.c 的内容:

      1 #include "md5.h"
      2 #include <memory.h>
      3 #include <stdio.h>
      4 
      5 unsigned char PADDING[] =
      6 {
      7   0x80,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
      8   0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
      9   0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
     10   0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
     11 };
     12 
     13 void MD5Init(MD5_CTX *context)
     14 {
     15   context->count[0] = 0;
     16   context->count[1] = 0;
     17   context->state[0] = 0x67452301;
     18   context->state[1] = 0xEFCDAB89;
     19   context->state[2] = 0x98BADCFE;
     20   context->state[3] = 0x10325476;
     21 }
     22 
     23 void MD5Update(MD5_CTX *context, unsigned char *input, unsigned int inputlen)
     24 {
     25   unsigned int i = 0;
     26   unsigned int index = 0;
     27   unsigned int partlen = 0;
     28 
     29   index = (context->count[0] >> 3) & 0x3F;
     30   partlen = 64 - index;
     31   context->count[0] += inputlen << 3;
     32 
     33   if(context->count[0] < (inputlen << 3))
     34     context->count[1]++;
     35   context->count[1] += inputlen >> 29;
     36 
     37   if(inputlen >= partlen)
     38   {
     39     memcpy(&context->buffer[index], input,partlen);
     40     MD5Transform(context->state, context->buffer);
     41 
     42     for(i = partlen; i+64 <= inputlen; i+=64)
     43       MD5Transform(context->state, &input[i]);
     44 
     45     index = 0;        
     46   }  
     47   else
     48   {
     49     i = 0;
     50   }
     51   memcpy(&context->buffer[index], &input[i], inputlen-i);
     52 }
     53 
     54 void MD5Final(MD5_CTX *context, unsigned char digest[16])
     55 {
     56   unsigned int index = 0,padlen = 0;
     57   unsigned char bits[8];
     58 
     59   index = (context->count[0] >> 3) & 0x3F;
     60   padlen = (index < 56)?(56-index):(120-index);
     61   MD5Encode(bits, context->count, 8);
     62   MD5Update(context, PADDING, padlen);
     63   MD5Update(context, bits, 8);
     64   MD5Encode(digest, context->state, 16);
     65 }
     66 
     67 void MD5Encode(unsigned char *output,unsigned int *input,unsigned int len)
     68 {
     69   unsigned int i = 0;
     70   unsigned int j = 0;
     71 
     72   while(j < len)
     73   {
     74     output[j] = input[i] & 0xFF;  
     75     output[j+1] = (input[i] >> 8) & 0xFF;
     76     output[j+2] = (input[i] >> 16) & 0xFF;
     77     output[j+3] = (input[i] >> 24) & 0xFF;
     78     i++;
     79     j += 4;
     80   }
     81 }
     82 
     83 void MD5Decode(unsigned int *output, unsigned char *input, unsigned int len)
     84 {
     85   unsigned int i = 0;
     86   unsigned int j = 0;
     87 
     88   while(j < len)
     89   {
     90     output[i] = (input[j]) |
     91       (input[j+1] << 8) |
     92       (input[j+2] << 16) |
     93       (input[j+3] << 24);
     94     i++;
     95     j += 4; 
     96   }
     97 }
     98 
     99 void MD5Transform(unsigned int state[4], unsigned char block[64])
    100 {
    101   unsigned int a = state[0];
    102   unsigned int b = state[1];
    103   unsigned int c = state[2];
    104   unsigned int d = state[3];
    105   unsigned int x[64];
    106 
    107   MD5Decode(x,block,64);
    108 
    109   FF(a, b, c, d, x[ 0], 7, 0xd76aa478); /* 1 */
    110   FF(d, a, b, c, x[ 1], 12, 0xe8c7b756); /* 2 */
    111   FF(c, d, a, b, x[ 2], 17, 0x242070db); /* 3 */
    112   FF(b, c, d, a, x[ 3], 22, 0xc1bdceee); /* 4 */
    113   FF(a, b, c, d, x[ 4], 7, 0xf57c0faf); /* 5 */
    114   FF(d, a, b, c, x[ 5], 12, 0x4787c62a); /* 6 */
    115   FF(c, d, a, b, x[ 6], 17, 0xa8304613); /* 7 */
    116   FF(b, c, d, a, x[ 7], 22, 0xfd469501); /* 8 */
    117   FF(a, b, c, d, x[ 8], 7, 0x698098d8); /* 9 */
    118   FF(d, a, b, c, x[ 9], 12, 0x8b44f7af); /* 10 */
    119   FF(c, d, a, b, x[10], 17, 0xffff5bb1); /* 11 */
    120   FF(b, c, d, a, x[11], 22, 0x895cd7be); /* 12 */
    121   FF(a, b, c, d, x[12], 7, 0x6b901122); /* 13 */
    122   FF(d, a, b, c, x[13], 12, 0xfd987193); /* 14 */
    123   FF(c, d, a, b, x[14], 17, 0xa679438e); /* 15 */
    124   FF(b, c, d, a, x[15], 22, 0x49b40821); /* 16 */
    125 
    126   /* Round 2 */
    127   GG(a, b, c, d, x[ 1], 5, 0xf61e2562); /* 17 */
    128   GG(d, a, b, c, x[ 6], 9, 0xc040b340); /* 18 */
    129   GG(c, d, a, b, x[11], 14, 0x265e5a51); /* 19 */
    130   GG(b, c, d, a, x[ 0], 20, 0xe9b6c7aa); /* 20 */
    131   GG(a, b, c, d, x[ 5], 5, 0xd62f105d); /* 21 */
    132   GG(d, a, b, c, x[10], 9,  0x2441453); /* 22 */
    133   GG(c, d, a, b, x[15], 14, 0xd8a1e681); /* 23 */
    134   GG(b, c, d, a, x[ 4], 20, 0xe7d3fbc8); /* 24 */
    135   GG(a, b, c, d, x[ 9], 5, 0x21e1cde6); /* 25 */
    136   GG(d, a, b, c, x[14], 9, 0xc33707d6); /* 26 */
    137   GG(c, d, a, b, x[ 3], 14, 0xf4d50d87); /* 27 */
    138   GG(b, c, d, a, x[ 8], 20, 0x455a14ed); /* 28 */
    139   GG(a, b, c, d, x[13], 5, 0xa9e3e905); /* 29 */
    140   GG(d, a, b, c, x[ 2], 9, 0xfcefa3f8); /* 30 */
    141   GG(c, d, a, b, x[ 7], 14, 0x676f02d9); /* 31 */
    142   GG(b, c, d, a, x[12], 20, 0x8d2a4c8a); /* 32 */
    143 
    144   /* Round 3 */
    145   HH(a, b, c, d, x[ 5], 4, 0xfffa3942); /* 33 */
    146   HH(d, a, b, c, x[ 8], 11, 0x8771f681); /* 34 */
    147   HH(c, d, a, b, x[11], 16, 0x6d9d6122); /* 35 */
    148   HH(b, c, d, a, x[14], 23, 0xfde5380c); /* 36 */
    149   HH(a, b, c, d, x[ 1], 4, 0xa4beea44); /* 37 */
    150   HH(d, a, b, c, x[ 4], 11, 0x4bdecfa9); /* 38 */
    151   HH(c, d, a, b, x[ 7], 16, 0xf6bb4b60); /* 39 */
    152   HH(b, c, d, a, x[10], 23, 0xbebfbc70); /* 40 */
    153   HH(a, b, c, d, x[13], 4, 0x289b7ec6); /* 41 */
    154   HH(d, a, b, c, x[ 0], 11, 0xeaa127fa); /* 42 */
    155   HH(c, d, a, b, x[ 3], 16, 0xd4ef3085); /* 43 */
    156   HH(b, c, d, a, x[ 6], 23,  0x4881d05); /* 44 */
    157   HH(a, b, c, d, x[ 9], 4, 0xd9d4d039); /* 45 */
    158   HH(d, a, b, c, x[12], 11, 0xe6db99e5); /* 46 */
    159   HH(c, d, a, b, x[15], 16, 0x1fa27cf8); /* 47 */
    160   HH(b, c, d, a, x[ 2], 23, 0xc4ac5665); /* 48 */
    161 
    162   /* Round 4 */
    163   II(a, b, c, d, x[ 0], 6, 0xf4292244); /* 49 */
    164   II(d, a, b, c, x[ 7], 10, 0x432aff97); /* 50 */
    165   II(c, d, a, b, x[14], 15, 0xab9423a7); /* 51 */
    166   II(b, c, d, a, x[ 5], 21, 0xfc93a039); /* 52 */
    167   II(a, b, c, d, x[12], 6, 0x655b59c3); /* 53 */
    168   II(d, a, b, c, x[ 3], 10, 0x8f0ccc92); /* 54 */
    169   II(c, d, a, b, x[10], 15, 0xffeff47d); /* 55 */
    170   II(b, c, d, a, x[ 1], 21, 0x85845dd1); /* 56 */
    171   II(a, b, c, d, x[ 8], 6, 0x6fa87e4f); /* 57 */
    172   II(d, a, b, c, x[15], 10, 0xfe2ce6e0); /* 58 */
    173   II(c, d, a, b, x[ 6], 15, 0xa3014314); /* 59 */
    174   II(b, c, d, a, x[13], 21, 0x4e0811a1); /* 60 */
    175   II(a, b, c, d, x[ 4], 6, 0xf7537e82); /* 61 */
    176   II(d, a, b, c, x[11], 10, 0xbd3af235); /* 62 */
    177   II(c, d, a, b, x[ 2], 15, 0x2ad7d2bb); /* 63 */
    178   II(b, c, d, a, x[ 9], 21, 0xeb86d391); /* 64 */
    179   state[0] += a;
    180   state[1] += b;
    181   state[2] += c;
    182   state[3] += d;
    183 }
    184 
    185 
    186 /******************************************************
    187 *函数名称:Compute_data_md5
    188 *输   入:data 校验数据首地址
    189         len 校验数据长度
    190         md5_str 字符串形式的MD5值
    191 *输   出:无
    192 *功   能:计算数据MD5值,并以字符串形式返回
    193 *******************************************************/
    194 int Compute_data_md5(unsigned char *data, unsigned int len,unsigned char *md5_str)
    195 {
    196     unsigned char md5_value[MD5_SIZE];
    197     unsigned int i = 0;
    198     
    199     MD5_CTX md5;
    200     
    201     MD5Init(&md5);
    202     MD5Update(&md5, data, len);
    203     MD5Final(&md5, md5_value);
    204     for(i = 0; i < MD5_SIZE; i++)
    205     {
    206         snprintf(md5_str + i*2, 2+1, "%02x", md5_value[i]);
    207     }
    208     md5_str[MD5_STR_LEN] = ''; // add end
    209 }
    210 
    211 #if 0
    212 int Compute_file_md5(const char *file_path, char *md5_str)
    213 {
    214   int i;
    215   int fd;
    216   int ret;
    217   unsigned char data[READ_DATA_SIZE];
    218   unsigned char md5_value[MD5_SIZE];
    219   MD5_CTX md5;
    220 
    221   fd = open(file_path, O_RDONLY);
    222   if (-1 == fd)
    223   {
    224     perror("open");
    225     return -1;
    226   }
    227 
    228   // init md5
    229   MD5Init(&md5);
    230 
    231   while (1)
    232   {
    233     ret = read(fd, data, READ_DATA_SIZE);
    234     if (-1 == ret)
    235     {
    236       perror("read");
    237       return -1;
    238     }
    239 
    240     MD5Update(&md5, data, ret);
    241 
    242     if (0 == ret || ret < READ_DATA_SIZE)
    243     {
    244       break;
    245     }
    246   }
    247 
    248   close(fd);
    249 
    250   MD5Final(&md5, md5_value);
    251 
    252   for(i = 0; i < MD5_SIZE; i++)
    253   {
    254     snprintf(md5_str + i*2, 2+1, "%02x", md5_value[i]);
    255   }
    256   md5_str[MD5_STR_LEN] = ''; // add end
    257 
    258   return 0;
    259 }
    260 #endif
    View Code
  • 相关阅读:
    input搜索框实时检索功能实现(超简单,核心原理请看思路即可)
    django blank 和autonow
    dwebsocket的坑
    vue 动态添加active+父子传值
    NO 2,人生苦短,我学python之python+selenium元素定位
    NO 1,人生苦短,我学python之python+selenium自动化环境搭建
    SPU与SKU概念
    数据库,缓存数据一致性常用解决方案总结
    利用注解 + 反射消除重复代码
    Nacos学习与实战
  • 原文地址:https://www.cnblogs.com/emlslxl/p/5643416.html
Copyright © 2020-2023  润新知