/std/md5.d

http://github.com/jcd/phobos · D · 493 lines · 280 code · 64 blank · 149 comment · 14 complexity · 4b14071076a7ed3f28f22aed0fd927a2 MD5 · raw file

  1. // Written in the D programming language.
  2. /* md5.d - RSA Data Security, Inc., MD5 message-digest algorithm
  3. * Derived from the RSA Data Security, Inc. MD5 Message-Digest Algorithm.
  4. */
  5. /**
  6. * Computes MD5 digests of arbitrary data. MD5 digests are 16 byte quantities that are like a checksum or crc, but are more robust.
  7. *
  8. * There are two ways to do this. The first does it all in one function call to
  9. * sum(). The second is for when the data is buffered.
  10. *
  11. * Bugs:
  12. * MD5 digests have been demonstrated to not be unique.
  13. *
  14. * Author:
  15. * The routines and algorithms are derived from the
  16. * $(I RSA Data Security, Inc. MD5 Message-Digest Algorithm).
  17. *
  18. * References:
  19. * $(LINK2 http://en.wikipedia.org/wiki/Md5, Wikipedia on MD5)
  20. *
  21. * Source: $(PHOBOSSRC std/_md5.d)
  22. *
  23. * Macros:
  24. * WIKI = Phobos/StdMd5
  25. */
  26. /++++++++++++++++++++++++++++++++
  27. Example:
  28. --------------------
  29. // This code is derived from the
  30. // RSA Data Security, Inc. MD5 Message-Digest Algorithm.
  31. import std.md5;
  32. import std.stdio;
  33. void main(string[] args)
  34. {
  35. foreach (arg; args)
  36. mdFile(arg);
  37. }
  38. /// Digests a file and prints the result.
  39. void mdFile(string filename)
  40. {
  41. ubyte[16] digest;
  42. MD5_CTX context;
  43. context.start();
  44. foreach (buffer; File(filename).byChunk(4096 * 1024))
  45. context.update(buffer);
  46. context.finish(digest);
  47. writefln("MD5 (%s) = %s", filename, digestToString(digest));
  48. }
  49. --------------------
  50. +/
  51. /* Copyright (C) 1991-2, RSA Data Security, Inc. Created 1991. All
  52. rights reserved.
  53. License to copy and use this software is granted provided that it
  54. is identified as the "RSA Data Security, Inc. MD5 Message-Digest
  55. Algorithm" in all material mentioning or referencing this software
  56. or this function.
  57. License is also granted to make and use derivative works provided
  58. that such works are identified as "derived from the RSA Data
  59. Security, Inc. MD5 Message-Digest Algorithm" in all material
  60. mentioning or referencing the derived work.
  61. RSA Data Security, Inc. makes no representations concerning either
  62. the merchantability of this software or the suitability of this
  63. software for any particular purpose. It is provided "as is"
  64. without express or implied warranty of any kind.
  65. These notices must be retained in any copies of any part of this
  66. documentation and/or software.
  67. */
  68. module std.md5;
  69. //debug=md5; // uncomment to turn on debugging printf's
  70. import std.ascii;
  71. import std.string;
  72. import std.exception;
  73. debug(md5) import std.c.stdio : printf;
  74. /***************************************
  75. * Computes MD5 digest of several arrays of data.
  76. */
  77. void sum(ref ubyte[16] digest, in void[][] data...)
  78. {
  79. MD5_CTX context;
  80. context.start();
  81. foreach (datum; data)
  82. {
  83. context.update(datum);
  84. }
  85. context.finish(digest);
  86. }
  87. // /******************
  88. // * Prints a message digest in hexadecimal to stdout.
  89. // */
  90. // void printDigest(const ubyte digest[16])
  91. // {
  92. // foreach (ubyte u; digest)
  93. // printf("%02x", u);
  94. // }
  95. /****************************************
  96. * Converts MD5 digest to a string.
  97. */
  98. string digestToString(in ubyte[16] digest)
  99. {
  100. auto result = new char[32];
  101. int i;
  102. foreach (ubyte u; digest)
  103. {
  104. result[i] = std.ascii.hexDigits[u >> 4];
  105. result[i + 1] = std.ascii.hexDigits[u & 15];
  106. i += 2;
  107. }
  108. return assumeUnique(result);
  109. }
  110. /**
  111. Gets the digest of all $(D data) items passed in.
  112. Example:
  113. ----
  114. string a = "Mary has ", b = "a little lamb";
  115. int[] c = [ 1, 2, 3, 4, 5 ];
  116. string d = getDigestString(a, b, c);
  117. ----
  118. */
  119. string getDigestString(in void[][] data...)
  120. {
  121. MD5_CTX ctx;
  122. ctx.start();
  123. foreach (datum; data) {
  124. ctx.update(datum);
  125. }
  126. ubyte[16] digest;
  127. ctx.finish(digest);
  128. return digestToString(digest);
  129. }
  130. version(unittest) import std.stdio;
  131. unittest
  132. {
  133. string a = "Mary has ", b = "a little lamb";
  134. int[] c = [ 1, 2, 3, 4, 5 ];
  135. string d = getDigestString(a, b, c);
  136. assert(d == "F36625A66B2A8D9F47270C00C8BEFD2F", d);
  137. }
  138. /**
  139. * Holds context of MD5 computation.
  140. *
  141. * Used when data to be digested is buffered.
  142. */
  143. struct MD5_CTX
  144. {
  145. uint state[4] = /* state (ABCD) */
  146. /* magic initialization constants */
  147. [0x67452301,0xefcdab89,0x98badcfe,0x10325476];
  148. ulong count; /* number of bits, modulo 2^64 */
  149. ubyte buffer[64]; /* input buffer */
  150. static ubyte[64] PADDING =
  151. [
  152. 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  153. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  154. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
  155. ];
  156. /* F, G, H and I are basic MD5 functions.
  157. */
  158. private static
  159. {
  160. uint F(uint x, uint y, uint z) { return (x & y) | (~x & z); }
  161. uint G(uint x, uint y, uint z) { return (x & z) | (y & ~z); }
  162. uint H(uint x, uint y, uint z) { return x ^ y ^ z; }
  163. uint I(uint x, uint y, uint z) { return y ^ (x | ~z); }
  164. }
  165. /* ROTATE_LEFT rotates x left n bits.
  166. */
  167. static uint ROTATE_LEFT(uint x, uint n)
  168. {
  169. // With recently added optimization to DMD (commit 32ea0206 at 07/28/11), this is translated to rol.
  170. // No assembler required.
  171. return (x << n) | (x >> (32-n));
  172. }
  173. /* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
  174. * Rotation is separate from addition to prevent recomputation.
  175. */
  176. static void FF(ref uint a, uint b, uint c, uint d, uint x, uint s, uint ac)
  177. {
  178. a += F (b, c, d) + x + cast(uint)(ac);
  179. a = ROTATE_LEFT (a, s);
  180. a += b;
  181. }
  182. static void GG(ref uint a, uint b, uint c, uint d, uint x, uint s, uint ac)
  183. {
  184. a += G (b, c, d) + x + cast(uint)(ac);
  185. a = ROTATE_LEFT (a, s);
  186. a += b;
  187. }
  188. static void HH(ref uint a, uint b, uint c, uint d, uint x, uint s, uint ac)
  189. {
  190. a += H (b, c, d) + x + cast(uint)(ac);
  191. a = ROTATE_LEFT (a, s);
  192. a += b;
  193. }
  194. static void II(ref uint a, uint b, uint c, uint d, uint x, uint s, uint ac)
  195. {
  196. a += I (b, c, d) + x + cast(uint)(ac);
  197. a = ROTATE_LEFT (a, s);
  198. a += b;
  199. }
  200. /**
  201. * MD5 initialization. Begins an MD5 operation, writing a new context.
  202. */
  203. void start()
  204. {
  205. this = MD5_CTX.init;
  206. }
  207. /** MD5 block update operation. Continues an MD5 message-digest
  208. operation, processing another message block, and updating the
  209. context.
  210. */
  211. void update(const void[] input)
  212. {
  213. uint i, index, partLen;
  214. auto inputLen = input.length;
  215. /* Compute number of bytes mod 64 */
  216. index = (cast(uint)count >> 3) & (64 - 1);
  217. /* Update number of bits */
  218. count += inputLen * 8;
  219. partLen = 64 - index;
  220. /* Transform as many times as possible. */
  221. if (inputLen >= partLen)
  222. {
  223. std.c.string.memcpy(&buffer[index], input.ptr, partLen);
  224. transform (buffer.ptr);
  225. for (i = partLen; i + 63 < inputLen; i += 64)
  226. transform ((cast(ubyte[])input)[i .. i + 64].ptr);
  227. index = 0;
  228. }
  229. else
  230. i = 0;
  231. /* Buffer remaining input */
  232. if (inputLen - i)
  233. std.c.string.memcpy(&buffer[index], &input[i], inputLen-i);
  234. }
  235. /** MD5 finalization. Ends an MD5 message-digest operation, writing the
  236. * the message to digest and zeroing the context.
  237. */
  238. void finish(ref ubyte[16] digest) /* message digest */
  239. {
  240. ubyte bits[8] = void;
  241. uint index, padLen;
  242. /* Save number of bits */
  243. Encode (bits.ptr, cast(const uint*) &count, 8);
  244. /* Pad out to 56 mod 64. */
  245. index = (cast(uint)count >> 3) & (64 - 1);
  246. padLen = (index < 56) ? (56 - index) : (120 - index);
  247. update (PADDING[0 .. padLen]);
  248. /* Append length (before padding) */
  249. update (bits);
  250. /* Store state in digest */
  251. Encode (digest.ptr, state.ptr, 16);
  252. /* Zeroize sensitive information. */
  253. std.c.string.memset (&this, 0, MD5_CTX.sizeof);
  254. }
  255. /* MD5 basic transformation. Transforms state based on block.
  256. */
  257. /* Constants for MD5Transform routine. */
  258. enum
  259. {
  260. S11 = 7,
  261. S12 = 12,
  262. S13 = 17,
  263. S14 = 22,
  264. S21 = 5,
  265. S22 = 9,
  266. S23 = 14,
  267. S24 = 20,
  268. S31 = 4,
  269. S32 = 11,
  270. S33 = 16,
  271. S34 = 23,
  272. S41 = 6,
  273. S42 = 10,
  274. S43 = 15,
  275. S44 = 21,
  276. }
  277. private void transform (const ubyte* /*[64]*/ block)
  278. {
  279. uint a = state[0],
  280. b = state[1],
  281. c = state[2],
  282. d = state[3];
  283. uint[16] x = void;
  284. Decode (x.ptr, block, 64);
  285. /* Round 1 */
  286. FF (a, b, c, d, x[ 0], S11, 0xd76aa478); /* 1 */
  287. FF (d, a, b, c, x[ 1], S12, 0xe8c7b756); /* 2 */
  288. FF (c, d, a, b, x[ 2], S13, 0x242070db); /* 3 */
  289. FF (b, c, d, a, x[ 3], S14, 0xc1bdceee); /* 4 */
  290. FF (a, b, c, d, x[ 4], S11, 0xf57c0faf); /* 5 */
  291. FF (d, a, b, c, x[ 5], S12, 0x4787c62a); /* 6 */
  292. FF (c, d, a, b, x[ 6], S13, 0xa8304613); /* 7 */
  293. FF (b, c, d, a, x[ 7], S14, 0xfd469501); /* 8 */
  294. FF (a, b, c, d, x[ 8], S11, 0x698098d8); /* 9 */
  295. FF (d, a, b, c, x[ 9], S12, 0x8b44f7af); /* 10 */
  296. FF (c, d, a, b, x[10], S13, 0xffff5bb1); /* 11 */
  297. FF (b, c, d, a, x[11], S14, 0x895cd7be); /* 12 */
  298. FF (a, b, c, d, x[12], S11, 0x6b901122); /* 13 */
  299. FF (d, a, b, c, x[13], S12, 0xfd987193); /* 14 */
  300. FF (c, d, a, b, x[14], S13, 0xa679438e); /* 15 */
  301. FF (b, c, d, a, x[15], S14, 0x49b40821); /* 16 */
  302. /* Round 2 */
  303. GG (a, b, c, d, x[ 1], S21, 0xf61e2562); /* 17 */
  304. GG (d, a, b, c, x[ 6], S22, 0xc040b340); /* 18 */
  305. GG (c, d, a, b, x[11], S23, 0x265e5a51); /* 19 */
  306. GG (b, c, d, a, x[ 0], S24, 0xe9b6c7aa); /* 20 */
  307. GG (a, b, c, d, x[ 5], S21, 0xd62f105d); /* 21 */
  308. GG (d, a, b, c, x[10], S22, 0x2441453); /* 22 */
  309. GG (c, d, a, b, x[15], S23, 0xd8a1e681); /* 23 */
  310. GG (b, c, d, a, x[ 4], S24, 0xe7d3fbc8); /* 24 */
  311. GG (a, b, c, d, x[ 9], S21, 0x21e1cde6); /* 25 */
  312. GG (d, a, b, c, x[14], S22, 0xc33707d6); /* 26 */
  313. GG (c, d, a, b, x[ 3], S23, 0xf4d50d87); /* 27 */
  314. GG (b, c, d, a, x[ 8], S24, 0x455a14ed); /* 28 */
  315. GG (a, b, c, d, x[13], S21, 0xa9e3e905); /* 29 */
  316. GG (d, a, b, c, x[ 2], S22, 0xfcefa3f8); /* 30 */
  317. GG (c, d, a, b, x[ 7], S23, 0x676f02d9); /* 31 */
  318. GG (b, c, d, a, x[12], S24, 0x8d2a4c8a); /* 32 */
  319. /* Round 3 */
  320. HH (a, b, c, d, x[ 5], S31, 0xfffa3942); /* 33 */
  321. HH (d, a, b, c, x[ 8], S32, 0x8771f681); /* 34 */
  322. HH (c, d, a, b, x[11], S33, 0x6d9d6122); /* 35 */
  323. HH (b, c, d, a, x[14], S34, 0xfde5380c); /* 36 */
  324. HH (a, b, c, d, x[ 1], S31, 0xa4beea44); /* 37 */
  325. HH (d, a, b, c, x[ 4], S32, 0x4bdecfa9); /* 38 */
  326. HH (c, d, a, b, x[ 7], S33, 0xf6bb4b60); /* 39 */
  327. HH (b, c, d, a, x[10], S34, 0xbebfbc70); /* 40 */
  328. HH (a, b, c, d, x[13], S31, 0x289b7ec6); /* 41 */
  329. HH (d, a, b, c, x[ 0], S32, 0xeaa127fa); /* 42 */
  330. HH (c, d, a, b, x[ 3], S33, 0xd4ef3085); /* 43 */
  331. HH (b, c, d, a, x[ 6], S34, 0x4881d05); /* 44 */
  332. HH (a, b, c, d, x[ 9], S31, 0xd9d4d039); /* 45 */
  333. HH (d, a, b, c, x[12], S32, 0xe6db99e5); /* 46 */
  334. HH (c, d, a, b, x[15], S33, 0x1fa27cf8); /* 47 */
  335. HH (b, c, d, a, x[ 2], S34, 0xc4ac5665); /* 48 */
  336. /* Round 4 */
  337. II (a, b, c, d, x[ 0], S41, 0xf4292244); /* 49 */
  338. II (d, a, b, c, x[ 7], S42, 0x432aff97); /* 50 */
  339. II (c, d, a, b, x[14], S43, 0xab9423a7); /* 51 */
  340. II (b, c, d, a, x[ 5], S44, 0xfc93a039); /* 52 */
  341. II (a, b, c, d, x[12], S41, 0x655b59c3); /* 53 */
  342. II (d, a, b, c, x[ 3], S42, 0x8f0ccc92); /* 54 */
  343. II (c, d, a, b, x[10], S43, 0xffeff47d); /* 55 */
  344. II (b, c, d, a, x[ 1], S44, 0x85845dd1); /* 56 */
  345. II (a, b, c, d, x[ 8], S41, 0x6fa87e4f); /* 57 */
  346. II (d, a, b, c, x[15], S42, 0xfe2ce6e0); /* 58 */
  347. II (c, d, a, b, x[ 6], S43, 0xa3014314); /* 59 */
  348. II (b, c, d, a, x[13], S44, 0x4e0811a1); /* 60 */
  349. II (a, b, c, d, x[ 4], S41, 0xf7537e82); /* 61 */
  350. II (d, a, b, c, x[11], S42, 0xbd3af235); /* 62 */
  351. II (c, d, a, b, x[ 2], S43, 0x2ad7d2bb); /* 63 */
  352. II (b, c, d, a, x[ 9], S44, 0xeb86d391); /* 64 */
  353. state[0] += a;
  354. state[1] += b;
  355. state[2] += c;
  356. state[3] += d;
  357. /* Zeroize sensitive information. */
  358. x[] = 0;
  359. }
  360. /* Encodes input (uint) into output (ubyte). Assumes len is
  361. a multiple of 4.
  362. */
  363. private static void Encode (ubyte *output, const uint *input, uint len)
  364. {
  365. version (BigEndian)
  366. {
  367. uint i, j;
  368. for (i = 0, j = 0; j < len; i++, j += 4)
  369. {
  370. *cast(uint *) &output[j] = core.bitop.bswap(input[i]);
  371. }
  372. }
  373. else
  374. {
  375. (cast(uint *)output)[0..len/4] = input[0..len/4];
  376. }
  377. }
  378. /* Decodes input (ubyte) into output (uint). Assumes len is
  379. a multiple of 4.
  380. */
  381. private static void Decode (uint *output, const ubyte *input, uint len)
  382. {
  383. version (BigEndian)
  384. {
  385. uint i, j;
  386. for (i = 0, j = 0; j < len; i++, j += 4)
  387. {
  388. output[i] = core.bitop.bswap(*cast(uint*)&input[j]);
  389. }
  390. }
  391. else
  392. {
  393. output[0..len/4] = (cast(const uint *)input)[0..len/4];
  394. }
  395. }
  396. }
  397. unittest
  398. {
  399. debug(md5) printf("std.md5.unittest\n");
  400. ubyte[16] digest;
  401. sum (digest, "");
  402. assert(digest == cast(ubyte[])x"d41d8cd98f00b204e9800998ecf8427e");
  403. sum (digest, "a");
  404. assert(digest == cast(ubyte[])x"0cc175b9c0f1b6a831c399e269772661");
  405. sum (digest, "abc");
  406. assert(digest == cast(ubyte[])x"900150983cd24fb0d6963f7d28e17f72");
  407. sum (digest, "message digest");
  408. assert(digest == cast(ubyte[])x"f96b697d7cb7938d525a2f31aaf161d0");
  409. sum (digest, "abcdefghijklmnopqrstuvwxyz");
  410. assert(digest == cast(ubyte[])x"c3fcd3d76192e4007dfb496cca67e13b");
  411. sum (digest, "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789");
  412. assert(digest == cast(ubyte[])x"d174ab98d277d9f5a5611c2c9f419d9f");
  413. sum (digest,
  414. "1234567890123456789012345678901234567890"
  415. "1234567890123456789012345678901234567890");
  416. assert(digest == cast(ubyte[])x"57edf4a22be3c955ac49da2e2107b67a");
  417. assert(digestToString(cast(ubyte[16])x"c3fcd3d76192e4007dfb496cca67e13b")
  418. == "C3FCD3D76192E4007DFB496CCA67E13B");
  419. }