Md5.hpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  1. #ifndef __MD5_H__
  2. #define __MD5_H__
  3. // Copyright (C) 1991-2, RSA Data Security, Inc. Created 1991. All
  4. // rights reserved.
  5. // License to copy and use this software is granted provided that it
  6. // is identified as the "RSA Data Security, Inc. MD5 Message-Digest
  7. // Algorithm" in all material mentioning or referencing this software
  8. // or this function.
  9. //
  10. // License is also granted to make and use derivative works provided
  11. // that such works are identified as "derived from the RSA Data
  12. // Security, Inc. MD5 Message-Digest Algorithm" in all material
  13. // mentioning or referencing the derived work.
  14. //
  15. // RSA Data Security, Inc. makes no representations concerning either
  16. // the merchantability of this software or the suitability of this
  17. // software for any particular purpose. It is provided "as is"
  18. // without express or implied warranty of any kind.
  19. //
  20. // These notices must be retained in any copies of any part of this
  21. // documentation and/or software.
  22. // The original md5 implementation avoids external libraries.
  23. // This version has dependency on stdio.h for file input and
  24. // string.h for memcpy.
  25. #include <cstdio>
  26. #include <cstring>
  27. #include <iostream>
  28. namespace Limonp
  29. {
  30. //#pragma region MD5 defines
  31. // Constants for MD5Transform routine.
  32. #define S11 7
  33. #define S12 12
  34. #define S13 17
  35. #define S14 22
  36. #define S21 5
  37. #define S22 9
  38. #define S23 14
  39. #define S24 20
  40. #define S31 4
  41. #define S32 11
  42. #define S33 16
  43. #define S34 23
  44. #define S41 6
  45. #define S42 10
  46. #define S43 15
  47. #define S44 21
  48. // F, G, H and I are basic MD5 functions.
  49. #define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
  50. #define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
  51. #define H(x, y, z) ((x) ^ (y) ^ (z))
  52. #define I(x, y, z) ((y) ^ ((x) | (~z)))
  53. // ROTATE_LEFT rotates x left n bits.
  54. #define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
  55. // FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
  56. // Rotation is separate from addition to prevent recomputation.
  57. #define FF(a, b, c, d, x, s, ac) { \
  58. (a) += F ((b), (c), (d)) + (x) + (UINT4)(ac); \
  59. (a) = ROTATE_LEFT ((a), (s)); \
  60. (a) += (b); \
  61. }
  62. #define GG(a, b, c, d, x, s, ac) { \
  63. (a) += G ((b), (c), (d)) + (x) + (UINT4)(ac); \
  64. (a) = ROTATE_LEFT ((a), (s)); \
  65. (a) += (b); \
  66. }
  67. #define HH(a, b, c, d, x, s, ac) { \
  68. (a) += H ((b), (c), (d)) + (x) + (UINT4)(ac); \
  69. (a) = ROTATE_LEFT ((a), (s)); \
  70. (a) += (b); \
  71. }
  72. #define II(a, b, c, d, x, s, ac) { \
  73. (a) += I ((b), (c), (d)) + (x) + (UINT4)(ac); \
  74. (a) = ROTATE_LEFT ((a), (s)); \
  75. (a) += (b); \
  76. }
  77. //#pragma endregion
  78. typedef unsigned char BYTE ;
  79. // POINTER defines a generic pointer type
  80. typedef unsigned char *POINTER;
  81. // UINT2 defines a two byte word
  82. typedef unsigned short int UINT2;
  83. // UINT4 defines a four byte word
  84. typedef unsigned int UINT4;
  85. static unsigned char PADDING[64] = {
  86. 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  87. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  88. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
  89. };
  90. // convenient object that wraps
  91. // the C-functions for use in C++ only
  92. class MD5
  93. {
  94. private:
  95. struct __context_t {
  96. UINT4 state[4]; /* state (ABCD) */
  97. UINT4 count[2]; /* number of bits, modulo 2^64 (lsb first) */
  98. unsigned char buffer[64]; /* input buffer */
  99. } context ;
  100. //#pragma region static helper functions
  101. // The core of the MD5 algorithm is here.
  102. // MD5 basic transformation. Transforms state based on block.
  103. static void MD5Transform( UINT4 state[4], unsigned char block[64] )
  104. {
  105. UINT4 a = state[0], b = state[1], c = state[2], d = state[3], x[16];
  106. Decode (x, block, 64);
  107. /* Round 1 */
  108. FF (a, b, c, d, x[ 0], S11, 0xd76aa478); /* 1 */
  109. FF (d, a, b, c, x[ 1], S12, 0xe8c7b756); /* 2 */
  110. FF (c, d, a, b, x[ 2], S13, 0x242070db); /* 3 */
  111. FF (b, c, d, a, x[ 3], S14, 0xc1bdceee); /* 4 */
  112. FF (a, b, c, d, x[ 4], S11, 0xf57c0faf); /* 5 */
  113. FF (d, a, b, c, x[ 5], S12, 0x4787c62a); /* 6 */
  114. FF (c, d, a, b, x[ 6], S13, 0xa8304613); /* 7 */
  115. FF (b, c, d, a, x[ 7], S14, 0xfd469501); /* 8 */
  116. FF (a, b, c, d, x[ 8], S11, 0x698098d8); /* 9 */
  117. FF (d, a, b, c, x[ 9], S12, 0x8b44f7af); /* 10 */
  118. FF (c, d, a, b, x[10], S13, 0xffff5bb1); /* 11 */
  119. FF (b, c, d, a, x[11], S14, 0x895cd7be); /* 12 */
  120. FF (a, b, c, d, x[12], S11, 0x6b901122); /* 13 */
  121. FF (d, a, b, c, x[13], S12, 0xfd987193); /* 14 */
  122. FF (c, d, a, b, x[14], S13, 0xa679438e); /* 15 */
  123. FF (b, c, d, a, x[15], S14, 0x49b40821); /* 16 */
  124. /* Round 2 */
  125. GG (a, b, c, d, x[ 1], S21, 0xf61e2562); /* 17 */
  126. GG (d, a, b, c, x[ 6], S22, 0xc040b340); /* 18 */
  127. GG (c, d, a, b, x[11], S23, 0x265e5a51); /* 19 */
  128. GG (b, c, d, a, x[ 0], S24, 0xe9b6c7aa); /* 20 */
  129. GG (a, b, c, d, x[ 5], S21, 0xd62f105d); /* 21 */
  130. GG (d, a, b, c, x[10], S22, 0x2441453); /* 22 */
  131. GG (c, d, a, b, x[15], S23, 0xd8a1e681); /* 23 */
  132. GG (b, c, d, a, x[ 4], S24, 0xe7d3fbc8); /* 24 */
  133. GG (a, b, c, d, x[ 9], S21, 0x21e1cde6); /* 25 */
  134. GG (d, a, b, c, x[14], S22, 0xc33707d6); /* 26 */
  135. GG (c, d, a, b, x[ 3], S23, 0xf4d50d87); /* 27 */
  136. GG (b, c, d, a, x[ 8], S24, 0x455a14ed); /* 28 */
  137. GG (a, b, c, d, x[13], S21, 0xa9e3e905); /* 29 */
  138. GG (d, a, b, c, x[ 2], S22, 0xfcefa3f8); /* 30 */
  139. GG (c, d, a, b, x[ 7], S23, 0x676f02d9); /* 31 */
  140. GG (b, c, d, a, x[12], S24, 0x8d2a4c8a); /* 32 */
  141. /* Round 3 */
  142. HH (a, b, c, d, x[ 5], S31, 0xfffa3942); /* 33 */
  143. HH (d, a, b, c, x[ 8], S32, 0x8771f681); /* 34 */
  144. HH (c, d, a, b, x[11], S33, 0x6d9d6122); /* 35 */
  145. HH (b, c, d, a, x[14], S34, 0xfde5380c); /* 36 */
  146. HH (a, b, c, d, x[ 1], S31, 0xa4beea44); /* 37 */
  147. HH (d, a, b, c, x[ 4], S32, 0x4bdecfa9); /* 38 */
  148. HH (c, d, a, b, x[ 7], S33, 0xf6bb4b60); /* 39 */
  149. HH (b, c, d, a, x[10], S34, 0xbebfbc70); /* 40 */
  150. HH (a, b, c, d, x[13], S31, 0x289b7ec6); /* 41 */
  151. HH (d, a, b, c, x[ 0], S32, 0xeaa127fa); /* 42 */
  152. HH (c, d, a, b, x[ 3], S33, 0xd4ef3085); /* 43 */
  153. HH (b, c, d, a, x[ 6], S34, 0x4881d05); /* 44 */
  154. HH (a, b, c, d, x[ 9], S31, 0xd9d4d039); /* 45 */
  155. HH (d, a, b, c, x[12], S32, 0xe6db99e5); /* 46 */
  156. HH (c, d, a, b, x[15], S33, 0x1fa27cf8); /* 47 */
  157. HH (b, c, d, a, x[ 2], S34, 0xc4ac5665); /* 48 */
  158. /* Round 4 */
  159. II (a, b, c, d, x[ 0], S41, 0xf4292244); /* 49 */
  160. II (d, a, b, c, x[ 7], S42, 0x432aff97); /* 50 */
  161. II (c, d, a, b, x[14], S43, 0xab9423a7); /* 51 */
  162. II (b, c, d, a, x[ 5], S44, 0xfc93a039); /* 52 */
  163. II (a, b, c, d, x[12], S41, 0x655b59c3); /* 53 */
  164. II (d, a, b, c, x[ 3], S42, 0x8f0ccc92); /* 54 */
  165. II (c, d, a, b, x[10], S43, 0xffeff47d); /* 55 */
  166. II (b, c, d, a, x[ 1], S44, 0x85845dd1); /* 56 */
  167. II (a, b, c, d, x[ 8], S41, 0x6fa87e4f); /* 57 */
  168. II (d, a, b, c, x[15], S42, 0xfe2ce6e0); /* 58 */
  169. II (c, d, a, b, x[ 6], S43, 0xa3014314); /* 59 */
  170. II (b, c, d, a, x[13], S44, 0x4e0811a1); /* 60 */
  171. II (a, b, c, d, x[ 4], S41, 0xf7537e82); /* 61 */
  172. II (d, a, b, c, x[11], S42, 0xbd3af235); /* 62 */
  173. II (c, d, a, b, x[ 2], S43, 0x2ad7d2bb); /* 63 */
  174. II (b, c, d, a, x[ 9], S44, 0xeb86d391); /* 64 */
  175. state[0] += a;
  176. state[1] += b;
  177. state[2] += c;
  178. state[3] += d;
  179. // Zeroize sensitive information.
  180. memset((POINTER)x, 0, sizeof (x));
  181. }
  182. // Encodes input (UINT4) into output (unsigned char). Assumes len is
  183. // a multiple of 4.
  184. static void Encode( unsigned char *output, UINT4 *input, unsigned int len )
  185. {
  186. unsigned int i, j;
  187. for (i = 0, j = 0; j < len; i++, j += 4) {
  188. output[j] = (unsigned char)(input[i] & 0xff);
  189. output[j+1] = (unsigned char)((input[i] >> 8) & 0xff);
  190. output[j+2] = (unsigned char)((input[i] >> 16) & 0xff);
  191. output[j+3] = (unsigned char)((input[i] >> 24) & 0xff);
  192. }
  193. }
  194. // Decodes input (unsigned char) into output (UINT4). Assumes len is
  195. // a multiple of 4.
  196. static void Decode( UINT4 *output, unsigned char *input, unsigned int len )
  197. {
  198. unsigned int i, j;
  199. for (i = 0, j = 0; j < len; i++, j += 4)
  200. output[i] = ((UINT4)input[j]) | (((UINT4)input[j+1]) << 8) |
  201. (((UINT4)input[j+2]) << 16) | (((UINT4)input[j+3]) << 24);
  202. }
  203. //#pragma endregion
  204. public:
  205. // MAIN FUNCTIONS
  206. MD5()
  207. {
  208. Init() ;
  209. }
  210. // MD5 initialization. Begins an MD5 operation, writing a new context.
  211. void Init()
  212. {
  213. context.count[0] = context.count[1] = 0;
  214. // Load magic initialization constants.
  215. context.state[0] = 0x67452301;
  216. context.state[1] = 0xefcdab89;
  217. context.state[2] = 0x98badcfe;
  218. context.state[3] = 0x10325476;
  219. }
  220. // MD5 block update operation. Continues an MD5 message-digest
  221. // operation, processing another message block, and updating the
  222. // context.
  223. void Update(
  224. unsigned char *input, // input block
  225. unsigned int inputLen ) // length of input block
  226. {
  227. unsigned int i, index, partLen;
  228. // Compute number of bytes mod 64
  229. index = (unsigned int)((context.count[0] >> 3) & 0x3F);
  230. // Update number of bits
  231. if ((context.count[0] += ((UINT4)inputLen << 3))
  232. < ((UINT4)inputLen << 3))
  233. context.count[1]++;
  234. context.count[1] += ((UINT4)inputLen >> 29);
  235. partLen = 64 - index;
  236. // Transform as many times as possible.
  237. if (inputLen >= partLen) {
  238. memcpy((POINTER)&context.buffer[index], (POINTER)input, partLen);
  239. MD5Transform (context.state, context.buffer);
  240. for (i = partLen; i + 63 < inputLen; i += 64)
  241. MD5Transform (context.state, &input[i]);
  242. index = 0;
  243. }
  244. else
  245. i = 0;
  246. /* Buffer remaining input */
  247. memcpy((POINTER)&context.buffer[index], (POINTER)&input[i], inputLen-i);
  248. }
  249. // MD5 finalization. Ends an MD5 message-digest operation, writing the
  250. // the message digest and zeroizing the context.
  251. // Writes to digestRaw
  252. void Final()
  253. {
  254. unsigned char bits[8];
  255. unsigned int index, padLen;
  256. // Save number of bits
  257. Encode( bits, context.count, 8 );
  258. // Pad out to 56 mod 64.
  259. index = (unsigned int)((context.count[0] >> 3) & 0x3f);
  260. padLen = (index < 56) ? (56 - index) : (120 - index);
  261. Update( PADDING, padLen );
  262. // Append length (before padding)
  263. Update( bits, 8 );
  264. // Store state in digest
  265. Encode( digestRaw, context.state, 16);
  266. // Zeroize sensitive information.
  267. memset((POINTER)&context, 0, sizeof (context));
  268. writeToString() ;
  269. }
  270. /// Buffer must be 32+1 (nul) = 33 chars long at least
  271. void writeToString()
  272. {
  273. int pos ;
  274. for( pos = 0 ; pos < 16 ; pos++ )
  275. sprintf( digestChars+(pos*2), "%02x", digestRaw[pos] ) ;
  276. }
  277. public:
  278. // an MD5 digest is a 16-byte number (32 hex digits)
  279. BYTE digestRaw[ 16 ] ;
  280. // This version of the digest is actually
  281. // a "printf'd" version of the digest.
  282. char digestChars[ 33 ] ;
  283. /// Load a file from disk and digest it
  284. // Digests a file and returns the result.
  285. const char* digestFile( const char *filename )
  286. {
  287. if (NULL == filename || strcmp(filename, "") == 0)
  288. return NULL;
  289. Init() ;
  290. FILE *file;
  291. unsigned char buffer[1024] ;
  292. if((file = fopen (filename, "rb")) == NULL)
  293. {
  294. return NULL;
  295. }
  296. int len;
  297. while( (len = fread( buffer, 1, 1024, file )) )
  298. Update( buffer, len ) ;
  299. Final();
  300. fclose( file );
  301. return digestChars ;
  302. }
  303. /// Digests a byte-array already in memory
  304. const char* digestMemory( BYTE *memchunk, int len )
  305. {
  306. if (NULL == memchunk)
  307. return NULL;
  308. Init() ;
  309. Update( memchunk, len ) ;
  310. Final() ;
  311. return digestChars ;
  312. }
  313. // Digests a string and prints the result.
  314. const char* digestString(const char *string )
  315. {
  316. if (string == NULL)
  317. return NULL;
  318. Init() ;
  319. Update( (unsigned char*)string, strlen(string) ) ;
  320. Final() ;
  321. return digestChars ;
  322. }
  323. };
  324. inline bool md5String(const char* str, std::string& res)
  325. {
  326. if (NULL == str)
  327. {
  328. res = "";
  329. return false;
  330. }
  331. MD5 md5;
  332. const char *pRes = md5.digestString(str);
  333. if (NULL == pRes)
  334. {
  335. res = "";
  336. return false;
  337. }
  338. res = pRes;
  339. return true;
  340. }
  341. inline bool md5File(const char* filepath, std::string& res)
  342. {
  343. if (NULL == filepath || strcmp(filepath, "") == 0)
  344. {
  345. res = "";
  346. return false;
  347. }
  348. MD5 md5;
  349. const char *pRes = md5.digestFile(filepath);
  350. if (NULL == pRes)
  351. {
  352. res = "";
  353. return false;
  354. }
  355. res = pRes;
  356. return true;
  357. }
  358. }
  359. #endif