Hex.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. using System.IO;
  5. using System.Text;
  6. namespace BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Encoders
  7. {
  8. /// <summary>
  9. /// Class to decode and encode Hex.
  10. /// </summary>
  11. public sealed class Hex
  12. {
  13. private static readonly IEncoder encoder = new HexEncoder();
  14. private Hex()
  15. {
  16. }
  17. public static string ToHexString(
  18. byte[] data)
  19. {
  20. return ToHexString(data, 0, data.Length);
  21. }
  22. public static string ToHexString(
  23. byte[] data,
  24. int off,
  25. int length)
  26. {
  27. byte[] hex = Encode(data, off, length);
  28. return Strings.FromAsciiByteArray(hex);
  29. }
  30. /**
  31. * encode the input data producing a Hex encoded byte array.
  32. *
  33. * @return a byte array containing the Hex encoded data.
  34. */
  35. public static byte[] Encode(
  36. byte[] data)
  37. {
  38. return Encode(data, 0, data.Length);
  39. }
  40. /**
  41. * encode the input data producing a Hex encoded byte array.
  42. *
  43. * @return a byte array containing the Hex encoded data.
  44. */
  45. public static byte[] Encode(
  46. byte[] data,
  47. int off,
  48. int length)
  49. {
  50. MemoryStream bOut = new MemoryStream(length * 2);
  51. encoder.Encode(data, off, length, bOut);
  52. return bOut.ToArray();
  53. }
  54. /**
  55. * Hex encode the byte data writing it to the given output stream.
  56. *
  57. * @return the number of bytes produced.
  58. */
  59. public static int Encode(
  60. byte[] data,
  61. Stream outStream)
  62. {
  63. return encoder.Encode(data, 0, data.Length, outStream);
  64. }
  65. /**
  66. * Hex encode the byte data writing it to the given output stream.
  67. *
  68. * @return the number of bytes produced.
  69. */
  70. public static int Encode(
  71. byte[] data,
  72. int off,
  73. int length,
  74. Stream outStream)
  75. {
  76. return encoder.Encode(data, off, length, outStream);
  77. }
  78. /**
  79. * decode the Hex encoded input data. It is assumed the input data is valid.
  80. *
  81. * @return a byte array representing the decoded data.
  82. */
  83. public static byte[] Decode(
  84. byte[] data)
  85. {
  86. MemoryStream bOut = new MemoryStream((data.Length + 1) / 2);
  87. encoder.Decode(data, 0, data.Length, bOut);
  88. return bOut.ToArray();
  89. }
  90. /**
  91. * decode the Hex encoded string data - whitespace will be ignored.
  92. *
  93. * @return a byte array representing the decoded data.
  94. */
  95. public static byte[] Decode(
  96. string data)
  97. {
  98. MemoryStream bOut = new MemoryStream((data.Length + 1) / 2);
  99. encoder.DecodeString(data, bOut);
  100. return bOut.ToArray();
  101. }
  102. /**
  103. * decode the Hex encoded string data writing it to the given output stream,
  104. * whitespace characters will be ignored.
  105. *
  106. * @return the number of bytes produced.
  107. */
  108. public static int Decode(
  109. string data,
  110. Stream outStream)
  111. {
  112. return encoder.DecodeString(data, outStream);
  113. }
  114. }
  115. }
  116. #pragma warning restore
  117. #endif