Base64.cs 3.3 KB

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