歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> MD5的C++源碼

MD5的C++源碼

日期:2017/3/1 10:46:32   编辑:Linux編程

關於MD5的C源碼,請參考另外一篇文章:MD5的C源碼 http://www.linuxidc.com/Linux/2011-12/49783.htm

md5.h:

  1. #ifndef MD5_H
  2. #define MD5_H
  3. #include <string>
  4. #include <fstream>
  5. /* Type define */
  6. typedef unsigned char byte;
  7. typedef unsigned int uint32;
  8. using std::string;
  9. using std::ifstream;
  10. /* MD5 declaration. */
  11. class MD5 {
  12. public:
  13. MD5();
  14. MD5(const void *input, size_t length);
  15. MD5(const string &str);
  16. MD5(ifstream &in);
  17. void update(const void *input, size_t length);
  18. void update(const string &str);
  19. void update(ifstream &in);
  20. const byte* digest();
  21. string toString();
  22. void reset();
  23. private:
  24. void update(const byte *input, size_t length);
  25. void final();
  26. void transform(const byte block[64]);
  27. void encode(const uint32 *input, byte *output, size_t length);
  28. void decode(const byte *input, uint32 *output, size_t length);
  29. string bytesToHexString(const byte *input, size_t length);
  30. /* class uncopyable */
  31. MD5(const MD5&);
  32. MD5& operator=(const MD5&);
  33. private:
  34. uint32 _state[4]; /* state (ABCD) */
  35. uint32 _count[2]; /* number of bits, modulo 2^64 (low-order word first) */
  36. byte _buffer[64]; /* input buffer */
  37. byte _digest[16]; /* message digest */
  38. bool _finished; /* calculate finished ? */
  39. static const byte PADDING[64]; /* padding for calculate */
  40. static const char HEX[16];
  41. static const size_t BUFFER_SIZE = 1024;
  42. };
  43. #endif/*MD5_H*/
