Changeset 388 for python/vendor/current/Modules/md5.c
- Timestamp:
- Mar 19, 2014, 11:11:30 AM (11 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
python/vendor/current/Modules/md5.c
r2 r388 28 28 This code implements the MD5 Algorithm defined in RFC 1321, whose 29 29 text is available at 30 30 http://www.ietf.org/rfc/rfc1321.txt 31 31 The code is derived from the text of the RFC, including the test suite 32 32 (section A.5) but excluding the rest of Appendix A. It does not include … … 39 39 40 40 2002-04-13 lpd Clarified derivation from RFC 1321; now handles byte order 41 42 41 either statically or dynamically; added missing #include <string.h> 42 in library. 43 43 2002-03-11 lpd Corrected argument list for main(), and added int return 44 44 type, in test program and T value program. 45 45 2002-02-21 lpd Added missing #include <stdio.h> in test program. 46 46 2000-07-03 lpd Patched to eliminate warnings about "constant is 47 48 47 unsigned in ANSI C, signed in traditional"; made test program 48 self-checking. 49 49 1999-11-04 lpd Edited comments slightly for automatic TOC extraction. 50 50 1999-10-18 lpd Fixed typo in header comment (ansi2knr rather than md5). … … 56 56 #include <limits.h> 57 57 58 #undef BYTE_ORDER 58 #undef BYTE_ORDER /* 1 = big-endian, -1 = little-endian, 0 = unknown */ 59 59 #ifdef ARCH_IS_BIG_ENDIAN 60 60 # define BYTE_ORDER (ARCH_IS_BIG_ENDIAN ? 1 : -1) … … 134 134 { 135 135 md5_word_t 136 137 136 a = pms->abcd[0], b = pms->abcd[1], 137 c = pms->abcd[2], d = pms->abcd[3]; 138 138 md5_word_t t; 139 139 #if BYTE_ORDER > 0 … … 148 148 { 149 149 #if BYTE_ORDER == 0 150 151 152 153 154 155 156 157 158 #endif 159 #if BYTE_ORDER <= 0 160 161 162 163 164 165 166 167 168 169 170 171 172 173 150 /* 151 * Determine dynamically whether this is a big-endian or 152 * little-endian machine, since we can use a more efficient 153 * algorithm on the latter. 154 */ 155 static const int w = 1; 156 157 if (*((const md5_byte_t *)&w)) /* dynamic little-endian */ 158 #endif 159 #if BYTE_ORDER <= 0 /* little-endian */ 160 { 161 /* 162 * On little-endian machines, we can process properly aligned 163 * data without copying it. 164 */ 165 if (!((data - (const md5_byte_t *)0) & 3)) { 166 /* data are properly aligned */ 167 X = (const md5_word_t *)data; 168 } else { 169 /* not aligned */ 170 memcpy(xbuf, data, 64); 171 X = xbuf; 172 } 173 } 174 174 #endif 175 175 #if BYTE_ORDER == 0 176 else/* dynamic big-endian */177 #endif 178 #if BYTE_ORDER >= 0 179 180 181 182 183 184 185 176 else /* dynamic big-endian */ 177 #endif 178 #if BYTE_ORDER >= 0 /* big-endian */ 179 { 180 /* 181 * On big-endian machines, we must arrange the bytes in the 182 * right order. 183 */ 184 const md5_byte_t *xp = data; 185 int i; 186 186 187 187 # if BYTE_ORDER == 0 188 X = xbuf;/* (dynamic only) */188 X = xbuf; /* (dynamic only) */ 189 189 # else 190 # define xbuf X 190 # define xbuf X /* (static only) */ 191 191 # endif 192 193 194 192 for (i = 0; i < 16; ++i, xp += 4) 193 xbuf[i] = xp[0] + (xp[1] << 8) + (xp[2] << 16) + (xp[3] << 24); 194 } 195 195 #endif 196 196 } … … 322 322 323 323 void 324 md5_append(md5_state_t *pms, const md5_byte_t *data, int nbytes)324 md5_append(md5_state_t *pms, const md5_byte_t *data, unsigned int nbytes) 325 325 { 326 326 const md5_byte_t *p = data; 327 int left = nbytes;328 int offset = (pms->count[0] >> 3) & 63;327 unsigned int left = nbytes; 328 unsigned int offset = (pms->count[0] >> 3) & 63; 329 329 md5_word_t nbits = (md5_word_t)(nbytes << 3); 330 330 331 331 if (nbytes <= 0) 332 332 return; 333 333 334 334 /* this special case is handled recursively */ 335 335 if (nbytes > INT_MAX - offset) { 336 int overlap;336 unsigned int overlap; 337 337 338 338 /* handle the append in two steps to prevent overflow */ … … 340 340 341 341 md5_append(pms, data, overlap); 342 md5_append(pms, data + overlap, nbytes - overlap); 342 md5_append(pms, data + overlap, nbytes - overlap); 343 343 return; 344 344 } … … 348 348 pms->count[0] += nbits; 349 349 if (pms->count[0] < nbits) 350 350 pms->count[1]++; 351 351 352 352 /* Process an initial partial block. */ 353 353 if (offset) { 354 355 356 357 358 359 360 361 354 unsigned int copy = (offset + nbytes > 64 ? 64 - offset : nbytes); 355 356 memcpy(pms->buf + offset, p, copy); 357 if (offset + copy < 64) 358 return; 359 p += copy; 360 left -= copy; 361 md5_process(pms, pms->buf); 362 362 } 363 363 364 364 /* Process full blocks. */ 365 365 for (; left >= 64; p += 64, left -= 64) 366 366 md5_process(pms, p); 367 367 368 368 /* Process a final partial block. */ 369 369 if (left) 370 370 memcpy(pms->buf, p, left); 371 371 } 372 372 … … 375 375 { 376 376 static const md5_byte_t pad[64] = { 377 378 379 380 377 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 378 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 379 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 380 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 381 381 }; 382 382 md5_byte_t data[8]; … … 385 385 /* Save the length before padding. */ 386 386 for (i = 0; i < 8; ++i) 387 387 data[i] = (md5_byte_t)(pms->count[i >> 2] >> ((i & 3) << 3)); 388 388 /* Pad to 56 bytes mod 64. */ 389 389 md5_append(pms, pad, ((55 - (pms->count[0] >> 3)) & 63) + 1); … … 391 391 md5_append(pms, data, 8); 392 392 for (i = 0; i < 16; ++i) 393 393 digest[i] = (md5_byte_t)(pms->abcd[i >> 2] >> ((i & 3) << 3)); 394 394 }
Note:
See TracChangeset
for help on using the changeset viewer.