CombinedHash.cs 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. using BestHTTP.SecureProtocol.Org.BouncyCastle.Tls.Crypto;
  5. using BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities;
  6. namespace BestHTTP.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. public virtual byte[] CalculateHash()
  42. {
  43. if (null != m_context && TlsUtilities.IsSsl(m_context))
  44. {
  45. Ssl3Utilities.CompleteCombinedHash(m_context, m_md5, m_sha1);
  46. }
  47. return Arrays.Concatenate(m_md5.CalculateHash(), m_sha1.CalculateHash());
  48. }
  49. public virtual TlsHash CloneHash()
  50. {
  51. return new CombinedHash(this);
  52. }
  53. public virtual void Reset()
  54. {
  55. m_md5.Reset();
  56. m_sha1.Reset();
  57. }
  58. }
  59. }
  60. #pragma warning restore
  61. #endif