SkeinDigest.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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.Parameters;
  5. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Utilities;
  6. namespace Best.HTTP.SecureProtocol.Org.BouncyCastle.Crypto.Digests
  7. {
  8. /// <summary>
  9. /// Implementation of the Skein parameterised hash function in 256, 512 and 1024 bit block sizes,
  10. /// based on the <see cref="Best.HTTP.SecureProtocol.Org.BouncyCastle.Crypto.Engines.ThreefishEngine">Threefish</see> tweakable block cipher.
  11. /// </summary>
  12. /// <remarks>
  13. /// This is the 1.3 version of Skein defined in the Skein hash function submission to the NIST SHA-3
  14. /// competition in October 2010.
  15. /// <p/>
  16. /// Skein was designed by Niels Ferguson - Stefan Lucks - Bruce Schneier - Doug Whiting - Mihir
  17. /// Bellare - Tadayoshi Kohno - Jon Callas - Jesse Walker.
  18. /// </remarks>
  19. /// <seealso cref="Best.HTTP.SecureProtocol.Org.BouncyCastle.Crypto.Digests.SkeinEngine"/>
  20. /// <seealso cref="Best.HTTP.SecureProtocol.Org.BouncyCastle.Crypto.Parameters.SkeinParameters"/>
  21. public class SkeinDigest
  22. : IDigest, IMemoable
  23. {
  24. /// <summary>
  25. /// 256 bit block size - Skein-256
  26. /// </summary>
  27. public const int SKEIN_256 = SkeinEngine.SKEIN_256;
  28. /// <summary>
  29. /// 512 bit block size - Skein-512
  30. /// </summary>
  31. public const int SKEIN_512 = SkeinEngine.SKEIN_512;
  32. /// <summary>
  33. /// 1024 bit block size - Skein-1024
  34. /// </summary>
  35. public const int SKEIN_1024 = SkeinEngine.SKEIN_1024;
  36. private readonly SkeinEngine engine;
  37. /// <summary>
  38. /// Constructs a Skein digest with an internal state size and output size.
  39. /// </summary>
  40. /// <param name="stateSizeBits">the internal state size in bits - one of <see cref="SKEIN_256"/> <see cref="SKEIN_512"/> or
  41. /// <see cref="SKEIN_1024"/>.</param>
  42. /// <param name="digestSizeBits">the output/digest size to produce in bits, which must be an integral number of
  43. /// bytes.</param>
  44. public SkeinDigest(int stateSizeBits, int digestSizeBits)
  45. {
  46. this.engine = new SkeinEngine(stateSizeBits, digestSizeBits);
  47. Init(null);
  48. }
  49. public SkeinDigest(SkeinDigest digest)
  50. {
  51. this.engine = new SkeinEngine(digest.engine);
  52. }
  53. public void Reset(IMemoable other)
  54. {
  55. SkeinDigest d = (SkeinDigest)other;
  56. engine.Reset(d.engine);
  57. }
  58. public IMemoable Copy()
  59. {
  60. return new SkeinDigest(this);
  61. }
  62. public string AlgorithmName
  63. {
  64. get { return "Skein-" + (engine.BlockSize * 8) + "-" + (engine.OutputSize * 8); }
  65. }
  66. public int GetDigestSize()
  67. {
  68. return engine.OutputSize;
  69. }
  70. public int GetByteLength()
  71. {
  72. return engine.BlockSize;
  73. }
  74. /// <summary>
  75. /// Optionally initialises the Skein digest with the provided parameters.
  76. /// </summary>
  77. /// See <see cref="Best.HTTP.SecureProtocol.Org.BouncyCastle.Crypto.Parameters.SkeinParameters"></see> for details on the parameterisation of the Skein hash function.
  78. /// <param name="parameters">the parameters to apply to this engine, or <code>null</code> to use no parameters.</param>
  79. public void Init(SkeinParameters parameters)
  80. {
  81. engine.Init(parameters);
  82. }
  83. public void Reset()
  84. {
  85. engine.Reset();
  86. }
  87. public void Update(byte inByte)
  88. {
  89. engine.Update(inByte);
  90. }
  91. public void BlockUpdate(byte[] inBytes, int inOff, int len)
  92. {
  93. engine.BlockUpdate(inBytes, inOff, len);
  94. }
  95. public int DoFinal(byte[] outBytes, int outOff)
  96. {
  97. return engine.DoFinal(outBytes, outOff);
  98. }
  99. #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER || UNITY_2021_2_OR_NEWER
  100. public void BlockUpdate(ReadOnlySpan<byte> input)
  101. {
  102. engine.BlockUpdate(input);
  103. }
  104. public int DoFinal(Span<byte> output)
  105. {
  106. return engine.DoFinal(output);
  107. }
  108. #endif
  109. }
  110. }
  111. #pragma warning restore
  112. #endif