00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00035
00036 #ifndef _MD5_H_
00037 #define _MD5_H_
00038
00039 typedef unsigned int uint4;
00040 typedef unsigned short int uint2;
00041 typedef unsigned char uchar;
00042
00043 char* PrintMD5(uchar md5Digest[16]);
00044 char* MD5String(char* szString);
00045 char* MD5File(char* szFilename);
00046
00047 class md5
00048 {
00049
00050 public:
00051 md5() { Init(); }
00052 void Init();
00053 void Update(uchar* chInput, uint4 nInputLen);
00054 void Finalize();
00055 uchar* Digest() { return m_Digest; }
00056
00057 private:
00058
00059 void Transform(uchar* block);
00060 void Encode(uchar* dest, uint4* src, uint4 nLength);
00061 void Decode(uint4* dest, uchar* src, uint4 nLength);
00062
00063
00064 inline uint4 rotate_left(uint4 x, uint4 n)
00065 { return ((x << n) | (x >> (32-n))); }
00066
00067 inline uint4 F(uint4 x, uint4 y, uint4 z)
00068 { return ((x & y) | (~x & z)); }
00069
00070 inline uint4 G(uint4 x, uint4 y, uint4 z)
00071 { return ((x & z) | (y & ~z)); }
00072
00073 inline uint4 H(uint4 x, uint4 y, uint4 z)
00074 { return (x ^ y ^ z); }
00075
00076 inline uint4 I(uint4 x, uint4 y, uint4 z)
00077 { return (y ^ (x | ~z)); }
00078
00079 inline void FF(uint4& a, uint4 b, uint4 c, uint4 d, uint4 x, uint4 s, uint4 ac)
00080 { a += F(b, c, d) + x + ac; a = rotate_left(a, s); a += b; }
00081
00082 inline void GG(uint4& a, uint4 b, uint4 c, uint4 d, uint4 x, uint4 s, uint4 ac)
00083 { a += G(b, c, d) + x + ac; a = rotate_left(a, s); a += b; }
00084
00085 inline void HH(uint4& a, uint4 b, uint4 c, uint4 d, uint4 x, uint4 s, uint4 ac)
00086 { a += H(b, c, d) + x + ac; a = rotate_left(a, s); a += b; }
00087
00088 inline void II(uint4& a, uint4 b, uint4 c, uint4 d, uint4 x, uint4 s, uint4 ac)
00089 { a += I(b, c, d) + x + ac; a = rotate_left(a, s); a += b; }
00090
00091
00092 private:
00093 uint4 m_State[4];
00094 uint4 m_Count[2];
00095 uchar m_Buffer[64];
00096 uchar m_Digest[16];
00097 uchar m_Finalized;
00098
00099 };
00100
00101 #endif