XofUtils.cs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Utilities;
  5. namespace Best.HTTP.SecureProtocol.Org.BouncyCastle.Crypto.Digests
  6. {
  7. internal class XofUtilities
  8. {
  9. internal static byte[] LeftEncode(long strLen)
  10. {
  11. byte n = 1;
  12. long v = strLen;
  13. while ((v >>= 8) != 0)
  14. {
  15. n++;
  16. }
  17. byte[] b = new byte[n + 1];
  18. b[0] = n;
  19. for (int i = 1; i <= n; i++)
  20. {
  21. b[i] = (byte)(strLen >> (8 * (n - i)));
  22. }
  23. return b;
  24. }
  25. #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER || UNITY_2021_2_OR_NEWER
  26. internal static int LeftEncode(long length, Span<byte> lengthEncoding)
  27. {
  28. byte n = 1;
  29. long v = length;
  30. while ((v >>= 8) != 0)
  31. {
  32. n++;
  33. }
  34. lengthEncoding[0] = n;
  35. for (int i = 1; i <= n; i++)
  36. {
  37. lengthEncoding[i] = (byte)(length >> (8 * (n - i)));
  38. }
  39. return 1 + n;
  40. }
  41. #endif
  42. internal static byte[] RightEncode(long strLen)
  43. {
  44. byte n = 1;
  45. long v = strLen;
  46. while ((v >>= 8) != 0)
  47. {
  48. n++;
  49. }
  50. byte[] b = new byte[n + 1];
  51. b[n] = n;
  52. for (int i = 0; i < n; i++)
  53. {
  54. b[i] = (byte)(strLen >> (8 * (n - i - 1)));
  55. }
  56. return b;
  57. }
  58. #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER || UNITY_2021_2_OR_NEWER
  59. internal static int RightEncode(long length, Span<byte> lengthEncoding)
  60. {
  61. byte n = 1;
  62. long v = length;
  63. while ((v >>= 8) != 0)
  64. {
  65. n++;
  66. }
  67. lengthEncoding[n] = n;
  68. for (int i = 0; i < n; i++)
  69. {
  70. lengthEncoding[i] = (byte)(length >> (8 * (n - i - 1)));
  71. }
  72. return n + 1;
  73. }
  74. #endif
  75. internal static byte[] Encode(byte X)
  76. {
  77. return Arrays.Concatenate(LeftEncode(8), new byte[] { X });
  78. }
  79. internal static byte[] Encode(byte[] inBuf, int inOff, int len)
  80. {
  81. if (inBuf.Length == len)
  82. {
  83. return Arrays.Concatenate(LeftEncode(len * 8), inBuf);
  84. }
  85. return Arrays.Concatenate(LeftEncode(len * 8), Arrays.CopyOfRange(inBuf, inOff, inOff + len));
  86. }
  87. #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER || UNITY_2021_2_OR_NEWER
  88. internal static void EncodeTo(IDigest digest, ReadOnlySpan<byte> buf)
  89. {
  90. Span<byte> lengthEncoding = stackalloc byte[9];
  91. int count = LeftEncode(buf.Length * 8, lengthEncoding);
  92. digest.BlockUpdate(lengthEncoding[..count]);
  93. digest.BlockUpdate(buf);
  94. }
  95. #endif
  96. }
  97. }
  98. #pragma warning restore
  99. #endif