|
Posted on 2006-11-09 20:54 neter 阅读(416) 评论(0) 编辑 收藏 引用 所属分类: 程序设计
http://www.ietf.org/rfc/rfc1321.txt 此为rfc1321.txt
1#include <stdio.h> 2 3typedef struct { 4 5 unsigned int state[4]; 6 7 unsigned int count[2]; 8 9 unsigned char buffer[64]; 10 11} MD5Context; 12 13 14 15void MD5_Init(MD5Context * context); 16 17void MD5_Update(MD5Context * context, unsigned char * buf, int len); 18 19void MD5_Final(MD5Context * context, unsigned char digest[16]); 20 21#define S11 7 22 23#define S12 12 24 25#define S13 17 26 27#define S14 22 28 29#define S21 5 30 31#define S22 9 32 33#define S23 14 34 35#define S24 20 36 37#define S31 4 38 39#define S32 11 40 41#define S33 16 42 43#define S34 23 44 45#define S41 6 46 47#define S42 10 48 49#define S43 15 50 51#define S44 21 52 53 54 55static unsigned char PADDING[64] = 56 57{ 58 59 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 60 61 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 62 63 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 64 65}; 66 67#define F(x, y, z) (((x) & (y)) | ((~x) & (z))) 68 69#define G(x, y, z) (((x) & (z)) | ((y) & (~z))) 70 71#define H(x, y, z) ((x) ^ (y) ^ (z)) 72 73#define I(x, y, z) ((y) ^ ((x) | (~z))) 74 75 76 77#define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n)))) 78 79 80 81#define FF(a, b, c, d, x, s, ac) \ 82 83 { \ 84 85 (a) += F((b), (c), (d)) + (x) + (unsigned int)(ac); \ 86 87 (a) = ROTATE_LEFT((a), (s)); \ 88 89 (a) += (b); \ 90 91 } 92 93 94 95#define GG(a, b, c, d, x, s, ac) \ 96 97 { \ 98 99 (a) += G((b), (c), (d)) + (x) + (unsigned int)(ac); \ 100 101 (a) = ROTATE_LEFT((a), (s)); \ 102 103 (a) += (b); \ 104 105 } 106 107 108 109#define HH(a, b, c, d, x, s, ac) \ 110 111 { \ 112 113 (a) += H((b), (c), (d)) + (x) + (unsigned int)(ac); \ 114 115 (a) = ROTATE_LEFT((a), (s)); \ 116 117 (a) += (b); \ 118 119 } 120 121 122 123#define II(a, b, c, d, x, s, ac) \ 124 125 { \ 126 127 (a) += I((b), (c), (d)) + (x) + (unsigned int)(ac); \ 128 129 (a) = ROTATE_LEFT((a), (s)); \ 130 131 (a) += (b); \ 132 133 } 134 135static void MD5_Encode(unsigned char * output, unsigned int * input, int len) 136 137{ 138 139 unsigned int i, j; 140 141 142 143 for (i = 0, j = 0; j < len; i++, j += 4) 144 145 { 146 147 output[j] = (unsigned char) (input[i] & 0xff); 148 149 output[j + 1] = (unsigned char) ((input[i] >> 8) & 0xff); 150 151 output[j + 2] = (unsigned char) ((input[i] >> 16) & 0xff); 152 153 output[j + 3] = (unsigned char) ((input[i] >> 24) & 0xff); 154 155 } 156 157} 158 159 160 161static void MD5_Decode(unsigned int * output, unsigned char * input, int len) 162 163{ 164 165 unsigned int i, j; 166 167 168 169 for (i = 0, j = 0; j < len; i++, j += 4) 170 171 { 172 173 output[i] = ((unsigned int) input[j]) | 174 175 (((unsigned int) input[j + 1]) << 8) | 176 177 (((unsigned int) input[j + 2]) << 16) | 178 179 (((unsigned int) input[j + 3]) << 24); 180 181 } 182 183} 184 185 186 187static void MD5_Transform(unsigned int state[4], unsigned char block[64]) 188 189{ 190 191 unsigned int a = state[0], b = state[1], c = state[2], d = state[3], x[16]; 192 193 194 195 MD5_Decode(x, block, 64); 196 197 198 199 /**//* Round 1 */ 200 201 FF(a, b, c, d, x[0], S11, 0xd76aa478); /**//* 1 */ 202 203 FF(d, a, b, c, x[1], S12, 0xe8c7b756); /**//* 2 */ 204 205 FF(c, d, a, b, x[2], S13, 0x242070db); /**//* 3 */ 206 207 FF(b, c, d, a, x[3], S14, 0xc1bdceee); /**//* 4 */ 208 209 FF(a, b, c, d, x[4], S11, 0xf57c0faf); /**//* 5 */ 210 211 FF(d, a, b, c, x[5], S12, 0x4787c62a); /**//* 6 */ 212 213 FF(c, d, a, b, x[6], S13, 0xa8304613); /**//* 7 */ 214 215 FF(b, c, d, a, x[7], S14, 0xfd469501); /**//* 8 */ 216 217 FF(a, b, c, d, x[8], S11, 0x698098d8); /**//* 9 */ 218 219 FF(d, a, b, c, x[9], S12, 0x8b44f7af); /**//* 10 */ 220 221 FF(c, d, a, b, x[10], S13, 0xffff5bb1); /**//* 11 */ 222 223 FF(b, c, d, a, x[11], S14, 0x895cd7be); /**//* 12 */ 224 225 FF(a, b, c, d, x[12], S11, 0x6b901122); /**//* 13 */ 226 227 FF(d, a, b, c, x[13], S12, 0xfd987193); /**//* 14 */ 228 229 FF(c, d, a, b, x[14], S13, 0xa679438e); /**//* 15 */ 230 231 FF(b, c, d, a, x[15], S14, 0x49b40821); /**//* 16 */ 232 233 234 235 /**//* Round 2 */ 236 237 GG(a, b, c, d, x[1], S21, 0xf61e2562); /**//* 17 */ 238 239 GG(d, a, b, c, x[6], S22, 0xc040b340); /**//* 18 */ 240 241 GG(c, d, a, b, x[11], S23, 0x265e5a51); /**//* 19 */ 242 243 GG(b, c, d, a, x[0], S24, 0xe9b6c7aa); /**//* 20 */ 244 245 GG(a, b, c, d, x[5], S21, 0xd62f105d); /**//* 21 */ 246 247 GG(d, a, b, c, x[10], S22, 0x2441453); /**//* 22 */ 248 249 GG(c, d, a, b, x[15], S23, 0xd8a1e681); /**//* 23 */ 250 251 GG(b, c, d, a, x[4], S24, 0xe7d3fbc8); /**//* 24 */ 252 253 GG(a, b, c, d, x[9], S21, 0x21e1cde6); /**//* 25 */ 254 255 GG(d, a, b, c, x[14], S22, 0xc33707d6); /**//* 26 */ 256 257 GG(c, d, a, b, x[3], S23, 0xf4d50d87); /**//* 27 */ 258 259 GG(b, c, d, a, x[8], S24, 0x455a14ed); /**//* 28 */ 260 261 GG(a, b, c, d, x[13], S21, 0xa9e3e905); /**//* 29 */ 262 263 GG(d, a, b, c, x[2], S22, 0xfcefa3f8); /**//* 30 */ 264 265 GG(c, d, a, b, x[7], S23, 0x676f02d9); /**//* 31 */ 266 267 GG(b, c, d, a, x[12], S24, 0x8d2a4c8a); /**//* 32 */ 268 269 270 271 /**//* Round 3 */ 272 273 HH(a, b, c, d, x[5], S31, 0xfffa3942); /**//* 33 */ 274 275 HH(d, a, b, c, x[8], S32, 0x8771f681); /**//* 34 */ 276 277 HH(c, d, a, b, x[11], S33, 0x6d9d6122); /**//* 35 */ 278 279 HH(b, c, d, a, x[14], S34, 0xfde5380c); /**//* 36 */ 280 281 HH(a, b, c, d, x[1], S31, 0xa4beea44); /**//* 37 */ 282 283 HH(d, a, b, c, x[4], S32, 0x4bdecfa9); /**//* 38 */ 284 285 HH(c, d, a, b, x[7], S33, 0xf6bb4b60); /**//* 39 */ 286 287 HH(b, c, d, a, x[10], S34, 0xbebfbc70); /**//* 40 */ 288 289 HH(a, b, c, d, x[13], S31, 0x289b7ec6); /**//* 41 */ 290
|