Doxygen Source Code Documentation
niml_md5.c File Reference
#include "niml_private.h"
Go to the source code of this file.
Defines | |
#define | S11 7 |
#define | S12 12 |
#define | S13 17 |
#define | S14 22 |
#define | S21 5 |
#define | S22 9 |
#define | S23 14 |
#define | S24 20 |
#define | S31 4 |
#define | S32 11 |
#define | S33 16 |
#define | S34 23 |
#define | S41 6 |
#define | S42 10 |
#define | S43 15 |
#define | S44 21 |
#define | F(x, y, z) (((x) & (y)) | ((~x) & (z))) |
#define | G(x, y, z) (((x) & (z)) | ((y) & (~z))) |
#define | H(x, y, z) ((x) ^ (y) ^ (z)) |
#define | I(x, y, z) ((y) ^ ((x) | (~z))) |
#define | ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n)))) |
#define | FF(a, b, c, d, x, s, ac) |
#define | GG(a, b, c, d, x, s, ac) |
#define | HH(a, b, c, d, x, s, ac) |
#define | II(a, b, c, d, x, s, ac) |
Functions | |
void | MD5Transform (UINT4[4], unsigned char[64]) |
void | Encode (unsigned char *, UINT4 *, unsigned int) |
void | Decode (UINT4 *, unsigned char *, unsigned int) |
void | MD5Init (MD5_CTX *context) |
void | MD5Update (MD5_CTX *context, unsigned char *input, unsigned int inputLen) |
void | MD5Final (unsigned char digest[16], MD5_CTX *context) |
char * | MD5_static_printf (unsigned char digest[16]) |
char * | MD5_static_array (int n, char *bytes) |
char * | MD5_malloc_array (int n, char *bytes) |
char * | MD5_static_string (char *string) |
char * | MD5_malloc_string (char *string) |
char * | MD5_static_file (char *filename) |
char * | MD5_malloc_file (char *filename) |
char * | MD5_to_B64 (unsigned char digest[16]) |
char * | MD5_B64_array (int n, char *bytes) |
char * | MD5_B64_string (char *string) |
char * | MD5_B64_file (char *filename) |
Variables | |
unsigned char | PADDING [64] |
Define Documentation
|
Definition at line 67 of file niml_md5.c. |
|
Value: { \ (a) += F ((b), (c), (d)) + (x) + (UINT4)(ac); \ (a) = ROTATE_LEFT ((a), (s)); \ (a) += (b); \ } Definition at line 79 of file niml_md5.c. Referenced by MD5Transform(). |
|
Definition at line 68 of file niml_md5.c. |
|
Value: { \ (a) += G ((b), (c), (d)) + (x) + (UINT4)(ac); \ (a) = ROTATE_LEFT ((a), (s)); \ (a) += (b); \ } Definition at line 85 of file niml_md5.c. Referenced by MD5Transform(). |
|
Definition at line 69 of file niml_md5.c. |
|
Value: { \ (a) += H ((b), (c), (d)) + (x) + (UINT4)(ac); \ (a) = ROTATE_LEFT ((a), (s)); \ (a) += (b); \ } Definition at line 91 of file niml_md5.c. Referenced by MD5Transform(), and pfit(). |
|
Definition at line 70 of file niml_md5.c. |
|
Value: { \ (a) += I ((b), (c), (d)) + (x) + (UINT4)(ac); \ (a) = ROTATE_LEFT ((a), (s)); \ (a) += (b); \ } Definition at line 97 of file niml_md5.c. Referenced by MD5Transform(). |
|
Definition at line 74 of file niml_md5.c. |
|
Definition at line 42 of file niml_md5.c. Referenced by MD5Transform(). |
|
Definition at line 43 of file niml_md5.c. Referenced by MD5Transform(). |
|
Definition at line 44 of file niml_md5.c. Referenced by MD5Transform(). |
|
Definition at line 45 of file niml_md5.c. Referenced by MD5Transform(). |
|
Definition at line 46 of file niml_md5.c. Referenced by MD5Transform(). |
|
Definition at line 47 of file niml_md5.c. Referenced by MD5Transform(). |
|
Definition at line 48 of file niml_md5.c. Referenced by MD5Transform(). |
|
Definition at line 49 of file niml_md5.c. Referenced by MD5Transform(). |
|
Definition at line 50 of file niml_md5.c. Referenced by MD5Transform(). |
|
Definition at line 51 of file niml_md5.c. Referenced by MD5Transform(). |
|
Definition at line 52 of file niml_md5.c. Referenced by MD5Transform(). |
|
Definition at line 53 of file niml_md5.c. Referenced by MD5Transform(). |
|
Definition at line 54 of file niml_md5.c. Referenced by MD5Transform(). |
|
Definition at line 55 of file niml_md5.c. Referenced by MD5Transform(). |
|
Definition at line 56 of file niml_md5.c. Referenced by MD5Transform(). |
|
Definition at line 57 of file niml_md5.c. Referenced by MD5Transform(). |
Function Documentation
|
Decodes input (unsigned char) into output (UINT4). Assumes len is a multiple of 4. ------------------------------------------------------------------------ Definition at line 316 of file niml_md5.c. References i.
|
|
Encodes input (UINT4) into output (unsigned char). Assumes len is a multiple of 4. ------------------------------------------------------------------------ Definition at line 299 of file niml_md5.c. References i.
00300 { 00301 unsigned int i, j; 00302 00303 for (i = 0, j = 0; j < len; i++, j += 4) { 00304 output[j] = (unsigned char)(input[i] & 0xff); 00305 output[j+1] = (unsigned char)((input[i] >> 8) & 0xff); 00306 output[j+2] = (unsigned char)((input[i] >> 16) & 0xff); 00307 output[j+3] = (unsigned char)((input[i] >> 24) & 0xff); 00308 } 00309 } |
|
Return the MD5 hash of an array as a Base64 string, instead of a hex string. strlen(result) is 22 instead of 32 result is malloc()-ed and should be free()-d when appropriate ------------------------------------------------------------------------------ Definition at line 466 of file niml_md5.c. References MD5_to_B64(), MD5Final(), MD5Init(), and MD5Update().
00467 { 00468 MD5_CTX context; 00469 unsigned char digest[16]; 00470 00471 if( n < 0 || bytes == NULL ) return NULL ; 00472 00473 MD5Init( &context ) ; 00474 MD5Update( &context, (unsigned char *)bytes, n ) ; 00475 MD5Final( digest, &context ) ; 00476 00477 return MD5_to_B64( digest ) ; 00478 } |
|
Return the MD5 hash of a file as a Base64 string, instead of a hex string.
Definition at line 500 of file niml_md5.c. References file, MD5_to_B64(), MD5Final(), MD5Init(), and MD5Update().
00501 { 00502 FILE *file; 00503 MD5_CTX context; 00504 int len; 00505 unsigned char buffer[1024] ; 00506 unsigned char digest[16] ; 00507 00508 if( (file=fopen (filename, "rb")) == NULL ) return NULL ; 00509 00510 MD5Init( &context ) ; 00511 00512 while( len = fread(buffer, 1, 1024, file) ) 00513 MD5Update( &context, buffer, len ) ; 00514 00515 MD5Final( digest, &context ); 00516 fclose (file); 00517 00518 return MD5_to_B64( digest ) ; 00519 } |
|
Return the MD5 hash of a C string as a Base64 string, instead of a hex string. strlen(result) is 22 instead of 32 result is malloc()-ed and should be free()-d when appropriate ------------------------------------------------------------------------------ Definition at line 487 of file niml_md5.c. References MD5_B64_array().
00488 { 00489 if( string == NULL ) string = "ElvisTheKing" ; 00490 return MD5_B64_array( strlen(string) , string ) ; 00491 } |
|
Digest an array and returns the printable string of the result, stored in a malloc()-ed array (length=32+1 bytes). ------------------------------------------------------------------------ Definition at line 372 of file niml_md5.c. References malloc, and MD5_static_array().
00373 { 00374 char *st , *dy ; 00375 st = MD5_static_array( n , bytes ) ; 00376 if( st == NULL ) return NULL ; 00377 dy = (char *) malloc(33) ; strcpy(dy,st) ; return dy ; 00378 } |
|
Digests a file and prints the result, stored in a malloc()-ed array (length=32+1 bytes). ------------------------------------------------------------------------ Definition at line 433 of file niml_md5.c. References malloc, and MD5_static_file().
00434 { 00435 char *st , *dy ; 00436 00437 st = MD5_static_file( filename ) ; 00438 if( st == NULL ) return NULL ; 00439 dy = (char *) malloc(33) ; strcpy(dy,st) ; return dy ; 00440 } |
|
Digest a C string and returns the printable string of the result, stored in a malloc()-ed array (length=32+1 bytes). ------------------------------------------------------------------------ Definition at line 396 of file niml_md5.c. References MD5_malloc_array().
00397 { 00398 if( string == NULL ) string = "ElvisTheKing" ; 00399 return MD5_malloc_array( strlen(string)+1 , string ) ; 00400 } |
|
Digest an array and returns the printable string of the result, stored in a static array (length=32+1 bytes). ------------------------------------------------------------------------ Definition at line 353 of file niml_md5.c. References MD5_static_printf(), MD5Final(), MD5Init(), and MD5Update().
00354 { 00355 MD5_CTX context; 00356 unsigned char digest[16]; 00357 00358 if( n < 0 || bytes == NULL ) return NULL ; 00359 00360 MD5Init( &context ) ; 00361 MD5Update( &context, (unsigned char *)bytes, n ) ; 00362 MD5Final( digest, &context ) ; 00363 00364 return MD5_static_printf(digest) ; 00365 } |
|
Digests a file and prints the result, stored in a static array (length=32+1 bytes). ------------------------------------------------------------------------ Definition at line 407 of file niml_md5.c. References file, MD5_static_printf(), MD5Final(), MD5Init(), and MD5Update().
00408 { 00409 FILE *file; 00410 MD5_CTX context; 00411 int len; 00412 unsigned char buffer[1024] ; 00413 unsigned char digest[16] ; 00414 00415 if( (file = fopen(filename, "rb")) == NULL ) return NULL ; 00416 00417 MD5Init( &context ) ; 00418 00419 while( len = fread(buffer, 1, 1024, file) ) 00420 MD5Update( &context, buffer, len ) ; 00421 00422 MD5Final( digest, &context ); 00423 fclose (file); 00424 00425 return MD5_static_printf( digest ) ; 00426 } |
|
Function to print a 128 bit digest into a static 32 char string. ------------------------------------------------------------------------ Definition at line 333 of file niml_md5.c.
00334 { 00335 static char st[33] ; 00336 00337 sprintf(st, 00338 "%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x" , 00339 digest[0] , digest[1] , digest[2] , digest[3] , digest[4] , 00340 digest[5] , digest[6] , digest[7] , digest[8] , digest[9] , 00341 digest[10], digest[11], digest[12], digest[13], digest[14], 00342 digest[15] 00343 ) ; 00344 00345 return st ; 00346 } |
|
Digest a C string and returns the printable string of the result, stored in a static array (length=32+1 bytes). ------------------------------------------------------------------------ Definition at line 385 of file niml_md5.c. References MD5_static_array().
00386 { 00387 if( string == NULL ) string = "ElvisTheKing" ; 00388 return MD5_static_array( strlen(string) , string ) ; 00389 } |
|
Convert a MD5 hex string to a Base64-ed string. strlen(result) is 22 instead of 32 result is malloc()-ed and should be free()-d when appropriate ------------------------------------------------------------------------------ Definition at line 448 of file niml_md5.c. References B64_to_base64().
00449 { 00450 int nb64=0 ; byte *b64=NULL ; 00451 00452 B64_to_base64( 16 , (byte *)digest , &nb64 , &b64 ) ; 00453 if( nb64 <= 0 || b64 == NULL ) return NULL ; 00454 b64[nb64-3] = '\0' ; /* remove trailing "==" */ 00455 if( isspace(b64[nb64-4]) ) b64[nb64-4]='\0' ; 00456 return (char *)b64 ; 00457 } |
|
MD5 finalization. Ends an MD5 message-digest operation, writing the the message digest and zeroizing the context. ------------------------------------------------------------------------ Definition at line 170 of file niml_md5.c. References MD5_CTX::count, Encode(), MD5Update(), PADDING, POINTER, and MD5_CTX::state. Referenced by MD5_B64_array(), MD5_B64_file(), MD5_static_array(), MD5_static_file(), and UUID_hashcode().
00171 { 00172 unsigned char bits[8]; 00173 unsigned int index, padLen; 00174 00175 /* Save number of bits */ 00176 00177 Encode (bits, context->count, 8); 00178 00179 /* Pad out to 56 mod 64. */ 00180 00181 index = (unsigned int)((context->count[0] >> 3) & 0x3f); 00182 padLen = (index < 56) ? (56 - index) : (120 - index); 00183 MD5Update (context, PADDING, padLen); 00184 00185 /* Append length (before padding) */ 00186 00187 MD5Update (context, bits, 8); 00188 00189 /* Store state in digest */ 00190 00191 Encode (digest, context->state, 16); 00192 00193 /* Zeroize sensitive information. */ 00194 00195 memset ((POINTER)context, 0, sizeof (*context)); 00196 } |
|
MD5 initialization. Begins an MD5 operation, writing a new context. ------------------------------------------------------------------------ Definition at line 107 of file niml_md5.c. References MD5_CTX::count, and MD5_CTX::state. Referenced by MD5_B64_array(), MD5_B64_file(), MD5_static_array(), MD5_static_file(), and UUID_hashcode().
|
|
MD5 basic transformation. Transforms state based on block. ------------------------------------------------------------------------ Definition at line 202 of file niml_md5.c. References a, c, Decode(), FF, GG, HH, II, S11, S12, S13, S14, S21, S22, S23, S24, S31, S32, S33, S34, S41, S42, S43, and S44.
00203 { 00204 UINT4 a = state[0], b = state[1], c = state[2], d = state[3], x[16]; 00205 00206 Decode (x, block, 64); 00207 00208 /* Round 1 */ 00209 00210 FF (a, b, c, d, x[ 0], S11, 0xd76aa478); /* 1 */ 00211 FF (d, a, b, c, x[ 1], S12, 0xe8c7b756); /* 2 */ 00212 FF (c, d, a, b, x[ 2], S13, 0x242070db); /* 3 */ 00213 FF (b, c, d, a, x[ 3], S14, 0xc1bdceee); /* 4 */ 00214 FF (a, b, c, d, x[ 4], S11, 0xf57c0faf); /* 5 */ 00215 FF (d, a, b, c, x[ 5], S12, 0x4787c62a); /* 6 */ 00216 FF (c, d, a, b, x[ 6], S13, 0xa8304613); /* 7 */ 00217 FF (b, c, d, a, x[ 7], S14, 0xfd469501); /* 8 */ 00218 FF (a, b, c, d, x[ 8], S11, 0x698098d8); /* 9 */ 00219 FF (d, a, b, c, x[ 9], S12, 0x8b44f7af); /* 10 */ 00220 FF (c, d, a, b, x[10], S13, 0xffff5bb1); /* 11 */ 00221 FF (b, c, d, a, x[11], S14, 0x895cd7be); /* 12 */ 00222 FF (a, b, c, d, x[12], S11, 0x6b901122); /* 13 */ 00223 FF (d, a, b, c, x[13], S12, 0xfd987193); /* 14 */ 00224 FF (c, d, a, b, x[14], S13, 0xa679438e); /* 15 */ 00225 FF (b, c, d, a, x[15], S14, 0x49b40821); /* 16 */ 00226 00227 /* Round 2 */ 00228 00229 GG (a, b, c, d, x[ 1], S21, 0xf61e2562); /* 17 */ 00230 GG (d, a, b, c, x[ 6], S22, 0xc040b340); /* 18 */ 00231 GG (c, d, a, b, x[11], S23, 0x265e5a51); /* 19 */ 00232 GG (b, c, d, a, x[ 0], S24, 0xe9b6c7aa); /* 20 */ 00233 GG (a, b, c, d, x[ 5], S21, 0xd62f105d); /* 21 */ 00234 GG (d, a, b, c, x[10], S22, 0x2441453); /* 22 */ 00235 GG (c, d, a, b, x[15], S23, 0xd8a1e681); /* 23 */ 00236 GG (b, c, d, a, x[ 4], S24, 0xe7d3fbc8); /* 24 */ 00237 GG (a, b, c, d, x[ 9], S21, 0x21e1cde6); /* 25 */ 00238 GG (d, a, b, c, x[14], S22, 0xc33707d6); /* 26 */ 00239 GG (c, d, a, b, x[ 3], S23, 0xf4d50d87); /* 27 */ 00240 GG (b, c, d, a, x[ 8], S24, 0x455a14ed); /* 28 */ 00241 GG (a, b, c, d, x[13], S21, 0xa9e3e905); /* 29 */ 00242 GG (d, a, b, c, x[ 2], S22, 0xfcefa3f8); /* 30 */ 00243 GG (c, d, a, b, x[ 7], S23, 0x676f02d9); /* 31 */ 00244 GG (b, c, d, a, x[12], S24, 0x8d2a4c8a); /* 32 */ 00245 00246 /* Round 3 */ 00247 00248 HH (a, b, c, d, x[ 5], S31, 0xfffa3942); /* 33 */ 00249 HH (d, a, b, c, x[ 8], S32, 0x8771f681); /* 34 */ 00250 HH (c, d, a, b, x[11], S33, 0x6d9d6122); /* 35 */ 00251 HH (b, c, d, a, x[14], S34, 0xfde5380c); /* 36 */ 00252 HH (a, b, c, d, x[ 1], S31, 0xa4beea44); /* 37 */ 00253 HH (d, a, b, c, x[ 4], S32, 0x4bdecfa9); /* 38 */ 00254 HH (c, d, a, b, x[ 7], S33, 0xf6bb4b60); /* 39 */ 00255 HH (b, c, d, a, x[10], S34, 0xbebfbc70); /* 40 */ 00256 HH (a, b, c, d, x[13], S31, 0x289b7ec6); /* 41 */ 00257 HH (d, a, b, c, x[ 0], S32, 0xeaa127fa); /* 42 */ 00258 HH (c, d, a, b, x[ 3], S33, 0xd4ef3085); /* 43 */ 00259 HH (b, c, d, a, x[ 6], S34, 0x4881d05); /* 44 */ 00260 HH (a, b, c, d, x[ 9], S31, 0xd9d4d039); /* 45 */ 00261 HH (d, a, b, c, x[12], S32, 0xe6db99e5); /* 46 */ 00262 HH (c, d, a, b, x[15], S33, 0x1fa27cf8); /* 47 */ 00263 HH (b, c, d, a, x[ 2], S34, 0xc4ac5665); /* 48 */ 00264 00265 /* Round 4 */ 00266 00267 II (a, b, c, d, x[ 0], S41, 0xf4292244); /* 49 */ 00268 II (d, a, b, c, x[ 7], S42, 0x432aff97); /* 50 */ 00269 II (c, d, a, b, x[14], S43, 0xab9423a7); /* 51 */ 00270 II (b, c, d, a, x[ 5], S44, 0xfc93a039); /* 52 */ 00271 II (a, b, c, d, x[12], S41, 0x655b59c3); /* 53 */ 00272 II (d, a, b, c, x[ 3], S42, 0x8f0ccc92); /* 54 */ 00273 II (c, d, a, b, x[10], S43, 0xffeff47d); /* 55 */ 00274 II (b, c, d, a, x[ 1], S44, 0x85845dd1); /* 56 */ 00275 II (a, b, c, d, x[ 8], S41, 0x6fa87e4f); /* 57 */ 00276 II (d, a, b, c, x[15], S42, 0xfe2ce6e0); /* 58 */ 00277 II (c, d, a, b, x[ 6], S43, 0xa3014314); /* 59 */ 00278 II (b, c, d, a, x[13], S44, 0x4e0811a1); /* 60 */ 00279 II (a, b, c, d, x[ 4], S41, 0xf7537e82); /* 61 */ 00280 II (d, a, b, c, x[11], S42, 0xbd3af235); /* 62 */ 00281 II (c, d, a, b, x[ 2], S43, 0x2ad7d2bb); /* 63 */ 00282 II (b, c, d, a, x[ 9], S44, 0xeb86d391); /* 64 */ 00283 00284 state[0] += a; 00285 state[1] += b; 00286 state[2] += c; 00287 state[3] += d; 00288 00289 /* Zeroize sensitive information. */ 00290 00291 memset ((POINTER)x, 0, sizeof (x)); 00292 } |
|
MD5 block update operation. Continues an MD5 message-digest operation, processing another message block, and updating the context. ------------------------------------------------------------------------ Definition at line 125 of file niml_md5.c. References MD5_CTX::buffer, MD5_CTX::count, i, MD5Transform(), POINTER, and MD5_CTX::state. Referenced by MD5_B64_array(), MD5_B64_file(), MD5_static_array(), MD5_static_file(), MD5Final(), and UUID_hashcode().
00127 { 00128 unsigned int i, index, partLen; 00129 00130 /* Compute number of bytes mod 64 */ 00131 00132 index = (unsigned int)((context->count[0] >> 3) & 0x3F); 00133 00134 /* Update number of bits */ 00135 00136 if( (context->count[0] += ((UINT4)inputLen << 3)) < ((UINT4)inputLen << 3) ) 00137 context->count[1]++; 00138 00139 context->count[1] += ((UINT4)inputLen >> 29); 00140 00141 partLen = 64 - index; 00142 00143 /* Transform as many times as possible. */ 00144 00145 if (inputLen >= partLen) { 00146 00147 memcpy ((POINTER)&context->buffer[index], (POINTER)input, partLen); 00148 00149 MD5Transform (context->state, context->buffer); 00150 00151 for (i = partLen; i + 63 < inputLen; i += 64) 00152 MD5Transform (context->state, &input[i]); 00153 00154 index = 0; 00155 } 00156 else 00157 i = 0; 00158 00159 /* Buffer remaining input */ 00160 00161 memcpy ((POINTER)&context->buffer[index], (POINTER)&input[i], 00162 inputLen-i); 00163 } |
Variable Documentation
|
Initial value: { 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 } Definition at line 59 of file niml_md5.c. Referenced by MD5Final(). |