UrlBase64.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. using System.IO;
  5. namespace BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Encoders
  6. {
  7. /**
  8. * Convert binary data to and from UrlBase64 encoding. This is identical to
  9. * Base64 encoding, except that the padding character is "." and the other
  10. * non-alphanumeric characters are "-" and "_" instead of "+" and "/".
  11. * <p>
  12. * The purpose of UrlBase64 encoding is to provide a compact encoding of binary
  13. * data that is safe for use as an URL parameter. Base64 encoding does not
  14. * produce encoded values that are safe for use in URLs, since "/" can be
  15. * interpreted as a path delimiter; "+" is the encoded form of a space; and
  16. * "=" is used to separate a name from the corresponding value in an URL
  17. * parameter.
  18. * </p>
  19. */
  20. public class UrlBase64
  21. {
  22. private static readonly IEncoder encoder = new UrlBase64Encoder();
  23. /**
  24. * Encode the input data producing a URL safe base 64 encoded byte array.
  25. *
  26. * @return a byte array containing the URL safe base 64 encoded data.
  27. */
  28. public static byte[] Encode(
  29. byte[] data)
  30. {
  31. MemoryStream bOut = new MemoryStream();
  32. try
  33. {
  34. encoder.Encode(data, 0, data.Length, bOut);
  35. }
  36. catch (IOException e)
  37. {
  38. throw new Exception("exception encoding URL safe base64 string: " + e.Message, e);
  39. }
  40. return bOut.ToArray();
  41. }
  42. /**
  43. * Encode the byte data writing it to the given output stream.
  44. *
  45. * @return the number of bytes produced.
  46. */
  47. public static int Encode(
  48. byte[] data,
  49. Stream outStr)
  50. {
  51. return encoder.Encode(data, 0, data.Length, outStr);
  52. }
  53. /**
  54. * Decode the URL safe base 64 encoded input data - white space will be ignored.
  55. *
  56. * @return a byte array representing the decoded data.
  57. */
  58. public static byte[] Decode(
  59. byte[] data)
  60. {
  61. MemoryStream bOut = new MemoryStream();
  62. try
  63. {
  64. encoder.Decode(data, 0, data.Length, bOut);
  65. }
  66. catch (IOException e)
  67. {
  68. throw new Exception("exception decoding URL safe base64 string: " + e.Message, e);
  69. }
  70. return bOut.ToArray();
  71. }
  72. /**
  73. * decode the URL safe base 64 encoded byte data writing it to the given output stream,
  74. * whitespace characters will be ignored.
  75. *
  76. * @return the number of bytes produced.
  77. */
  78. public static int Decode(
  79. byte[] data,
  80. Stream outStr)
  81. {
  82. return encoder.Decode(data, 0, data.Length, outStr);
  83. }
  84. /**
  85. * decode the URL safe base 64 encoded string data - whitespace will be ignored.
  86. *
  87. * @return a byte array representing the decoded data.
  88. */
  89. public static byte[] Decode(
  90. string data)
  91. {
  92. MemoryStream bOut = new MemoryStream();
  93. try
  94. {
  95. encoder.DecodeString(data, bOut);
  96. }
  97. catch (IOException e)
  98. {
  99. throw new Exception("exception decoding URL safe base64 string: " + e.Message, e);
  100. }
  101. return bOut.ToArray();
  102. }
  103. /**
  104. * Decode the URL safe base 64 encoded string data writing it to the given output stream,
  105. * whitespace characters will be ignored.
  106. *
  107. * @return the number of bytes produced.
  108. */
  109. public static int Decode(
  110. string data,
  111. Stream outStr)
  112. {
  113. return encoder.DecodeString(data, outStr);
  114. }
  115. }
  116. }
  117. #pragma warning restore
  118. #endif