Sha512Digest.cs 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. using System;
  3. using Org.BouncyCastle.Crypto.Utilities;
  4. using Org.BouncyCastle.Utilities;
  5. namespace Org.BouncyCastle.Crypto.Digests
  6. {
  7. /**
  8. * Draft FIPS 180-2 implementation of SHA-512. <b>Note:</b> As this is
  9. * based on a draft this implementation is subject to change.
  10. *
  11. * <pre>
  12. * block word digest
  13. * SHA-1 512 32 160
  14. * SHA-256 512 32 256
  15. * SHA-384 1024 64 384
  16. * SHA-512 1024 64 512
  17. * </pre>
  18. */
  19. public class Sha512Digest
  20. : LongDigest
  21. {
  22. private const int DigestLength = 64;
  23. public Sha512Digest()
  24. {
  25. }
  26. /**
  27. * Copy constructor. This will copy the state of the provided
  28. * message digest.
  29. */
  30. public Sha512Digest(
  31. Sha512Digest t)
  32. : base(t)
  33. {
  34. }
  35. public override string AlgorithmName
  36. {
  37. get { return "SHA-512"; }
  38. }
  39. public override int GetDigestSize()
  40. {
  41. return DigestLength;
  42. }
  43. public override int DoFinal(
  44. byte[] output,
  45. int outOff)
  46. {
  47. Finish();
  48. Pack.UInt64_To_BE(H1, output, outOff);
  49. Pack.UInt64_To_BE(H2, output, outOff + 8);
  50. Pack.UInt64_To_BE(H3, output, outOff + 16);
  51. Pack.UInt64_To_BE(H4, output, outOff + 24);
  52. Pack.UInt64_To_BE(H5, output, outOff + 32);
  53. Pack.UInt64_To_BE(H6, output, outOff + 40);
  54. Pack.UInt64_To_BE(H7, output, outOff + 48);
  55. Pack.UInt64_To_BE(H8, output, outOff + 56);
  56. Reset();
  57. return DigestLength;
  58. }
  59. /**
  60. * reset the chaining variables
  61. */
  62. public override void Reset()
  63. {
  64. base.Reset();
  65. /* SHA-512 initial hash value
  66. * The first 64 bits of the fractional parts of the square roots
  67. * of the first eight prime numbers
  68. */
  69. H1 = 0x6a09e667f3bcc908;
  70. H2 = 0xbb67ae8584caa73b;
  71. H3 = 0x3c6ef372fe94f82b;
  72. H4 = 0xa54ff53a5f1d36f1;
  73. H5 = 0x510e527fade682d1;
  74. H6 = 0x9b05688c2b3e6c1f;
  75. H7 = 0x1f83d9abfb41bd6b;
  76. H8 = 0x5be0cd19137e2179;
  77. }
  78. public override IMemoable Copy()
  79. {
  80. return new Sha512Digest(this);
  81. }
  82. public override void Reset(IMemoable other)
  83. {
  84. Sha512Digest d = (Sha512Digest)other;
  85. CopyIn(d);
  86. }
  87. }
  88. }
  89. #endif