Strings.cs 2.7 KB

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