md5.cpp:
  1. #include "md5.h"
  2. using namespace std;
  3. /* Constants for MD5Transform routine. */
  4. #define S11 7
  5. #define S12 12
  6. #define S13 17
  7. #define S14 22
  8. #define S21 5
  9. #define S22 9
  10. #define S23 14
  11. #define S24 20
  12. #define S31 4
  13. #define S32 11
  14. #define S33 16
  15. #define S34 23
  16. #define S41 6
  17. #define S42 10
  18. #define S43 15
  19. #define S44 21
  20. /* F, G, H and I are basic MD5 functions.
  21. */
  22. #define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
  23. #define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
  24. #define H(x, y, z) ((x) ^ (y) ^ (z))
  25. #define I(x, y, z) ((y) ^ ((x) | (~z)))
  26. /* ROTATE_LEFT rotates x left n bits.
  27. */
  28. #define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
  29. /* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
  30. Rotation is separate from addition to prevent recomputation.
  31. */
  32. #define FF(a, b, c, d, x, s, ac) { \
  33. (a) += F ((b), (c), (d)) + (x) + ac; \
  34. (a) = ROTATE_LEFT ((a), (s)); \
  35. (a) += (b); \
  36. }
  37. #define GG(a, b, c, d, x, s, ac) { \
  38. (a) += G ((b), (c), (d)) + (x) + ac; \
  39. (a) = ROTATE_LEFT ((a), (s)); \
  40. (a) += (b); \
  41. }
  42. #define HH(a, b, c, d, x, s, ac) { \
  43. (a) += H ((b), (c), (d)) + (x) + ac; \
  44. (a) = ROTATE_LEFT ((a), (s)); \
  45. (a) += (b); \
  46. }
  47. #define II(a, b, c, d, x, s, ac) { \
  48. (a) += I ((b), (c), (d)) + (x) + ac; \
  49. (a) = ROTATE_LEFT ((a), (s)); \
  50. (a) += (b); \
  51. }
  52. const byte MD5::PADDING[64] = { 0x80 };
  53. const char MD5::HEX[16] = {
  54. '0', '1', '2', '3',
  55. '4', '5', '6', '7',
  56. '8', '9', 'a', 'b',
  57. 'c', 'd', 'e', 'f'
  58. };
  59. /* Default construct. */
  60. MD5::MD5() {
  61. reset();
  62. }
  63. /* Construct a MD5 object with a input buffer. */
  64. MD5::MD5(const void *input, size_t length) {
  65. reset();
  66. update(input, length);
  67. }
  68. /* Construct a MD5 object with a string. */
  69. MD5::MD5(const string &str) {
  70. reset();
  71. update(str);
  72. }
  73. /* Construct a MD5 object with a file. */
  74. MD5::MD5(ifstream &in) {
  75. reset();
  76. update(in);
  77. }
  78. /* Return the message-digest */
  79. const byte* MD5::digest() {
  80. if (!_finished) {
  81. _finished = true;
  82. final();
  83. }
  84. return _digest;
  85. }
  86. /* Reset the calculate state */
  87. void MD5::reset() {
  88. _finished = false;
  89. /* reset number of bits. */
  90. _count[0] = _count[1] = 0;
  91. /* Load magic initialization constants. */
  92. _state[0] = 0x67452301;
  93. _state[1] = 0xefcdab89;
  94. _state[2] = 0x98badcfe;
  95. _state[3] = 0x10325476;
  96. }
  97. /* Updating the context with a input buffer. */
  98. void MD5::update(const void *input, size_t length) {
  99. update((const byte*)input, length);
  100. }
  101. /* Updating the context with a string. */
  102. void MD5::update(const string &str) {
  103. update((const byte*)str.c_str(), str.length());
  104. }
  105. /* Updating the context with a file. */
  106. void MD5::update(ifstream &in) {
  107. if (!in)
  108. return;
  109. std::streamsize length;
  110. char buffer[BUFFER_SIZE];
  111. while (!in.eof()) {
  112. in.read(buffer, BUFFER_SIZE);
  113. length = in.gcount();
  114. if (length > 0)
  115. update(buffer, length);
  116. }
  117. in.close();
  118. }
  119. /* MD5 block update operation. Continues an MD5 message-digest
  120. operation, processing another message block, and updating the
  121. context.
  122. */
  123. void MD5::update(const byte *input, size_t length) {
  124. uint32 i, index, partLen;
  125. _finished = false;
  126. /* Compute number of bytes mod 64 */
  127. index = (uint32)((_count[0] >> 3) & 0x3f);
  128. /* update number of bits */
  129. if((_count[0] += ((uint32)length << 3)) < ((uint32)length << 3))
  130. _count[1]++;
  131. _count[1] += ((uint32)length >> 29);
  132. partLen = 64 - index;
  133. /* transform as many times as possible. */
  134. if(length >= partLen) {
  135. memcpy(&_buffer[index], input, partLen);
  136. transform(_buffer);
  137. for (i = partLen; i + 63 < length; i += 64)
  138. transform(&input[i]);
  139. index = 0;
  140. } else {
  141. i = 0;
  142. }
  143. /* Buffer remaining input */
  144. memcpy(&_buffer[index], &input[i], length-i);
  145. }
  146. /* MD5 finalization. Ends an MD5 message-_digest operation, writing the
  147. the message _digest and zeroizing the context.
  148. */
  149. void MD5::final() {
  150. byte bits[8];
  151. uint32 oldState[4];
  152. uint32 oldCount[2];
  153. uint32 index, padLen;
  154. /* Save current state and count. */
  155. memcpy(oldState, _state, 16);
  156. memcpy(oldCount, _count, 8);
  157. /* Save number of bits */
  158. encode(_count, bits, 8);
  159. /* Pad out to 56 mod 64. */
  160. index = (uint32)((_count[0] >> 3) & 0x3f);
  161. padLen = (index < 56) ? (56 - index) : (120 - index);
  162. update(PADDING, padLen);
  163. /* Append length (before padding) */
  164. update(bits, 8);
  165. /* Store state in digest */
  166. encode(_state, _digest, 16);
  167. /* Restore current state and count. */
  168. memcpy(_state, oldState, 16);
  169. memcpy(_count, oldCount, 8);
  170. }
  171. /* MD5 basic transformation. Transforms _state based on block. */
  172. void MD5::transform(const byte block[64]) {
  173. uint32 a = _state[0], b = _state[1], c = _state[2], d = _state[3], x[16];
  174. decode(block, x, 64);
  175. /* Round 1 */
  176. FF (a, b, c, d, x[ 0], S11, 0xd76aa478); /* 1 */
  177. FF (d, a, b, c, x[ 1], S12, 0xe8c7b756); /* 2 */
  178. FF (c, d, a, b, x[ 2], S13, 0x242070db); /* 3 */
  179. FF (b, c, d, a, x[ 3], S14, 0xc1bdceee); /* 4 */
  180. FF (a, b, c, d, x[ 4], S11, 0xf57c0faf); /* 5 */
  181. FF (d, a, b, c, x[ 5], S12, 0x4787c62a); /* 6 */
  182. FF (c, d, a, b, x[ 6], S13, 0xa8304613); /* 7 */
  183. FF (b, c, d, a, x[ 7], S14, 0xfd469501); /* 8 */
  184. FF (a, b, c, d, x[ 8], S11, 0x698098d8); /* 9 */
  185. FF (d, a, b, c, x[ 9], S12, 0x8b44f7af); /* 10 */
  186. FF (c, d, a, b, x[10], S13, 0xffff5bb1); /* 11 */
  187. FF (b, c, d, a, x[11], S14, 0x895cd7be); /* 12 */
  188. FF (a, b, c, d, x[12], S11, 0x6b901122); /* 13 */
  189. FF (d, a, b, c, x[13], S12, 0xfd987193); /* 14 */
  190. FF (c, d, a, b, x[14], S13, 0xa679438e); /* 15 */
  191. FF (b, c, d, a, x[15], S14, 0x49b40821); /* 16 */
  192. /* Round 2 */
  193. GG (a, b, c, d, x[ 1], S21, 0xf61e2562); /* 17 */
  194. GG (d, a, b, c, x[ 6], S22, 0xc040b340); /* 18 */
  195. GG (c, d, a, b, x[11], S23, 0x265e5a51); /* 19 */
  196. GG (b, c, d, a, x[ 0], S24, 0xe9b6c7aa); /* 20 */
  197. GG (a, b, c, d, x[ 5], S21, 0xd62f105d); /* 21 */
  198. GG (d, a, b, c, x[10], S22, 0x2441453); /* 22 */
  199. GG (c, d, a, b, x[15], S23, 0xd8a1e681); /* 23 */
  200. GG (b, c, d, a, x[ 4], S24, 0xe7d3fbc8); /* 24 */
  201. GG (a, b, c, d, x[ 9], S21, 0x21e1cde6); /* 25 */
  202. GG (d, a, b, c, x[14], S22, 0xc33707d6); /* 26 */
  203. GG (c, d, a, b, x[ 3], S23, 0xf4d50d87); /* 27 */
  204. GG (b, c, d, a, x[ 8], S24, 0x455a14ed); /* 28 */
  205. GG (a, b, c, d, x[13], S21, 0xa9e3e905); /* 29 */
  206. GG (d, a, b, c, x[ 2], S22, 0xfcefa3f8); /* 30 */
  207. GG (c, d, a, b, x[ 7], S23, 0x676f02d9); /* 31 */
  208. GG (b, c, d, a, x[12], S24, 0x8d2a4c8a); /* 32 */
  209. /* Round 3 */
  210. HH (a, b, c, d, x[ 5], S31, 0xfffa3942); /* 33 */
  211. HH (d, a, b, c, x[ 8], S32, 0x8771f681); /* 34 */
  212. HH (c, d, a, b, x[11], S33, 0x6d9d6122); /* 35 */
  213. HH (b, c, d, a, x[14], S34, 0xfde5380c); /* 36 */
  214. HH (a, b, c, d, x[ 1], S31, 0xa4beea44); /* 37 */
  215. HH (d, a, b, c, x[ 4], S32, 0x4bdecfa9); /* 38 */
  216. HH (c, d, a, b, x[ 7], S33, 0xf6bb4b60); /* 39 */
  217. HH (b, c, d, a, x[10], S34, 0xbebfbc70); /* 40 */
  218. HH (a, b, c, d, x[13], S31, 0x289b7ec6); /* 41 */
  219. HH (d, a, b, c, x[ 0], S32, 0xeaa127fa); /* 42 */
  220. HH (c, d, a, b, x[ 3], S33, 0xd4ef3085); /* 43 */
  221. HH (b, c, d, a, x[ 6], S34, 0x4881d05); /* 44 */
  222. HH (a, b, c, d, x[ 9], S31, 0xd9d4d039); /* 45 */
  223. HH (d, a, b, c, x[12], S32, 0xe6db99e5); /* 46 */
  224. HH (c, d, a, b, x[15], S33, 0x1fa27cf8); /* 47 */
  225. HH (b, c, d, a, x[ 2], S34, 0xc4ac5665); /* 48 */
  226. /* Round 4 */
  227. II (a, b, c, d, x[ 0], S41, 0xf4292244); /* 49 */
  228. II (d, a, b, c, x[ 7], S42, 0x432aff97); /* 50 */
  229. II (c, d, a, b, x[14], S43, 0xab9423a7); /* 51 */
  230. II (b, c, d, a, x[ 5], S44, 0xfc93a039); /* 52 */
  231. II (a, b, c, d, x[12], S41, 0x655b59c3); /* 53 */
  232. II (d, a, b, c, x[ 3], S42, 0x8f0ccc92); /* 54 */
  233. II (c, d, a, b, x[10], S43, 0xffeff47d); /* 55 */
  234. II (b, c, d, a, x[ 1], S44, 0x85845dd1); /* 56 */
  235. II (a, b, c, d, x[ 8], S41, 0x6fa87e4f); /* 57 */
  236. II (d, a, b, c, x[15], S42, 0xfe2ce6e0); /* 58 */
  237. II (c, d, a, b, x[ 6], S43, 0xa3014314); /* 59 */
  238. II (b, c, d, a, x[13], S44, 0x4e0811a1); /* 60 */
  239. II (a, b, c, d, x[ 4], S41, 0xf7537e82); /* 61 */
  240. II (d, a, b, c, x[11], S42, 0xbd3af235); /* 62 */
  241. II (c, d, a, b, x[ 2], S43, 0x2ad7d2bb); /* 63 */
  242. II (b, c, d, a, x[ 9], S44, 0xeb86d391); /* 64 */
  243. _state[0] += a;
  244. _state[1] += b;
  245. _state[2] += c;
  246. _state[3] += d;
  247. }
  248. /* Encodes input (ulong) into output (byte). Assumes length is
  249. a multiple of 4.
  250. */
  251. void MD5::encode(const uint32 *input, byte *output, size_t length) {
  252. for(size_t i=0, j=0; j<length; i++, j+=4) {
  253. output[j]= (byte)(input[i] & 0xff);
  254. output[j+1] = (byte)((input[i] >> 8) & 0xff);
  255. output[j+2] = (byte)((input[i] >> 16) & 0xff);
  256. output[j+3] = (byte)((input[i] >> 24) & 0xff);
  257. }
  258. }
  259. /* Decodes input (byte) into output (ulong). Assumes length is
  260. a multiple of 4.
  261. */
  262. void MD5::decode(const byte *input, uint32 *output, size_t length) {
  263. for(size_t i=0, j=0; j<length; i++, j+=4) {
  264. output[i] = ((uint32)input[j]) | (((uint32)input[j+1]) << 8) |
  265. (((uint32)input[j+2]) << 16) | (((uint32)input[j+3]) << 24);
  266. }
  267. }
  268. /* Convert byte array to hex string. */
  269. string MD5::bytesToHexString(const byte *input, size_t length) {
  270. string str;
  271. str.reserve(length << 1);
  272. for(size_t i = 0; i < length; i++) {
  273. int t = input[i];
  274. int a = t / 16;
  275. int b = t % 16;
  276. str.append(1, HEX[a]);
  277. str.append(1, HEX[b]);
  278. }
  279. return str;
  280. }
  281. /* Convert digest to string value */
  282. string MD5::toString() {
  283. return bytesToHexString(digest(), 16);
  284. }
Copyright © Linux教程網 All Rights Reserved