1、MD5算法程序/ shiyan.cpp : 定义控制台应用程序的入口点。/#include stdafx.h#include #include #include /* Constants for MD5Transform routine.*/#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#d
2、efine S42 10#define S43 15#define S44 21#ifndef PROTOTYPES#define PROTOTYPES 0#endif/* PROTO_LIST is defined depending on how PROTOTYPES is defined above.If using PROTOTYPES, then PROTO_LIST returns the list, otherwise it returns an empty list.*/#if PROTOTYPES#define PROTO_LIST(list) list#else#defin
3、e PROTO_LIST(list) ()#endif/* 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) (32-(n)/* FF, GG,
4、HH, and II transformations for rounds 1, 2, 3, and 4.Rotation is separate from addition to prevent recomputation.*/#define FF(a, b, c, d, x, s, ac) (a) += F (b), (c), (d) + (x) + (UINT4)(ac); (a) = ROTATE_LEFT (a), (s); (a) += (b); #define GG(a, b, c, d, x, s, ac) (a) += G (b), (c), (d) + (x) + (UIN
5、T4)(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); #define MD_CTX MD
6、5_CTX#define MDInit MD5Init#define MD5Update MD5Update#define MDFinal MD5Final/* Length of test block, number of test blocks.*/#define TEST_BLOCK_LEN 10000#define TEST_BLOCK_COUNT 10000/* POINTER defines a generic pointer type */typedef unsigned char *POINTER;/* UINT2 defines a two byte word */typed
7、ef unsigned short int UINT2;/* UINT4 defines a four byte word */typedef unsigned long int UINT4;/* MD5 context. */typedef struct UINT4 state4; /* state (ABCD) */ UINT4 count2; /* number of bits, modulo 264 (lsb first) */ unsigned char buffer64; /* input buffer */ MD5_CTX;void MD5Init PROTO_LIST (MD5
8、_CTX *);void MD5Update PROTO_LIST(MD5_CTX *, unsigned char *, unsigned int);void MD5Final PROTO_LIST (unsigned char 16, MD5_CTX *);static void MD5Transform PROTO_LIST (UINT4 4, unsigned char 64);static void Encode PROTO_LIST(unsigned char *, UINT4 *, unsigned int);static void Decode PROTO_LIST(UINT4
9、 *, unsigned char *, unsigned int);static void MD5_memcpy PROTO_LIST (POINTER, POINTER, unsigned int);static void MD5_memset PROTO_LIST (POINTER, int, unsigned int);static unsigned char PADDING64 = 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,
10、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;/* MD5 initialization. Begins an MD5 operation, writing a new context.*/void MD5Init (MD5_CTX *context) /* context */ context-count0 = context-count1 = 0; /* Load magic initialization constants.*/ context-sta
11、te0 = 0x67452301; context-state1 = 0xefcdab89; context-state2 = 0x98badcfe; context-state3 = 0x10325476;/* 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+) outputi = inputi;/* Note
12、: 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;/* Decodes input (unsigned char) into output (UINT4). Assumes len isa multiple of 4.*/static void Decode (UIN
13、T4 *output, unsigned char *input, unsigned int len) unsigned int i, j; for (i = 0, j = 0; j len; i+, j += 4) outputi = (UINT4)inputj) | (UINT4)inputj+1) 8) | (UINT4)inputj+2) 16) | (UINT4)inputj+3) count0 3) & 0x3F); /* Update number of bits */ if (context-count0 += (UINT4)inputLen 3) (UINT4)inputLe
14、n count1+; context-count1 += (UINT4)inputLen 29); partLen = 64 - index; /* Transform as many times as possible.*/ if (inputLen = partLen) MD5_memcpy(POINTER)&context-bufferindex, (POINTER)input, partLen); MD5Transform (context-state, context-buffer); for (i = partLen; i + 63 state, &inputi); index =
15、 0; else i = 0; /* Buffer remaining input */ MD5_memcpy(POINTER)&context-bufferindex, (POINTER)&inputi,inputLen-i);/* 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 (
16、i = 0, j = 0; j 8) & 0xff); outputj+2 = (unsigned char)(inputi 16) & 0xff); outputj+3 = (unsigned char)(inputi 24) & 0xff); /* MD5 finalization. Ends an MD5 message-digest operation, writing the the message digest and zeroizing the context.*/void MD5Final (unsigned char digest16, MD5_CTX *context) unsigned char bits8; unsigned int index, padLen; /* Save number of bits */ Encode (bits, context-count, 8); /* Pad out to 56 mod 64.*/ index = (unsigned int)(context-count0 3) & 0x3f); padLen = (index 56) ? (56 - index) : (120 - index); MD5Update (c
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1