Hex.cs 3.4 KB

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