BcTlsHash.cs 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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;
  5. namespace Best.HTTP.SecureProtocol.Org.BouncyCastle.Tls.Crypto.Impl.BC
  6. {
  7. internal sealed class BcTlsHash
  8. : TlsHash
  9. {
  10. private readonly BcTlsCrypto m_crypto;
  11. private readonly int m_cryptoHashAlgorithm;
  12. private readonly IDigest m_digest;
  13. internal BcTlsHash(BcTlsCrypto crypto, int cryptoHashAlgorithm)
  14. : this(crypto, cryptoHashAlgorithm, crypto.CreateDigest(cryptoHashAlgorithm))
  15. {
  16. }
  17. private BcTlsHash(BcTlsCrypto crypto, int cryptoHashAlgorithm, IDigest digest)
  18. {
  19. this.m_crypto = crypto;
  20. this.m_cryptoHashAlgorithm = cryptoHashAlgorithm;
  21. this.m_digest = digest;
  22. }
  23. public void Update(byte[] data, int offSet, int length)
  24. {
  25. m_digest.BlockUpdate(data, offSet, length);
  26. }
  27. #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER || UNITY_2021_2_OR_NEWER
  28. public void Update(ReadOnlySpan<byte> input)
  29. {
  30. m_digest.BlockUpdate(input);
  31. }
  32. #endif
  33. public byte[] CalculateHash()
  34. {
  35. byte[] rv = new byte[m_digest.GetDigestSize()];
  36. m_digest.DoFinal(rv, 0);
  37. return rv;
  38. }
  39. public TlsHash CloneHash()
  40. {
  41. IDigest clone = m_crypto.CloneDigest(m_cryptoHashAlgorithm, m_digest);
  42. return new BcTlsHash(m_crypto, m_cryptoHashAlgorithm, clone);
  43. }
  44. public void Reset()
  45. {
  46. m_digest.Reset();
  47. }
  48. }
  49. }
  50. #pragma warning restore
  51. #endif