BcTlsHmac.cs 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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.Macs;
  5. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Crypto.Parameters;
  6. namespace Best.HTTP.SecureProtocol.Org.BouncyCastle.Tls.Crypto.Impl.BC
  7. {
  8. internal sealed class BcTlsHmac
  9. : TlsHmac
  10. {
  11. private readonly HMac m_hmac;
  12. internal BcTlsHmac(HMac hmac)
  13. {
  14. this.m_hmac = hmac;
  15. }
  16. public void SetKey(byte[] key, int keyOff, int keyLen)
  17. {
  18. m_hmac.Init(new KeyParameter(key, keyOff, keyLen));
  19. }
  20. #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER || UNITY_2021_2_OR_NEWER
  21. public void SetKey(ReadOnlySpan<byte> key)
  22. {
  23. m_hmac.Init(new KeyParameter(key));
  24. }
  25. #endif
  26. public void Update(byte[] input, int inOff, int length)
  27. {
  28. m_hmac.BlockUpdate(input, inOff, length);
  29. }
  30. #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER || UNITY_2021_2_OR_NEWER
  31. public void Update(ReadOnlySpan<byte> input)
  32. {
  33. m_hmac.BlockUpdate(input);
  34. }
  35. #endif
  36. public byte[] CalculateMac()
  37. {
  38. byte[] rv = new byte[m_hmac.GetMacSize()];
  39. m_hmac.DoFinal(rv, 0);
  40. return rv;
  41. }
  42. public void CalculateMac(byte[] output, int outOff)
  43. {
  44. m_hmac.DoFinal(output, outOff);
  45. }
  46. public int InternalBlockSize
  47. {
  48. get { return m_hmac.GetUnderlyingDigest().GetByteLength(); }
  49. }
  50. public int MacLength
  51. {
  52. get { return m_hmac.GetMacSize(); }
  53. }
  54. public void Reset()
  55. {
  56. m_hmac.Reset();
  57. }
  58. }
  59. }
  60. #pragma warning restore
  61. #endif