NullDigest.cs 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. using System.IO;
  5. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Utilities.IO;
  6. namespace Best.HTTP.SecureProtocol.Org.BouncyCastle.Crypto.Digests
  7. {
  8. public class NullDigest : IDigest
  9. {
  10. private readonly MemoryStream bOut = new MemoryStream();
  11. public string AlgorithmName
  12. {
  13. get { return "NULL"; }
  14. }
  15. public int GetByteLength()
  16. {
  17. // TODO Is this okay?
  18. return 0;
  19. }
  20. public int GetDigestSize()
  21. {
  22. return Convert.ToInt32(bOut.Length);
  23. }
  24. public void Update(byte b)
  25. {
  26. bOut.WriteByte(b);
  27. }
  28. public void BlockUpdate(byte[] inBytes, int inOff, int len)
  29. {
  30. bOut.Write(inBytes, inOff, len);
  31. }
  32. #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER || UNITY_2021_2_OR_NEWER
  33. public void BlockUpdate(ReadOnlySpan<byte> input)
  34. {
  35. bOut.Write(input);
  36. }
  37. #endif
  38. public int DoFinal(byte[] outBytes, int outOff)
  39. {
  40. try
  41. {
  42. byte[] data = bOut.GetBuffer();
  43. int length = Convert.ToInt32(bOut.Length);
  44. Array.Copy(data, 0, outBytes, outOff, length);
  45. return length;
  46. }
  47. finally
  48. {
  49. Reset();
  50. }
  51. }
  52. #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER || UNITY_2021_2_OR_NEWER
  53. public int DoFinal(Span<byte> output)
  54. {
  55. try
  56. {
  57. byte[] data = bOut.GetBuffer();
  58. int length = Convert.ToInt32(bOut.Length);
  59. data.AsSpan(0, length).CopyTo(output);
  60. return length;
  61. }
  62. finally
  63. {
  64. Reset();
  65. }
  66. }
  67. #endif
  68. public void Reset()
  69. {
  70. bOut.SetLength(0);
  71. }
  72. }
  73. }
  74. #pragma warning restore
  75. #endif