ShortenedDigest.cs 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. using BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto;
  5. namespace BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto.Digests
  6. {
  7. /**
  8. * Wrapper class that reduces the output length of a particular digest to
  9. * only the first n bytes of the digest function.
  10. */
  11. public class ShortenedDigest
  12. : IDigest
  13. {
  14. private IDigest baseDigest;
  15. private int length;
  16. /**
  17. * Base constructor.
  18. *
  19. * @param baseDigest underlying digest to use.
  20. * @param length length in bytes of the output of doFinal.
  21. * @exception ArgumentException if baseDigest is null, or length is greater than baseDigest.GetDigestSize().
  22. */
  23. public ShortenedDigest(
  24. IDigest baseDigest,
  25. int length)
  26. {
  27. if (baseDigest == null)
  28. {
  29. throw new ArgumentNullException("baseDigest");
  30. }
  31. if (length > baseDigest.GetDigestSize())
  32. {
  33. throw new ArgumentException("baseDigest output not large enough to support length");
  34. }
  35. this.baseDigest = baseDigest;
  36. this.length = length;
  37. }
  38. public string AlgorithmName
  39. {
  40. get { return baseDigest.AlgorithmName + "(" + length * 8 + ")"; }
  41. }
  42. public int GetDigestSize()
  43. {
  44. return length;
  45. }
  46. public void Update(byte input)
  47. {
  48. baseDigest.Update(input);
  49. }
  50. public void BlockUpdate(byte[] input, int inOff, int length)
  51. {
  52. baseDigest.BlockUpdate(input, inOff, length);
  53. }
  54. public int DoFinal(byte[] output, int outOff)
  55. {
  56. byte[] tmp = new byte[baseDigest.GetDigestSize()];
  57. baseDigest.DoFinal(tmp, 0);
  58. Array.Copy(tmp, 0, output, outOff, length);
  59. return length;
  60. }
  61. public void Reset()
  62. {
  63. baseDigest.Reset();
  64. }
  65. public int GetByteLength()
  66. {
  67. return baseDigest.GetByteLength();
  68. }
  69. }
  70. }
  71. #pragma warning restore
  72. #endif