Sha1Digest.cs 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. using BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto.Utilities;
  5. using BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities;
  6. namespace BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto.Digests
  7. {
  8. /**
  9. * implementation of SHA-1 as outlined in "Handbook of Applied Cryptography", pages 346 - 349.
  10. *
  11. * It is interesting to ponder why the, apart from the extra IV, the other difference here from MD5
  12. * is the "endianness" of the word processing!
  13. */
  14. public class Sha1Digest
  15. : GeneralDigest
  16. {
  17. private const int DigestLength = 20;
  18. private uint H1, H2, H3, H4, H5;
  19. private uint[] X = new uint[80];
  20. private int xOff;
  21. public Sha1Digest()
  22. {
  23. Reset();
  24. }
  25. /**
  26. * Copy constructor. This will copy the state of the provided
  27. * message digest.
  28. */
  29. public Sha1Digest(Sha1Digest t)
  30. : base(t)
  31. {
  32. CopyIn(t);
  33. }
  34. private void CopyIn(Sha1Digest t)
  35. {
  36. base.CopyIn(t);
  37. H1 = t.H1;
  38. H2 = t.H2;
  39. H3 = t.H3;
  40. H4 = t.H4;
  41. H5 = t.H5;
  42. Array.Copy(t.X, 0, X, 0, t.X.Length);
  43. xOff = t.xOff;
  44. }
  45. public override string AlgorithmName
  46. {
  47. get { return "SHA-1"; }
  48. }
  49. public override int GetDigestSize()
  50. {
  51. return DigestLength;
  52. }
  53. internal override void ProcessWord(
  54. byte[] input,
  55. int inOff)
  56. {
  57. X[xOff] = Pack.BE_To_UInt32(input, inOff);
  58. if (++xOff == 16)
  59. {
  60. ProcessBlock();
  61. }
  62. }
  63. internal override void ProcessLength(long bitLength)
  64. {
  65. if (xOff > 14)
  66. {
  67. ProcessBlock();
  68. }
  69. X[14] = (uint)((ulong)bitLength >> 32);
  70. X[15] = (uint)((ulong)bitLength);
  71. }
  72. public override int DoFinal(
  73. byte[] output,
  74. int outOff)
  75. {
  76. Finish();
  77. Pack.UInt32_To_BE(H1, output, outOff);
  78. Pack.UInt32_To_BE(H2, output, outOff + 4);
  79. Pack.UInt32_To_BE(H3, output, outOff + 8);
  80. Pack.UInt32_To_BE(H4, output, outOff + 12);
  81. Pack.UInt32_To_BE(H5, output, outOff + 16);
  82. Reset();
  83. return DigestLength;
  84. }
  85. /**
  86. * reset the chaining variables
  87. */
  88. public override void Reset()
  89. {
  90. base.Reset();
  91. H1 = 0x67452301;
  92. H2 = 0xefcdab89;
  93. H3 = 0x98badcfe;
  94. H4 = 0x10325476;
  95. H5 = 0xc3d2e1f0;
  96. xOff = 0;
  97. Array.Clear(X, 0, X.Length);
  98. }
  99. //
  100. // Additive constants
  101. //
  102. private const uint Y1 = 0x5a827999;
  103. private const uint Y2 = 0x6ed9eba1;
  104. private const uint Y3 = 0x8f1bbcdc;
  105. private const uint Y4 = 0xca62c1d6;
  106. private static uint F(uint u, uint v, uint w)
  107. {
  108. return (u & v) | (~u & w);
  109. }
  110. private static uint H(uint u, uint v, uint w)
  111. {
  112. return u ^ v ^ w;
  113. }
  114. private static uint G(uint u, uint v, uint w)
  115. {
  116. return (u & v) | (u & w) | (v & w);
  117. }
  118. internal override void ProcessBlock()
  119. {
  120. //
  121. // expand 16 word block into 80 word block.
  122. //
  123. for (int i = 16; i < 80; i++)
  124. {
  125. uint t = X[i - 3] ^ X[i - 8] ^ X[i - 14] ^ X[i - 16];
  126. X[i] = t << 1 | t >> 31;
  127. }
  128. //
  129. // set up working variables.
  130. //
  131. uint A = H1;
  132. uint B = H2;
  133. uint C = H3;
  134. uint D = H4;
  135. uint E = H5;
  136. //
  137. // round 1
  138. //
  139. int idx = 0;
  140. for (int j = 0; j < 4; j++)
  141. {
  142. // E = rotateLeft(A, 5) + F(B, C, D) + E + X[idx++] + Y1
  143. // B = rotateLeft(B, 30)
  144. E += (A << 5 | (A >> 27)) + F(B, C, D) + X[idx++] + Y1;
  145. B = B << 30 | (B >> 2);
  146. D += (E << 5 | (E >> 27)) + F(A, B, C) + X[idx++] + Y1;
  147. A = A << 30 | (A >> 2);
  148. C += (D << 5 | (D >> 27)) + F(E, A, B) + X[idx++] + Y1;
  149. E = E << 30 | (E >> 2);
  150. B += (C << 5 | (C >> 27)) + F(D, E, A) + X[idx++] + Y1;
  151. D = D << 30 | (D >> 2);
  152. A += (B << 5 | (B >> 27)) + F(C, D, E) + X[idx++] + Y1;
  153. C = C << 30 | (C >> 2);
  154. }
  155. //
  156. // round 2
  157. //
  158. for (int j = 0; j < 4; j++)
  159. {
  160. // E = rotateLeft(A, 5) + H(B, C, D) + E + X[idx++] + Y2
  161. // B = rotateLeft(B, 30)
  162. E += (A << 5 | (A >> 27)) + H(B, C, D) + X[idx++] + Y2;
  163. B = B << 30 | (B >> 2);
  164. D += (E << 5 | (E >> 27)) + H(A, B, C) + X[idx++] + Y2;
  165. A = A << 30 | (A >> 2);
  166. C += (D << 5 | (D >> 27)) + H(E, A, B) + X[idx++] + Y2;
  167. E = E << 30 | (E >> 2);
  168. B += (C << 5 | (C >> 27)) + H(D, E, A) + X[idx++] + Y2;
  169. D = D << 30 | (D >> 2);
  170. A += (B << 5 | (B >> 27)) + H(C, D, E) + X[idx++] + Y2;
  171. C = C << 30 | (C >> 2);
  172. }
  173. //
  174. // round 3
  175. //
  176. for (int j = 0; j < 4; j++)
  177. {
  178. // E = rotateLeft(A, 5) + G(B, C, D) + E + X[idx++] + Y3
  179. // B = rotateLeft(B, 30)
  180. E += (A << 5 | (A >> 27)) + G(B, C, D) + X[idx++] + Y3;
  181. B = B << 30 | (B >> 2);
  182. D += (E << 5 | (E >> 27)) + G(A, B, C) + X[idx++] + Y3;
  183. A = A << 30 | (A >> 2);
  184. C += (D << 5 | (D >> 27)) + G(E, A, B) + X[idx++] + Y3;
  185. E = E << 30 | (E >> 2);
  186. B += (C << 5 | (C >> 27)) + G(D, E, A) + X[idx++] + Y3;
  187. D = D << 30 | (D >> 2);
  188. A += (B << 5 | (B >> 27)) + G(C, D, E) + X[idx++] + Y3;
  189. C = C << 30 | (C >> 2);
  190. }
  191. //
  192. // round 4
  193. //
  194. for (int j = 0; j < 4; j++)
  195. {
  196. // E = rotateLeft(A, 5) + H(B, C, D) + E + X[idx++] + Y4
  197. // B = rotateLeft(B, 30)
  198. E += (A << 5 | (A >> 27)) + H(B, C, D) + X[idx++] + Y4;
  199. B = B << 30 | (B >> 2);
  200. D += (E << 5 | (E >> 27)) + H(A, B, C) + X[idx++] + Y4;
  201. A = A << 30 | (A >> 2);
  202. C += (D << 5 | (D >> 27)) + H(E, A, B) + X[idx++] + Y4;
  203. E = E << 30 | (E >> 2);
  204. B += (C << 5 | (C >> 27)) + H(D, E, A) + X[idx++] + Y4;
  205. D = D << 30 | (D >> 2);
  206. A += (B << 5 | (B >> 27)) + H(C, D, E) + X[idx++] + Y4;
  207. C = C << 30 | (C >> 2);
  208. }
  209. H1 += A;
  210. H2 += B;
  211. H3 += C;
  212. H4 += D;
  213. H5 += E;
  214. //
  215. // reset start of the buffer.
  216. //
  217. xOff = 0;
  218. Array.Clear(X, 0, 16);
  219. }
  220. public override IMemoable Copy()
  221. {
  222. return new Sha1Digest(this);
  223. }
  224. public override void Reset(IMemoable other)
  225. {
  226. Sha1Digest d = (Sha1Digest)other;
  227. CopyIn(d);
  228. }
  229. }
  230. }
  231. #pragma warning restore
  232. #endif