BasicAlphabetMapper.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. using System.Collections;
  5. using BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities;
  6. namespace BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto.Utilities
  7. {
  8. /**
  9. * A basic alphabet mapper that just creates a mapper based on the
  10. * passed in array of characters.
  11. */
  12. public class BasicAlphabetMapper
  13. : IAlphabetMapper
  14. {
  15. private readonly IDictionary indexMap = BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.CreateHashtable();
  16. private readonly IDictionary charMap = BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.CreateHashtable();
  17. /**
  18. * Base constructor.
  19. *
  20. * @param alphabet a string of characters making up the alphabet.
  21. */
  22. public BasicAlphabetMapper(string alphabet) :
  23. this(alphabet.ToCharArray())
  24. {
  25. }
  26. /**
  27. * Base constructor.
  28. *
  29. * @param alphabet an array of characters making up the alphabet.
  30. */
  31. public BasicAlphabetMapper(char[] alphabet)
  32. {
  33. for (int i = 0; i != alphabet.Length; i++)
  34. {
  35. if (indexMap.Contains(alphabet[i]))
  36. {
  37. throw new ArgumentException("duplicate key detected in alphabet: " + alphabet[i]);
  38. }
  39. indexMap.Add(alphabet[i], i);
  40. charMap.Add(i, alphabet[i]);
  41. }
  42. }
  43. public int Radix
  44. {
  45. get { return indexMap.Count; }
  46. }
  47. public byte[] ConvertToIndexes(char[] input)
  48. {
  49. byte[] outBuf;
  50. if (indexMap.Count <= 256)
  51. {
  52. outBuf = new byte[input.Length];
  53. for (int i = 0; i != input.Length; i++)
  54. {
  55. outBuf[i] = (byte)(int)indexMap[input[i]];
  56. }
  57. }
  58. else
  59. {
  60. outBuf = new byte[input.Length * 2];
  61. for (int i = 0; i != input.Length; i++)
  62. {
  63. int idx = (int)indexMap[input[i]];
  64. outBuf[i * 2] = (byte)((idx >> 8) & 0xff);
  65. outBuf[i * 2 + 1] = (byte)(idx & 0xff);
  66. }
  67. }
  68. return outBuf;
  69. }
  70. public char[] ConvertToChars(byte[] input)
  71. {
  72. char[] outBuf;
  73. if (charMap.Count <= 256)
  74. {
  75. outBuf = new char[input.Length];
  76. for (int i = 0; i != input.Length; i++)
  77. {
  78. outBuf[i] = (char)charMap[input[i] & 0xff];
  79. }
  80. }
  81. else
  82. {
  83. if ((input.Length & 0x1) != 0)
  84. {
  85. throw new ArgumentException("two byte radix and input string odd.Length");
  86. }
  87. outBuf = new char[input.Length / 2];
  88. for (int i = 0; i != input.Length; i += 2)
  89. {
  90. outBuf[i / 2] = (char)charMap[((input[i] << 8) & 0xff00) | (input[i + 1] & 0xff)];
  91. }
  92. }
  93. return outBuf;
  94. }
  95. }
  96. }
  97. #pragma warning restore
  98. #endif