Strings.cs 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. using System.Text;
  5. namespace BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities
  6. {
  7. /// <summary> General string utilities.</summary>
  8. public abstract class Strings
  9. {
  10. internal static bool IsOneOf(string s, params string[] candidates)
  11. {
  12. foreach (string candidate in candidates)
  13. {
  14. if (s == candidate)
  15. return true;
  16. }
  17. return false;
  18. }
  19. public static string FromByteArray(
  20. byte[] bs)
  21. {
  22. char[] cs = new char[bs.Length];
  23. for (int i = 0; i < cs.Length; ++i)
  24. {
  25. cs[i] = Convert.ToChar(bs[i]);
  26. }
  27. return new string(cs);
  28. }
  29. public static byte[] ToByteArray(
  30. char[] cs)
  31. {
  32. byte[] bs = new byte[cs.Length];
  33. for (int i = 0; i < bs.Length; ++i)
  34. {
  35. bs[i] = Convert.ToByte(cs[i]);
  36. }
  37. return bs;
  38. }
  39. public static byte[] ToByteArray(
  40. string s)
  41. {
  42. byte[] bs = new byte[s.Length];
  43. for (int i = 0; i < bs.Length; ++i)
  44. {
  45. bs[i] = Convert.ToByte(s[i]);
  46. }
  47. return bs;
  48. }
  49. public static string FromAsciiByteArray(
  50. byte[] bytes)
  51. {
  52. #if SILVERLIGHT || PORTABLE || NETFX_CORE
  53. // TODO Check for non-ASCII bytes in input?
  54. return Encoding.UTF8.GetString(bytes, 0, bytes.Length);
  55. #else
  56. return Encoding.ASCII.GetString(bytes, 0, bytes.Length);
  57. #endif
  58. }
  59. public static byte[] ToAsciiByteArray(
  60. char[] cs)
  61. {
  62. #if SILVERLIGHT || PORTABLE || NETFX_CORE
  63. // TODO Check for non-ASCII characters in input?
  64. return Encoding.UTF8.GetBytes(cs);
  65. #else
  66. return Encoding.ASCII.GetBytes(cs);
  67. #endif
  68. }
  69. public static byte[] ToAsciiByteArray(
  70. string s)
  71. {
  72. #if SILVERLIGHT || PORTABLE || NETFX_CORE
  73. // TODO Check for non-ASCII characters in input?
  74. return Encoding.UTF8.GetBytes(s);
  75. #else
  76. return Encoding.ASCII.GetBytes(s);
  77. #endif
  78. }
  79. public static string FromUtf8ByteArray(
  80. byte[] bytes)
  81. {
  82. return Encoding.UTF8.GetString(bytes, 0, bytes.Length);
  83. }
  84. public static byte[] ToUtf8ByteArray(
  85. char[] cs)
  86. {
  87. return Encoding.UTF8.GetBytes(cs);
  88. }
  89. public static byte[] ToUtf8ByteArray(
  90. string s)
  91. {
  92. return Encoding.UTF8.GetBytes(s);
  93. }
  94. }
  95. }
  96. #pragma warning restore
  97. #endif