SkeinDigest.cs 3.6 KB

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