Sha512Digest.cs 3.3 KB

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