MD4.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  1. /* Copyright (C) 1990-2, RSA Data Security, Inc. All rights reserved.
  2. License to copy and use this software is granted provided that it
  3. is identified as the "RSA Data Security, Inc. MD4 Message-Digest
  4. Algorithm" in all material mentioning or referencing this software
  5. or this function.
  6. License is also granted to make and use derivative works provided
  7. that such works are identified as "derived from the RSA Data
  8. Security, Inc. MD4 Message-Digest Algorithm" in all material
  9. mentioning or referencing the derived work.
  10. RSA Data Security, Inc. makes no representations concerning either
  11. the merchantability of this software or the suitability of this
  12. software for any particular purpose. It is provided "as is"
  13. without express or implied warranty of any kind.
  14. --------------------------------------------------------------
  15. Ported from Norbert Hranitzky's (norbert.hranitzky@mchp.siemens.de)
  16. Java version by Oren Novotny (osn@po.cwru.edu)
  17. --------------------------------------------------------------
  18. Adapted to C# 2.0 By Tal Aloni
  19. --------------------------------------------------------------
  20. */
  21. using System.Text;
  22. namespace System.Security.Cryptography
  23. {
  24. using System;
  25. /// <summary>
  26. /// Implements the MD4 message digest algorithm in C#
  27. /// </summary>
  28. /// <remarks>
  29. /// <p>
  30. /// <b>References:</b>
  31. /// <ol>
  32. /// <li> Ronald L. Rivest,
  33. /// "<a href = "http://www.roxen.com/rfc/rfc1320.html">
  34. /// The MD4 Message-Digest Algorithm</a>",
  35. /// IETF RFC-1320 (informational).
  36. /// </li>
  37. /// </ol>
  38. /// </p>
  39. /// </remarks>
  40. internal class MD4
  41. {
  42. // MD4 specific object variables
  43. //-----------------------------------------------------------------------
  44. /// <summary>
  45. /// The size in bytes of the input block to the transformation algorithm
  46. /// </summary>
  47. private const int BLOCK_LENGTH = 64; // = 512 / 8
  48. /// <summary>
  49. /// 512-bit work buffer = 16 x 32-bit words
  50. /// </summary>
  51. private readonly uint[] X = new uint[16];
  52. /// <summary>
  53. /// 4 32-bit words (interim result)
  54. /// </summary>
  55. private readonly uint[] context = new uint[4];
  56. /// <summary>
  57. /// 512-bit input buffer = 16 x 32-bit words holds until it reaches 512 bits
  58. /// </summary>
  59. private byte[] buffer = new byte[BLOCK_LENGTH];
  60. /// <summary>
  61. /// Number of bytes procesed so far mod. 2 power of 64.
  62. /// </summary>
  63. private long count;
  64. // Constructors
  65. //------------------------------------------------------------------------
  66. public MD4()
  67. {
  68. EngineReset();
  69. }
  70. /// <summary>
  71. /// This constructor is here to implement the clonability of this class
  72. /// </summary>
  73. /// <param name = "md"> </param>
  74. private MD4(MD4 md)
  75. : this()
  76. {
  77. //this();
  78. context = (uint[])md.context.Clone();
  79. buffer = (byte[])md.buffer.Clone();
  80. count = md.count;
  81. }
  82. // Clonable method implementation
  83. //-------------------------------------------------------------------------
  84. public object Clone()
  85. {
  86. return new MD4(this);
  87. }
  88. // JCE methods
  89. //-------------------------------------------------------------------------
  90. /// <summary>
  91. /// Resets this object disregarding any temporary data present at the
  92. /// time of the invocation of this call.
  93. /// </summary>
  94. private void EngineReset()
  95. {
  96. // initial values of MD4 i.e. A, B, C, D
  97. // as per rfc-1320; they are low-order byte first
  98. context[0] = 0x67452301;
  99. context[1] = 0xEFCDAB89;
  100. context[2] = 0x98BADCFE;
  101. context[3] = 0x10325476;
  102. count = 0L;
  103. for (int i = 0; i < BLOCK_LENGTH; i++)
  104. buffer[i] = 0;
  105. }
  106. /// <summary>
  107. /// Continues an MD4 message digest using the input byte
  108. /// </summary>
  109. /// <param name = "b">byte to input</param>
  110. private void EngineUpdate(byte b)
  111. {
  112. // compute number of bytes still unhashed; ie. present in buffer
  113. int i = (int)(count % BLOCK_LENGTH);
  114. count++; // update number of bytes
  115. buffer[i] = b;
  116. if (i == BLOCK_LENGTH - 1)
  117. Transform(ref buffer, 0);
  118. }
  119. /// <summary>
  120. /// MD4 block update operation
  121. /// </summary>
  122. /// <remarks>
  123. /// Continues an MD4 message digest operation by filling the buffer,
  124. /// transform(ing) data in 512-bit message block(s), updating the variables
  125. /// context and count, and leaving (buffering) the remaining bytes in buffer
  126. /// for the next update or finish.
  127. /// </remarks>
  128. /// <param name = "input">input block</param>
  129. /// <param name = "offset">start of meaningful bytes in input</param>
  130. /// <param name = "len">count of bytes in input blcok to consider</param>
  131. private void EngineUpdate(byte[] input, int offset, int len)
  132. {
  133. // make sure we don't exceed input's allocated size/length
  134. if (offset < 0 || len < 0 || (long)offset + len > input.Length)
  135. throw new ArgumentOutOfRangeException();
  136. // compute number of bytes still unhashed; ie. present in buffer
  137. int bufferNdx = (int)(count % BLOCK_LENGTH);
  138. count += len; // update number of bytes
  139. int partLen = BLOCK_LENGTH - bufferNdx;
  140. int i = 0;
  141. if (len >= partLen)
  142. {
  143. Array.Copy(input, offset + i, buffer, bufferNdx, partLen);
  144. Transform(ref buffer, 0);
  145. for (i = partLen; i + BLOCK_LENGTH - 1 < len; i += BLOCK_LENGTH)
  146. Transform(ref input, offset + i);
  147. bufferNdx = 0;
  148. }
  149. // buffer remaining input
  150. if (i < len)
  151. Array.Copy(input, offset + i, buffer, bufferNdx, len - i);
  152. }
  153. /// <summary>
  154. /// Completes the hash computation by performing final operations such
  155. /// as padding. At the return of this engineDigest, the MD engine is
  156. /// reset.
  157. /// </summary>
  158. /// <returns>the array of bytes for the resulting hash value.</returns>
  159. private byte[] EngineDigest()
  160. {
  161. // pad output to 56 mod 64; as RFC1320 puts it: congruent to 448 mod 512
  162. int bufferNdx = (int)(count % BLOCK_LENGTH);
  163. int padLen = (bufferNdx < 56) ? (56 - bufferNdx) : (120 - bufferNdx);
  164. // padding is always binary 1 followed by binary 0's
  165. byte[] tail = new byte[padLen + 8];
  166. tail[0] = 0x80;
  167. // append length before final transform
  168. // save number of bits, casting the long to an array of 8 bytes
  169. // save low-order byte first.
  170. for (int i = 0; i < 8; i++)
  171. tail[padLen + i] = (byte)((count * 8) >> (8 * i));
  172. EngineUpdate(tail, 0, tail.Length);
  173. byte[] result = new byte[16];
  174. // cast this MD4's context (array of 4 uints) into an array of 16 bytes.
  175. for (int i = 0; i < 4; i++)
  176. for (int j = 0; j < 4; j++)
  177. result[i * 4 + j] = (byte)(context[i] >> (8 * j));
  178. // reset the engine
  179. EngineReset();
  180. return result;
  181. }
  182. /// <summary>
  183. /// Returns a byte hash from a string
  184. /// </summary>
  185. /// <param name = "s">string to hash</param>
  186. /// <returns>byte-array that contains the hash</returns>
  187. public byte[] GetByteHashFromString(string s)
  188. {
  189. byte[] b = Encoding.UTF8.GetBytes(s);
  190. MD4 md4 = new MD4();
  191. md4.EngineUpdate(b, 0, b.Length);
  192. return md4.EngineDigest();
  193. }
  194. /// <summary>
  195. /// Returns a binary hash from an input byte array
  196. /// </summary>
  197. /// <param name = "b">byte-array to hash</param>
  198. /// <returns>binary hash of input</returns>
  199. public byte[] GetByteHashFromBytes(byte[] b)
  200. {
  201. MD4 md4 = new MD4();
  202. md4.EngineUpdate(b, 0, b.Length);
  203. return md4.EngineDigest();
  204. }
  205. /// <summary>
  206. /// Returns a string that contains the hexadecimal hash
  207. /// </summary>
  208. /// <param name = "b">byte-array to input</param>
  209. /// <returns>String that contains the hex of the hash</returns>
  210. public string GetHexHashFromBytes(byte[] b)
  211. {
  212. byte[] e = GetByteHashFromBytes(b);
  213. return BytesToHex(e, e.Length);
  214. }
  215. /// <summary>
  216. /// Returns a byte hash from the input byte
  217. /// </summary>
  218. /// <param name = "b">byte to hash</param>
  219. /// <returns>binary hash of the input byte</returns>
  220. public byte[] GetByteHashFromByte(byte b)
  221. {
  222. MD4 md4 = new MD4();
  223. md4.EngineUpdate(b);
  224. return md4.EngineDigest();
  225. }
  226. /// <summary>
  227. /// Returns a string that contains the hexadecimal hash
  228. /// </summary>
  229. /// <param name = "b">byte to hash</param>
  230. /// <returns>String that contains the hex of the hash</returns>
  231. public string GetHexHashFromByte(byte b)
  232. {
  233. byte[] e = GetByteHashFromByte(b);
  234. return BytesToHex(e, e.Length);
  235. }
  236. /// <summary>
  237. /// Returns a string that contains the hexadecimal hash
  238. /// </summary>
  239. /// <param name = "s">string to hash</param>
  240. /// <returns>String that contains the hex of the hash</returns>
  241. public string GetHexHashFromString(string s)
  242. {
  243. byte[] b = GetByteHashFromString(s);
  244. return BytesToHex(b, b.Length);
  245. }
  246. private static string BytesToHex(byte[] a, int len)
  247. {
  248. string temp = BitConverter.ToString(a);
  249. // We need to remove the dashes that come from the BitConverter
  250. StringBuilder sb = new StringBuilder((len - 2) / 2); // This should be the final size
  251. for (int i = 0; i < temp.Length; i++)
  252. if (temp[i] != '-')
  253. sb.Append(temp[i]);
  254. return sb.ToString();
  255. }
  256. // own methods
  257. //-----------------------------------------------------------------------------------
  258. /// <summary>
  259. /// MD4 basic transformation
  260. /// </summary>
  261. /// <remarks>
  262. /// Transforms context based on 512 bits from input block starting
  263. /// from the offset'th byte.
  264. /// </remarks>
  265. /// <param name = "block">input sub-array</param>
  266. /// <param name = "offset">starting position of sub-array</param>
  267. private void Transform(ref byte[] block, int offset)
  268. {
  269. // decodes 64 bytes from input block into an array of 16 32-bit
  270. // entities. Use A as a temp var.
  271. for (int i = 0; i < 16; i++)
  272. X[i] = ((uint)block[offset++] & 0xFF) |
  273. (((uint)block[offset++] & 0xFF) << 8) |
  274. (((uint)block[offset++] & 0xFF) << 16) |
  275. (((uint)block[offset++] & 0xFF) << 24);
  276. uint A = context[0];
  277. uint B = context[1];
  278. uint C = context[2];
  279. uint D = context[3];
  280. A = FF(A, B, C, D, X[0], 3);
  281. D = FF(D, A, B, C, X[1], 7);
  282. C = FF(C, D, A, B, X[2], 11);
  283. B = FF(B, C, D, A, X[3], 19);
  284. A = FF(A, B, C, D, X[4], 3);
  285. D = FF(D, A, B, C, X[5], 7);
  286. C = FF(C, D, A, B, X[6], 11);
  287. B = FF(B, C, D, A, X[7], 19);
  288. A = FF(A, B, C, D, X[8], 3);
  289. D = FF(D, A, B, C, X[9], 7);
  290. C = FF(C, D, A, B, X[10], 11);
  291. B = FF(B, C, D, A, X[11], 19);
  292. A = FF(A, B, C, D, X[12], 3);
  293. D = FF(D, A, B, C, X[13], 7);
  294. C = FF(C, D, A, B, X[14], 11);
  295. B = FF(B, C, D, A, X[15], 19);
  296. A = GG(A, B, C, D, X[0], 3);
  297. D = GG(D, A, B, C, X[4], 5);
  298. C = GG(C, D, A, B, X[8], 9);
  299. B = GG(B, C, D, A, X[12], 13);
  300. A = GG(A, B, C, D, X[1], 3);
  301. D = GG(D, A, B, C, X[5], 5);
  302. C = GG(C, D, A, B, X[9], 9);
  303. B = GG(B, C, D, A, X[13], 13);
  304. A = GG(A, B, C, D, X[2], 3);
  305. D = GG(D, A, B, C, X[6], 5);
  306. C = GG(C, D, A, B, X[10], 9);
  307. B = GG(B, C, D, A, X[14], 13);
  308. A = GG(A, B, C, D, X[3], 3);
  309. D = GG(D, A, B, C, X[7], 5);
  310. C = GG(C, D, A, B, X[11], 9);
  311. B = GG(B, C, D, A, X[15], 13);
  312. A = HH(A, B, C, D, X[0], 3);
  313. D = HH(D, A, B, C, X[8], 9);
  314. C = HH(C, D, A, B, X[4], 11);
  315. B = HH(B, C, D, A, X[12], 15);
  316. A = HH(A, B, C, D, X[2], 3);
  317. D = HH(D, A, B, C, X[10], 9);
  318. C = HH(C, D, A, B, X[6], 11);
  319. B = HH(B, C, D, A, X[14], 15);
  320. A = HH(A, B, C, D, X[1], 3);
  321. D = HH(D, A, B, C, X[9], 9);
  322. C = HH(C, D, A, B, X[5], 11);
  323. B = HH(B, C, D, A, X[13], 15);
  324. A = HH(A, B, C, D, X[3], 3);
  325. D = HH(D, A, B, C, X[11], 9);
  326. C = HH(C, D, A, B, X[7], 11);
  327. B = HH(B, C, D, A, X[15], 15);
  328. context[0] += A;
  329. context[1] += B;
  330. context[2] += C;
  331. context[3] += D;
  332. }
  333. // The basic MD4 atomic functions.
  334. private uint FF(uint a, uint b, uint c, uint d, uint x, int s)
  335. {
  336. uint t = a + ((b & c) | (~b & d)) + x;
  337. return t << s | t >> (32 - s);
  338. }
  339. private uint GG(uint a, uint b, uint c, uint d, uint x, int s)
  340. {
  341. uint t = a + ((b & (c | d)) | (c & d)) + x + 0x5A827999;
  342. return t << s | t >> (32 - s);
  343. }
  344. private uint HH(uint a, uint b, uint c, uint d, uint x, int s)
  345. {
  346. uint t = a + (b ^ c ^ d) + x + 0x6ED9EBA1;
  347. return t << s | t >> (32 - s);
  348. }
  349. }
  350. // class MD4
  351. }
  352. // namespace MD4Hash