码迷,mamicode.com
首页 > 编程语言 > 详细

md5信息摘要算法 基于16位机器运行

时间:2020-07-02 13:39:17      阅读:47      评论:0      收藏:0      [点我收藏+]

标签:output   摘要   click   散列   padding   md5算法   else   art   signed   

MD5信息摘要算法(Message Digest Algorithm)

  1. md5是一种密码散列函数,可以生成128位(16字节)的散列值,用于确保信息传输完整一致。
  2. 这里举例如何在16机器上实现md5算法:
  3. md5.h 
    技术图片
     1 #ifndef MD5_H
     2 #define MD5_H
     3  
     4 typedef struct
     5 {
     6     unsigned long count[2];
     7     unsigned long state[4];
     8     unsigned char buffer[64];   
     9 }MD5_CTX;
    10  
    11                          
    12 #define F(x,y,z) ((x & y) | (~x & z))
    13 #define G(x,y,z) ((x & z) | (y & ~z))
    14 #define H(x,y,z) (x^y^z)
    15 #define I(x,y,z) (y ^ (x | ~z))
    16 #define ROTATE_LEFT(x,n) ((x << n) | (x >> (32-n)))
    17 #define FF(a,b,c,d,x,s,ac) 18           { 19           a += F(b,c,d) + x + ac; 20           a = ROTATE_LEFT(a,s); 21           a += b; 22           }
    23 #define GG(a,b,c,d,x,s,ac) 24           { 25           a += G(b,c,d) + x + ac; 26           a = ROTATE_LEFT(a,s); 27           a += b; 28           }
    29 #define HH(a,b,c,d,x,s,ac) 30           { 31           a += H(b,c,d) + x + ac; 32           a = ROTATE_LEFT(a,s); 33           a += b; 34           }
    35 #define II(a,b,c,d,x,s,ac) 36           { 37           a += I(b,c,d) + x + ac; 38           a = ROTATE_LEFT(a,s); 39           a += b; 40           }                                            
    41 void MD5Init(MD5_CTX *context);
    42 void MD5Update(MD5_CTX *context,unsigned char *input,unsigned long inputlen);
    43 void MD5Final(MD5_CTX *context,unsigned char digest[16]);
    44 void MD5Transform(unsigned long state[4],unsigned char block[64]);
    45 void MD5Encode(unsigned char *output,unsigned long *input,unsigned long len);
    46 void MD5Decode(unsigned long *output,unsigned char *input,unsigned long len);
    47 void md5_test(void);
    48 
    49 #endif
    50 
    51 //================ end of file =====================
    md5.h

     

  4. 技术图片
      1 #include <string.h>
      2 #include <stdio.h>
      3 #include <math.h>
      4 #include <stdlib.h>
      5 #include <float.h>
      6 #include <limits.h>
      7 #include <ctype.h>
      8 #include "md5.h"
      9  
     10 unsigned char PADDING[]={0x80,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
     11                          0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
     12                          0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
     13                          0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
     14                          
     15 void MD5Init(MD5_CTX *context)
     16 {
     17      context->count[0] = 0;
     18      context->count[1] = 0;
     19      context->state[0] = 0x67452301;
     20      context->state[1] = 0xEFCDAB89;
     21      context->state[2] = 0x98BADCFE;
     22      context->state[3] = 0x10325476;
     23 }
     24 void MD5Update(MD5_CTX *context,unsigned char *input,unsigned long inputlen)
     25 {
     26     unsigned long i = 0,index = 0,partlen = 0;
     27     index = (context->count[0] >> 3) & 0x3F;
     28     partlen = 64 - index;
     29     context->count[0] += inputlen << 3;
     30     if(context->count[0] < (inputlen << 3))
     31        context->count[1]++;
     32     context->count[1] += inputlen >> 29;
     33     
     34     if(inputlen >= partlen)
     35     {
     36        memcpy(&context->buffer[index],input,partlen);
     37        MD5Transform(context->state,context->buffer);
     38        for(i = partlen;i+64 <= inputlen;i+=64)
     39            MD5Transform(context->state,&input[i]);
     40        index = 0;        
     41     }  
     42     else
     43     {
     44         i = 0;
     45     }
     46     memcpy(&context->buffer[index],&input[i],inputlen-i);
     47 }
     48 void MD5Final(MD5_CTX *context,unsigned char digest[16])
     49 {
     50     unsigned long index = 0,padlen = 0;
     51     unsigned char bits[8];
     52     index = (context->count[0] >> 3) & 0x3F;
     53     padlen = (index < 56)?(56-index):(120-index);
     54     MD5Encode(bits,context->count,8);
     55     MD5Update(context,PADDING,padlen);
     56     MD5Update(context,bits,8);
     57     MD5Encode(digest,context->state,16);
     58 }
     59 void MD5Encode(unsigned char *output,unsigned long *input,unsigned long len)
     60 {
     61     unsigned long i = 0,j = 0;
     62     while(j < len)
     63     {
     64          output[j] = input[i] & 0xFF;  
     65          output[j+1] = (input[i] >> 8) & 0xFF;
     66          output[j+2] = (input[i] >> 16) & 0xFF;
     67          output[j+3] = (input[i] >> 24) & 0xFF;
     68          i++;
     69          j+=4;
     70     }
     71 }
     72 void MD5Decode(unsigned long *output,unsigned char *input,unsigned long len)
     73 {
     74      unsigned long i = 0,j = 0;
     75      while(j < len)
     76      {
     77            output[i] = (input[j]) |
     78                        ((unsigned long)input[j+1] << 8) |
     79                        ((unsigned long)input[j+2] << 16) |
     80                        ((unsigned long)input[j+3] << 24);
     81            i++;
     82            j+=4; 
     83      }
     84 }
     85 void MD5Transform(unsigned long state[4],unsigned char block[64])
     86 {
     87   unsigned long a = state[0];
     88   unsigned long b = state[1];
     89   unsigned long c = state[2];
     90   unsigned long d = state[3];
     91   unsigned long x[64];
     92   MD5Decode(x,block,64);
     93   FF(a, b, c, d, x[ 0], 7, 0xd76aa478); /* 1 */
     94   FF(d, a, b, c, x[ 1], 12, 0xe8c7b756); /* 2 */
     95   FF(c, d, a, b, x[ 2], 17, 0x242070db); /* 3 */
     96   FF(b, c, d, a, x[ 3], 22, 0xc1bdceee); /* 4 */
     97   FF(a, b, c, d, x[ 4], 7, 0xf57c0faf); /* 5 */
     98   FF(d, a, b, c, x[ 5], 12, 0x4787c62a); /* 6 */
     99   FF(c, d, a, b, x[ 6], 17, 0xa8304613); /* 7 */
    100   FF(b, c, d, a, x[ 7], 22, 0xfd469501); /* 8 */
    101   FF(a, b, c, d, x[ 8], 7, 0x698098d8); /* 9 */
    102   FF(d, a, b, c, x[ 9], 12, 0x8b44f7af); /* 10 */
    103   FF(c, d, a, b, x[10], 17, 0xffff5bb1); /* 11 */
    104   FF(b, c, d, a, x[11], 22, 0x895cd7be); /* 12 */
    105   FF(a, b, c, d, x[12], 7, 0x6b901122); /* 13 */
    106   FF(d, a, b, c, x[13], 12, 0xfd987193); /* 14 */
    107   FF(c, d, a, b, x[14], 17, 0xa679438e); /* 15 */
    108   FF(b, c, d, a, x[15], 22, 0x49b40821); /* 16 */
    109  
    110   /* Round 2 */
    111   GG(a, b, c, d, x[ 1], 5, 0xf61e2562); /* 17 */
    112   GG(d, a, b, c, x[ 6], 9, 0xc040b340); /* 18 */
    113   GG(c, d, a, b, x[11], 14, 0x265e5a51); /* 19 */
    114   GG(b, c, d, a, x[ 0], 20, 0xe9b6c7aa); /* 20 */
    115   GG(a, b, c, d, x[ 5], 5, 0xd62f105d); /* 21 */
    116   GG(d, a, b, c, x[10], 9,  0x2441453); /* 22 */
    117   GG(c, d, a, b, x[15], 14, 0xd8a1e681); /* 23 */
    118   GG(b, c, d, a, x[ 4], 20, 0xe7d3fbc8); /* 24 */
    119   GG(a, b, c, d, x[ 9], 5, 0x21e1cde6); /* 25 */
    120   GG(d, a, b, c, x[14], 9, 0xc33707d6); /* 26 */
    121   GG(c, d, a, b, x[ 3], 14, 0xf4d50d87); /* 27 */
    122   GG(b, c, d, a, x[ 8], 20, 0x455a14ed); /* 28 */
    123   GG(a, b, c, d, x[13], 5, 0xa9e3e905); /* 29 */
    124   GG(d, a, b, c, x[ 2], 9, 0xfcefa3f8); /* 30 */
    125   GG(c, d, a, b, x[ 7], 14, 0x676f02d9); /* 31 */
    126   GG(b, c, d, a, x[12], 20, 0x8d2a4c8a); /* 32 */
    127  
    128   /* Round 3 */
    129   HH(a, b, c, d, x[ 5], 4, 0xfffa3942); /* 33 */
    130   HH(d, a, b, c, x[ 8], 11, 0x8771f681); /* 34 */
    131   HH(c, d, a, b, x[11], 16, 0x6d9d6122); /* 35 */
    132   HH(b, c, d, a, x[14], 23, 0xfde5380c); /* 36 */
    133   HH(a, b, c, d, x[ 1], 4, 0xa4beea44); /* 37 */
    134   HH(d, a, b, c, x[ 4], 11, 0x4bdecfa9); /* 38 */
    135   HH(c, d, a, b, x[ 7], 16, 0xf6bb4b60); /* 39 */
    136   HH(b, c, d, a, x[10], 23, 0xbebfbc70); /* 40 */
    137   HH(a, b, c, d, x[13], 4, 0x289b7ec6); /* 41 */
    138   HH(d, a, b, c, x[ 0], 11, 0xeaa127fa); /* 42 */
    139   HH(c, d, a, b, x[ 3], 16, 0xd4ef3085); /* 43 */
    140   HH(b, c, d, a, x[ 6], 23,  0x4881d05); /* 44 */
    141   HH(a, b, c, d, x[ 9], 4, 0xd9d4d039); /* 45 */
    142   HH(d, a, b, c, x[12], 11, 0xe6db99e5); /* 46 */
    143   HH(c, d, a, b, x[15], 16, 0x1fa27cf8); /* 47 */
    144   HH(b, c, d, a, x[ 2], 23, 0xc4ac5665); /* 48 */
    145  
    146   /* Round 4 */
    147   II(a, b, c, d, x[ 0], 6, 0xf4292244); /* 49 */
    148   II(d, a, b, c, x[ 7], 10, 0x432aff97); /* 50 */
    149   II(c, d, a, b, x[14], 15, 0xab9423a7); /* 51 */
    150   II(b, c, d, a, x[ 5], 21, 0xfc93a039); /* 52 */
    151   II(a, b, c, d, x[12], 6, 0x655b59c3); /* 53 */
    152   II(d, a, b, c, x[ 3], 10, 0x8f0ccc92); /* 54 */
    153   II(c, d, a, b, x[10], 15, 0xffeff47d); /* 55 */
    154   II(b, c, d, a, x[ 1], 21, 0x85845dd1); /* 56 */
    155   II(a, b, c, d, x[ 8], 6, 0x6fa87e4f); /* 57 */
    156   II(d, a, b, c, x[15], 10, 0xfe2ce6e0); /* 58 */
    157   II(c, d, a, b, x[ 6], 15, 0xa3014314); /* 59 */
    158   II(b, c, d, a, x[13], 21, 0x4e0811a1); /* 60 */
    159   II(a, b, c, d, x[ 4], 6, 0xf7537e82); /* 61 */
    160   II(d, a, b, c, x[11], 10, 0xbd3af235); /* 62 */
    161   II(c, d, a, b, x[ 2], 15, 0x2ad7d2bb); /* 63 */
    162   II(b, c, d, a, x[ 9], 21, 0xeb86d391); /* 64 */
    163   state[0] += a;
    164   state[1] += b;
    165   state[2] += c;
    166   state[3] += d;
    167 }
    168 
    169 
    170 unsigned char plaintext_bytes[]="admin";//after encoding: 21232f297a57a5a743894a0e4a801fc3
    171 unsigned char ciphertext_bytes[16];
    172 void md5_test(void)
    173 {
    174   //object define
    175   MD5_CTX my_md5;
    176   //init object
    177   MD5Init(&my_md5);
    178   //encoding
    179   MD5Update(&my_md5,plaintext_bytes,strlen((char*)plaintext_bytes));
    180   MD5Final(&my_md5,ciphertext_bytes);
    181   
    182 }
    183 
    184 
    185 
    186 
    187 //==================== end of file ========================
    md5.c

     

  5. 因为32位机器 unsigned int == 32bits 为16位机器 unsigned int == 16bits 所以需要将 unsigned int ---- unsigned long 转换.

 

谢谢。

end.

md5信息摘要算法 基于16位机器运行

标签:output   摘要   click   散列   padding   md5算法   else   art   signed   

原文地址:https://www.cnblogs.com/lumao1122-Milolu/p/13223663.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!