C Language MD5 Source code
Md5c.h:
/* POINTER defines a generic POINTER type */typedef unsigned char * POINTER; /* UINT2 defines a-byte word *///typedef unsigned short int UINT2; /* UINT4 defines a four byte word */typedef unsigned long int UINT4; /* MD5 context. */typedef struct {UINT4 state[4]; /* State (ABCD) */UINT4 count[2]; /* Number of bits, modulo 2^64 (LSB first) */unsigned char buffer[64]; /* Input buffer */} Md5_ctx; void Md5init (Md5_ctx *context); void Md5update (Md5_ctx *context, unsigned char *input, unsigned int inputlen); void md5upd Aterstring (md5_ctx *context,const char *string); int md5fileupdatefile (Md5_ctx *context,char *filename); void MD5Final ( unsigned char digest[16], md5_ctx *context); void MDString (char *string,unsigned char digest[16]); int Md5file (char *filen ame,unsigned Char digest[16]);
MD5C.C:
/* md5c. C-rsa Data Security, Inc., MD5 message-digest algorithm */* Copyright (C) 1991-2, RSA Data Security, Inc. Created 199 1. allrights reserved. License to copy and use this software are granted provided that ITIS identified as the "RSA Data Security, Inc. MD5 Message -digestalgorithm "in all material mentioning or referencing this softwareor this function. License is also granted to make and use derivative works providedthat such works be identified as "derived from the RSA D Atasecurity, Inc. MD5 message-digest algorithm "in any materialmentioning or referencing the derived work. RSA Data Security, Inc. makes no representations concerning eitherthe merchantability of this software or the suitability of Thissoftware for any particular purpose. It is provided ' as is ' without express or implied warranty of any kind. These notices must is retained in any copies of all part of thisdocumentation and/or software. */#include "md5c.h" #include <string.h> #include <stdio.h> /* Constants for Md5transform routine.*/#define S11 7#define S12 12#define S13 17#define S14 22#define S21 5#define S 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 static void md5_memcpy (POINTER output, POINTER input, unsigned int len), static void Md5transform (U INT4 State[4], unsigned char block[64]); static void Encode (unsigned char *output, UINT4 *input, unsigned int len); void Md5_memset (POINTER output, int value, unsigned int len), static void Decode (UINT4 *output, unsigned char *input, uns igned int len); static unsigned char padding[64] = {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}; /* F, G, H and I are basic MD5 functions.*/#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))) /* Rotate_left rotates x left n bits.*/#define ROTATE_LEFT (x, N) (((x) << (n)) | ((x) >> (32-(n)))/* FF, GG, HH, and II transformations for Rounds 1, 2, 3, and 4.Rotation are separate from Additi On to prevent recomputation.*/#define FF (A, B, C, D, X, S, ac) {\ (a) + = F ((b), (c), (d)) + (x) + (UINT4) (AC); \ (a) = R Otate_left ((a), (s)); \ (a) + = (b); \} #define GG (A, B, C, D, X, S, ac) {\ (a) + = G ((b), (c), (d)) + (x) + (UINT4) (AC), \ (a) = Rotate_left ((a), (s)); \ (A ) + = (b); \} #define HH (A, B, C, D, X, S, ac) {\ (a) + = H ((b), (c), (d)) + (x) + (UINT4) (AC), \ (a) = Rotate_left ((a), (s)); \ (A ) + = (b); \} #define II (A, B, C, D, X, S, ac) {\ (a) + = I ((b), (c), (d)) + (x) + (UINT4) (AC), \ (a) = Rotate_left ((a), (s)); \ (A ) + = (b); \}/* MD5 initialization. Begins an MD5 operation, writing a new context. */void md5init (Md5_ctx *context)/* Context */{Context-> count[0] = context->count[1] = 0; /* Load Magic Initialization constants. */context->state[0] = 0x67452301; CONTEXT->STATE[1] = 0xefcdab89; CONTEXT->STATE[2] = 0x98badcfe; CONTEXT->STATE[3] = 0x10325476;} /* MD5 block update operation. Continues an MD5 message-digest operation, processing another message block, and updating the context. */void md5update (Md5_ctx *context, unsigned char *input, unsigned int inputlen) {unsigned int i, index, Partlen; /* Compute number of bytes MoD * * * index = (unsigned int) ((Context->count[0] >> 3) & 0x3F); /* Update Number of bits */if ((context->count[0] + = ((UINT4) Inputlen << 3)) < ((UINT4) Inputlen << 3)) context->count[1]++; CONTEXT->COUNT[1] + = ((UINT4) Inputlen >> 29); Partlen = 64-index; /* Transform as many times as possible. */if (Inputlen >= partlen) {md5_memcpy ((POINTER) &context->buffer[index], (POINTER) input, Partlen); Md5transform (Context->state, COntext->buffer); for (i = Partlen; i + < Inputlen; i + =) md5transform (context->state, &input[i]); index = 0; } else i = 0; /* Buffer remaining input */md5_memcpy ((POINTER) &context->buffer[index], (POINTER) &input[i],inputlen-i);} /* MD5 finalization. Ends an MD5 message-digest operation, writing the message digest and zeroizing the context. */void md5final (unsigned char digest[16], md5_ctx *context) {unsigned char bits[8]; unsigned int index, Padlen; /* Save Number of bits */Encode (bits, context->count, 8); /* Pad out to 64 mod. */index = (unsigned int) ((Context->count[0] >> 3) & 0x3f); Padlen = (Index < 56)? (56-index): (120-index); Md5update (context, PADDING, Padlen); /* Append length (before padding) */md5update (context, bits, 8); /* Store State in Digest */Encode (Digest, Context->state, 16); /* Zeroize sensitive information. */Md5_memset ((POINTER) context, 0, sizeof (*context));} /* MD5 basic transformation. Transforms State based on block. */static void Md5transform (UINT4 state[4], unsigned char block[64]) {UINT4 a = state[0], B = state[1], c = state[2], d = STATE[3], x[16]; Decode (x, block, 64); /* Round 1 */FF (A, B, C, D, x[0], S11, 0xd76aa478); /* 1 */FF (d, a, B, C, x[1], S12, 0xe8c7b756); /* 2 */FF (c, D, a, B, x[2], S13, 0x242070db); /* 3 */FF (b, C, D, A, x[3], S14, 0xc1bdceee); /* 4 */FF (A, B, C, D, x[4], S11, 0XF57C0FAF); /* 5 */FF (d, a, B, C, x[5], S12, 0x4787c62a); /* 6 */FF (c, D, a, B, x[6], S13, 0xa8304613); /* 7 */FF (b, C, D, A, x[7], S14, 0xfd469501); /* 8 */FF (A, B, C, D, x[8], S11, 0X698098D8); /* 9 */FF (d, a, B, C, x[9], S12, 0X8B44F7AF); /* Ten */FF (c, D, a, B, x[10], S13, 0XFFFF5BB1); /* each */FF (b, C, D, A, x[11], S14, 0x895cd7be); /* */FF (A, B, C, D, X[12], S11, 0x6b901122); /* */FF (d, a, B, C, x[13], S12, 0xfd987193); /* */FF (c, D, a, B, x[14], S13, 0xa679438e); /* */FF (b, C, D,A, x[15], S14, 0x49b40821); /* * */* Round 2 */GG (A, B, C, D, x[1], S21, 0xf61e2562); /* + */GG (d, a, B, C, x[6], S22, 0xc040b340); /* */GG (c, D, a, B, x[11], S23, 0x265e5a51); /* */GG (b, C, D, A, x[0], S24, 0XE9B6C7AA); /* */GG (A, B, C, D, x[5], S21, 0xd62f105d); /* */GG (d, a, B, C, x[10], S22, 0x2441453); /* */GG (c, D, a, B, x[15], S23, 0xd8a1e681); /* */GG (b, C, D, A, x[4], S24, 0XE7D3FBC8); /* + */GG (A, B, C, D, x[9], S21, 0x21e1cde6); /* */GG (d, a, B, C, x[14], S22, 0XC33707D6); /* + */GG (c, D, a, B, x[3], S23, 0xf4d50d87); /* + */GG (b, C, D, A, x[8], S24, 0x455a14ed); /* */GG (A, B, C, D, X[13], S21, 0xa9e3e905); /* */GG (d, a, B, C, x[2], S22, 0XFCEFA3F8); /* + * GG (c, D, a, B, x[7], S23, 0X676F02D9); /* */GG (b, C, D, A, x[12], S24, 0x8d2a4c8a); /* * */* Round 3 */HH (A, B, C, D, x[5], S31, 0xfffa3942); /* */HH (d, a, B, C, x[8], S32, 0x8771f681); /* */HH (c, D, a, B, x[11], S33, 0x6d9d6122); /* */HH (b, C, D, A, x[14], S34, 0xfde5380c); /* */HH (A, B, C, D, x[1], S31, 0xa4beea44); /* Notoginseng */HH (d, a, B, C, x[4], S32, 0x4bdecfa9); /* * */HH (c, D, a, B, x[7], S33, 0xf6bb4b60); /* */HH (b, C, D, A, x[10], S34, 0XBEBFBC70); /* +/HH (A, B, C, D, X[13], S31, 0X289B7EC6); /* */HH (d, a, B, C, x[0], S32, 0XEAA127FA); /* */HH (c, D, a, B, x[3], S33, 0xd4ef3085); /* */HH (b, C, D, A, x[6], S34, 0X4881D05); /* * */HH (A, B, C, D, x[9], S31, 0xd9d4d039); /* */HH (d, a, B, C, x[12], S32, 0xe6db99e5); /* * */HH (c, D, a, B, x[15], S33, 0X1FA27CF8); /* */HH (b, C, D, A, x[2], S34, 0xc4ac5665); /* * * */* Round 4 */II (A, B, C, D, x[0], S41, 0xf4292244); /* * */II (d, a, B, C, x[7], S42, 0X432AFF97); /* */II (c, D, a, B, x[14], S43, 0XAB9423A7); /* Wuyi */II (b, C, D, A, x[5], S44, 0xfc93a039); /* * */II (A, B, C, D, X[12], S41, 0X655B59C3); /* +/II (d, a, B, C, x[3], S42, 0x8f0ccc92); /* * * */II (c, D, a, B, X[Ten], S43, 0xffeff47d); /* * */II (b, C, D, A, x[1], S44, 0X85845DD1); /* (A, B, C, D, x[8], S41, 0x6fa87e4f); /* x[15 */II (d, a, B, C,], S42, 0XFE2CE6E0); /* (+/-) +/II (c, D, a, B, x[6], S43, 0xa3014314); /* (b, C, D, A, x[13], S44, 0X4E0811A1); /* */II (A, B, C, D, x[4], S41, 0xf7537e82); /*//(d, a, B, C, x[11], S42, 0xbd3af235); /* * +/II (c, D, a, B, x[2], S43, 0X2AD7D2BB); /* */II (b, C, D, A, x[9], S44, 0xeb86d391); /* * */state[0] + = A; STATE[1] + = b; STATE[2] + = C; STATE[3] + = D; /* Zeroize sensitive information. */Md5_memset ((POINTER) x, 0, sizeof (x));} /* encodes input (UINT4) into output (unsigned char). Assumes Len is a multiple of 4. */static void Encode (unsigned char *output, UINT4 *input, unsigned int len) {unsigned int i, J; for (i = 0, j = 0; j < Len; i++, J + + 4) {Output[j] = (unsigned char) (Input[i] & 0xff); OUTPUT[J+1] = (unsigned char) ((Input[i] >> 8) & 0xff); OUTPUT[J+2] = (unsigned cHar) ((Input[i] >>) & 0xff); OUTPUT[J+3] = (unsigned char) ((Input[i] >>) & 0xff); }}/* Decodes input (unsigned char) into output (UINT4). Assumes Len is a multiple of 4. */static void Decode (UINT4 *output, unsigned char *input, unsigned int len) {unsigned int i, J; for (i = 0, j = 0; j < Len; i++, J + + 4) output[i] = ((UINT4) input[j]) | (((UINT4) input[j+1]) << 8) | (((UINT4) input[j+2]) << 16) | (((UINT4) input[j+3]) << 24);} /* note:replace ' for loop ' with standard memcpy if possible. */static void md5_memcpy (POINTER output, POINTER input, unsigned int len) {unsigned int i; for (i = 0; i < len; i++) output[i] = Input[i];} /* note:replace ' for loop ' with standard memset if possible. */static void Md5_memset (POINTER output, int value, unsigned int len) {unsigned int i; for (i = 0; i < len; i++) ((char *) output) [I] = (char) value;} /* Digests a string and prints the result. */void MDString (char *string,unsigned char DIGEST[16]) {MD5_CTX context; unsigned int len = strlen (string); Md5init (&context); Md5update (&context, (unsigned char *) string, len); Md5final (Digest, &context);} /* Digests a file and prints the result. */int Md5file (char *filename,unsigned char digest[16]) {FILE *file; Md5_ctx context; int Len; unsigned char buffer[1024]; if (file = fopen (filename, "RB")) = = NULL) return-1; else {md5init (&context); while (len = fread (buffer, 1, 1024x768, file)) md5update (&context, buffer, Len); Md5final (Digest, &context); fclose (file); } return 0;} void Md5updaterstring (Md5_ctx *context,const char *string) {unsigned int len = strlen (string); Md5update (context, (unsigned char *) string, len);} int Md5fileupdatefile (md5_ctx *context,char *filename) {FILE *file; int len; unsigned char buffer[1024]; if (file = fopen (filename, "RB")) = = NULL) return-1; else {while (len = fread (buffer, 1, 1024x768, file)) md5update (context, buffer, Len); fclose (file); } return 0;}
Usage:
void Main (void) { unsigned char digest[16];//Storage result //First usage: md5_ctx md5c; Md5init (&MD5C); Initialize md5updaterstring (&md5c, "the string you want to test"); Md5fileupdatefile (&md5c, "The file path you want to test"); Md5final (digest,&md5c); The second usage: MDString ("The string you want to test", digest);//Enter the string directly and draw the result //Third usage: md5file ("File path you want to test", Digest); Enter the file path directly and draw the result}