SHA1Hash.cs 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. using System.Security.Cryptography;
  2. using System;
  3. namespace Renci.SshNet.Security.Cryptography
  4. {
  5. /// <summary>
  6. /// SHA1 algorithm implementation
  7. /// </summary>
  8. public class SHA1Hash : HashAlgorithm
  9. {
  10. private const int DIGEST_SIZE = 20;
  11. private const uint Y1 = 0x5a827999;
  12. private const uint Y2 = 0x6ed9eba1;
  13. private const uint Y3 = 0x8f1bbcdc;
  14. private const uint Y4 = 0xca62c1d6;
  15. private uint H1, H2, H3, H4, H5;
  16. private uint[] _hashValue = new uint[80];
  17. private int _offset;
  18. private byte[] _buffer;
  19. private int _bufferOffset;
  20. private long _byteCount;
  21. /// <summary>
  22. /// Gets the size, in bits, of the computed hash code.
  23. /// </summary>
  24. /// <returns>The size, in bits, of the computed hash code.</returns>
  25. public override int HashSize
  26. {
  27. get
  28. {
  29. return DIGEST_SIZE * 8;
  30. }
  31. }
  32. /// <summary>
  33. /// Gets the input block size.
  34. /// </summary>
  35. /// <returns>The input block size.</returns>
  36. public override int InputBlockSize
  37. {
  38. get
  39. {
  40. return 64;
  41. }
  42. }
  43. /// <summary>
  44. /// Gets the output block size.
  45. /// </summary>
  46. /// <returns>The output block size.</returns>
  47. public override int OutputBlockSize
  48. {
  49. get
  50. {
  51. return 64;
  52. }
  53. }
  54. /// <summary>
  55. /// Gets a value indicating whether the current transform can be reused.
  56. /// </summary>
  57. /// <returns>Always true.</returns>
  58. public override bool CanReuseTransform
  59. {
  60. get
  61. {
  62. return true;
  63. }
  64. }
  65. /// <summary>
  66. /// Gets a value indicating whether multiple blocks can be transformed.
  67. /// </summary>
  68. /// <returns>true if multiple blocks can be transformed; otherwise, false.</returns>
  69. public override bool CanTransformMultipleBlocks
  70. {
  71. get
  72. {
  73. return true;
  74. }
  75. }
  76. /// <summary>
  77. /// Initializes a new instance of the <see cref="SHA1Hash"/> class.
  78. /// </summary>
  79. public SHA1Hash()
  80. {
  81. this._buffer = new byte[4];
  82. this.Initialize();
  83. }
  84. /// <summary>
  85. /// Routes data written to the object into the hash algorithm for computing the hash.
  86. /// </summary>
  87. /// <param name="array">The input to compute the hash code for.</param>
  88. /// <param name="ibStart">The offset into the byte array from which to begin using data.</param>
  89. /// <param name="cbSize">The number of bytes in the byte array to use as data.</param>
  90. protected override void HashCore(byte[] array, int ibStart, int cbSize)
  91. {
  92. // Fill the current word
  93. while ((this._bufferOffset != 0) && (cbSize > 0))
  94. {
  95. this.Update(array[ibStart]);
  96. ibStart++;
  97. cbSize--;
  98. }
  99. // Process whole words.
  100. while (cbSize > this._buffer.Length)
  101. {
  102. this.ProcessWord(array, ibStart);
  103. ibStart += this._buffer.Length;
  104. cbSize -= this._buffer.Length;
  105. this._byteCount += this._buffer.Length;
  106. }
  107. // Load in the remainder.
  108. while (cbSize > 0)
  109. {
  110. this.Update(array[ibStart]);
  111. ibStart++;
  112. cbSize--;
  113. }
  114. }
  115. /// <summary>
  116. /// Finalizes the hash computation after the last data is processed by the cryptographic stream object.
  117. /// </summary>
  118. /// <returns>
  119. /// The computed hash code.
  120. /// </returns>
  121. protected override byte[] HashFinal()
  122. {
  123. var output = new byte[DIGEST_SIZE];
  124. long bitLength = (this._byteCount << 3);
  125. //
  126. // add the pad bytes.
  127. //
  128. this.Update((byte)128);
  129. while (this._bufferOffset != 0)
  130. this.Update((byte)0);
  131. if (this._offset > 14)
  132. {
  133. this.ProcessBlock();
  134. }
  135. _hashValue[14] = (uint)((ulong)bitLength >> 32);
  136. _hashValue[15] = (uint)((ulong)bitLength);
  137. this.ProcessBlock();
  138. UInt32_To__BE(H1, output, 0);
  139. UInt32_To__BE(H2, output, 0 + 4);
  140. UInt32_To__BE(H3, output, 0 + 8);
  141. UInt32_To__BE(H4, output, 0 + 12);
  142. UInt32_To__BE(H5, output, 0 + 16);
  143. this.Initialize();
  144. return output;
  145. }
  146. /// <summary>
  147. /// Initializes an implementation of the <see cref="T:System.Security.Cryptography.HashAlgorithm"/> class.
  148. /// </summary>
  149. public override void Initialize()
  150. {
  151. this._byteCount = 0;
  152. this._bufferOffset = 0;
  153. Array.Clear(this._buffer, 0, this._buffer.Length);
  154. H1 = 0x67452301;
  155. H2 = 0xefcdab89;
  156. H3 = 0x98badcfe;
  157. H4 = 0x10325476;
  158. H5 = 0xc3d2e1f0;
  159. this._offset = 0;
  160. Array.Clear(_hashValue, 0, _hashValue.Length);
  161. }
  162. private void Update(byte input)
  163. {
  164. this._buffer[this._bufferOffset++] = input;
  165. if (this._bufferOffset == this._buffer.Length)
  166. {
  167. this.ProcessWord(this._buffer, 0);
  168. this._bufferOffset = 0;
  169. }
  170. this._byteCount++;
  171. }
  172. private void ProcessWord(byte[] input, int inOff)
  173. {
  174. _hashValue[this._offset] = BE_To__UInt32(input, inOff);
  175. if (++this._offset == 16)
  176. {
  177. this.ProcessBlock();
  178. }
  179. }
  180. private static uint F(uint u, uint v, uint w)
  181. {
  182. return (u & v) | (~u & w);
  183. }
  184. private static uint H(uint u, uint v, uint w)
  185. {
  186. return u ^ v ^ w;
  187. }
  188. private static uint G(uint u, uint v, uint w)
  189. {
  190. return (u & v) | (u & w) | (v & w);
  191. }
  192. private void ProcessBlock()
  193. {
  194. //
  195. // expand 16 word block into 80 word block.
  196. //
  197. for (int i = 16; i < 80; i++)
  198. {
  199. uint t = _hashValue[i - 3] ^ _hashValue[i - 8] ^ _hashValue[i - 14] ^ _hashValue[i - 16];
  200. _hashValue[i] = t << 1 | t >> 31;
  201. }
  202. //
  203. // set up working variables.
  204. //
  205. uint A = H1;
  206. uint B = H2;
  207. uint C = H3;
  208. uint D = H4;
  209. uint E = H5;
  210. //
  211. // round 1
  212. //
  213. int idx = 0;
  214. for (int j = 0; j < 4; j++)
  215. {
  216. // E = rotateLeft(A, 5) + F(B, C, D) + E + X[idx++] + Y1
  217. // B = rotateLeft(B, 30)
  218. E += (A << 5 | (A >> 27)) + F(B, C, D) + _hashValue[idx++] + Y1;
  219. B = B << 30 | (B >> 2);
  220. D += (E << 5 | (E >> 27)) + F(A, B, C) + _hashValue[idx++] + Y1;
  221. A = A << 30 | (A >> 2);
  222. C += (D << 5 | (D >> 27)) + F(E, A, B) + _hashValue[idx++] + Y1;
  223. E = E << 30 | (E >> 2);
  224. B += (C << 5 | (C >> 27)) + F(D, E, A) + _hashValue[idx++] + Y1;
  225. D = D << 30 | (D >> 2);
  226. A += (B << 5 | (B >> 27)) + F(C, D, E) + _hashValue[idx++] + Y1;
  227. C = C << 30 | (C >> 2);
  228. }
  229. //
  230. // round 2
  231. //
  232. for (int j = 0; j < 4; j++)
  233. {
  234. // E = rotateLeft(A, 5) + H(B, C, D) + E + X[idx++] + Y2
  235. // B = rotateLeft(B, 30)
  236. E += (A << 5 | (A >> 27)) + H(B, C, D) + _hashValue[idx++] + Y2;
  237. B = B << 30 | (B >> 2);
  238. D += (E << 5 | (E >> 27)) + H(A, B, C) + _hashValue[idx++] + Y2;
  239. A = A << 30 | (A >> 2);
  240. C += (D << 5 | (D >> 27)) + H(E, A, B) + _hashValue[idx++] + Y2;
  241. E = E << 30 | (E >> 2);
  242. B += (C << 5 | (C >> 27)) + H(D, E, A) + _hashValue[idx++] + Y2;
  243. D = D << 30 | (D >> 2);
  244. A += (B << 5 | (B >> 27)) + H(C, D, E) + _hashValue[idx++] + Y2;
  245. C = C << 30 | (C >> 2);
  246. }
  247. //
  248. // round 3
  249. //
  250. for (int j = 0; j < 4; j++)
  251. {
  252. // E = rotateLeft(A, 5) + G(B, C, D) + E + X[idx++] + Y3
  253. // B = rotateLeft(B, 30)
  254. E += (A << 5 | (A >> 27)) + G(B, C, D) + _hashValue[idx++] + Y3;
  255. B = B << 30 | (B >> 2);
  256. D += (E << 5 | (E >> 27)) + G(A, B, C) + _hashValue[idx++] + Y3;
  257. A = A << 30 | (A >> 2);
  258. C += (D << 5 | (D >> 27)) + G(E, A, B) + _hashValue[idx++] + Y3;
  259. E = E << 30 | (E >> 2);
  260. B += (C << 5 | (C >> 27)) + G(D, E, A) + _hashValue[idx++] + Y3;
  261. D = D << 30 | (D >> 2);
  262. A += (B << 5 | (B >> 27)) + G(C, D, E) + _hashValue[idx++] + Y3;
  263. C = C << 30 | (C >> 2);
  264. }
  265. //
  266. // round 4
  267. //
  268. for (int j = 0; j < 4; j++)
  269. {
  270. // E = rotateLeft(A, 5) + H(B, C, D) + E + X[idx++] + Y4
  271. // B = rotateLeft(B, 30)
  272. E += (A << 5 | (A >> 27)) + H(B, C, D) + _hashValue[idx++] + Y4;
  273. B = B << 30 | (B >> 2);
  274. D += (E << 5 | (E >> 27)) + H(A, B, C) + _hashValue[idx++] + Y4;
  275. A = A << 30 | (A >> 2);
  276. C += (D << 5 | (D >> 27)) + H(E, A, B) + _hashValue[idx++] + Y4;
  277. E = E << 30 | (E >> 2);
  278. B += (C << 5 | (C >> 27)) + H(D, E, A) + _hashValue[idx++] + Y4;
  279. D = D << 30 | (D >> 2);
  280. A += (B << 5 | (B >> 27)) + H(C, D, E) + _hashValue[idx++] + Y4;
  281. C = C << 30 | (C >> 2);
  282. }
  283. H1 += A;
  284. H2 += B;
  285. H3 += C;
  286. H4 += D;
  287. H5 += E;
  288. //
  289. // reset start of the buffer.
  290. //
  291. this._offset = 0;
  292. Array.Clear(_hashValue, 0, 16);
  293. }
  294. private static uint BE_To__UInt32(byte[] bs, int off)
  295. {
  296. uint n = (uint)bs[off] << 24;
  297. n |= (uint)bs[++off] << 16;
  298. n |= (uint)bs[++off] << 8;
  299. n |= (uint)bs[++off];
  300. return n;
  301. }
  302. private static void UInt32_To__BE
  303. (uint n, byte[] bs, int off)
  304. {
  305. bs[off] = (byte)(n >> 24);
  306. bs[++off] = (byte)(n >> 16);
  307. bs[++off] = (byte)(n >> 8);
  308. bs[++off] = (byte)(n);
  309. }
  310. }
  311. }