Base64.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. public sealed class Base64
  9. {
  10. private Base64()
  11. {
  12. }
  13. public static string ToBase64String(
  14. byte[] data)
  15. {
  16. return Convert.ToBase64String(data, 0, data.Length);
  17. }
  18. public static string ToBase64String(
  19. byte[] data,
  20. int off,
  21. int length)
  22. {
  23. return Convert.ToBase64String(data, off, length);
  24. }
  25. /**
  26. * encode the input data producing a base 64 encoded byte array.
  27. *
  28. * @return a byte array containing the base 64 encoded data.
  29. */
  30. public static byte[] Encode(
  31. byte[] data)
  32. {
  33. return Encode(data, 0, data.Length);
  34. }
  35. /**
  36. * encode the input data producing a base 64 encoded byte array.
  37. *
  38. * @return a byte array containing the base 64 encoded data.
  39. */
  40. public static byte[] Encode(
  41. byte[] data,
  42. int off,
  43. int length)
  44. {
  45. string s = Convert.ToBase64String(data, off, length);
  46. return Strings.ToAsciiByteArray(s);
  47. }
  48. /**
  49. * Encode the byte data to base 64 writing it to the given output stream.
  50. *
  51. * @return the number of bytes produced.
  52. */
  53. public static int Encode(
  54. byte[] data,
  55. Stream outStream)
  56. {
  57. byte[] encoded = Encode(data);
  58. outStream.Write(encoded, 0, encoded.Length);
  59. return encoded.Length;
  60. }
  61. /**
  62. * Encode the byte data to base 64 writing it to the given output stream.
  63. *
  64. * @return the number of bytes produced.
  65. */
  66. public static int Encode(
  67. byte[] data,
  68. int off,
  69. int length,
  70. Stream outStream)
  71. {
  72. byte[] encoded = Encode(data, off, length);
  73. outStream.Write(encoded, 0, encoded.Length);
  74. return encoded.Length;
  75. }
  76. /**
  77. * decode the base 64 encoded input data. It is assumed the input data is valid.
  78. *
  79. * @return a byte array representing the decoded data.
  80. */
  81. public static byte[] Decode(
  82. byte[] data)
  83. {
  84. string s = Strings.FromAsciiByteArray(data);
  85. return Convert.FromBase64String(s);
  86. }
  87. /**
  88. * decode the base 64 encoded string data - whitespace will be ignored.
  89. *
  90. * @return a byte array representing the decoded data.
  91. */
  92. public static byte[] Decode(
  93. string data)
  94. {
  95. return Convert.FromBase64String(data);
  96. }
  97. /**
  98. * decode the base 64 encoded string data writing it to the given output stream,
  99. * whitespace characters will be ignored.
  100. *
  101. * @return the number of bytes produced.
  102. */
  103. public static int Decode(
  104. string data,
  105. Stream outStream)
  106. {
  107. byte[] decoded = Decode(data);
  108. outStream.Write(decoded, 0, decoded.Length);
  109. return decoded.Length;
  110. }
  111. }
  112. }
  113. #pragma warning restore
  114. #endif