XofUtils.cs 1.6 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.Utilities;
  5. namespace BestHTTP.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. internal static byte[] RightEncode(long strLen)
  26. {
  27. byte n = 1;
  28. long v = strLen;
  29. while ((v >>= 8) != 0)
  30. {
  31. n++;
  32. }
  33. byte[] b = new byte[n + 1];
  34. b[n] = n;
  35. for (int i = 0; i < n; i++)
  36. {
  37. b[i] = (byte)(strLen >> (8 * (n - i - 1)));
  38. }
  39. return b;
  40. }
  41. internal static byte[] Encode(byte X)
  42. {
  43. return Arrays.Concatenate(LeftEncode(8), new byte[] { X });
  44. }
  45. internal static byte[] Encode(byte[] inBuf, int inOff, int len)
  46. {
  47. if (inBuf.Length == len)
  48. {
  49. return Arrays.Concatenate(LeftEncode(len * 8), inBuf);
  50. }
  51. return Arrays.Concatenate(LeftEncode(len * 8), Arrays.CopyOfRange(inBuf, inOff, inOff + len));
  52. }
  53. }
  54. }
  55. #pragma warning restore
  56. #endif