CombinedHash.cs 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Tls.Crypto;
  5. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Utilities;
  6. namespace Best.HTTP.SecureProtocol.Org.BouncyCastle.Tls
  7. {
  8. /// <summary>A combined hash, which implements md5(m) || sha1(m).</summary>
  9. public class CombinedHash
  10. : TlsHash
  11. {
  12. protected readonly TlsContext m_context;
  13. protected readonly TlsCrypto m_crypto;
  14. protected readonly TlsHash m_md5;
  15. protected readonly TlsHash m_sha1;
  16. internal CombinedHash(TlsContext context, TlsHash md5, TlsHash sha1)
  17. {
  18. this.m_context = context;
  19. this.m_crypto = context.Crypto;
  20. this.m_md5 = md5;
  21. this.m_sha1 = sha1;
  22. }
  23. public CombinedHash(TlsCrypto crypto)
  24. {
  25. this.m_crypto = crypto;
  26. this.m_md5 = crypto.CreateHash(CryptoHashAlgorithm.md5);
  27. this.m_sha1 = crypto.CreateHash(CryptoHashAlgorithm.sha1);
  28. }
  29. public CombinedHash(CombinedHash t)
  30. {
  31. this.m_context = t.m_context;
  32. this.m_crypto = t.m_crypto;
  33. this.m_md5 = t.m_md5.CloneHash();
  34. this.m_sha1 = t.m_sha1.CloneHash();
  35. }
  36. public virtual void Update(byte[] input, int inOff, int len)
  37. {
  38. m_md5.Update(input, inOff, len);
  39. m_sha1.Update(input, inOff, len);
  40. }
  41. #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER || UNITY_2021_2_OR_NEWER
  42. public void Update(ReadOnlySpan<byte> input)
  43. {
  44. m_md5.Update(input);
  45. m_sha1.Update(input);
  46. }
  47. #endif
  48. public virtual byte[] CalculateHash()
  49. {
  50. if (null != m_context && TlsUtilities.IsSsl(m_context))
  51. {
  52. Ssl3Utilities.CompleteCombinedHash(m_context, m_md5, m_sha1);
  53. }
  54. return Arrays.Concatenate(m_md5.CalculateHash(), m_sha1.CalculateHash());
  55. }
  56. public virtual TlsHash CloneHash()
  57. {
  58. return new CombinedHash(this);
  59. }
  60. public virtual void Reset()
  61. {
  62. m_md5.Reset();
  63. m_sha1.Reset();
  64. }
  65. }
  66. }
  67. #pragma warning restore
  68. #endif