HexTranslator.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. namespace BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Encoders
  5. {
  6. /// <summary>
  7. /// A hex translator.
  8. /// </summary>
  9. public class HexTranslator : ITranslator
  10. {
  11. private static readonly byte[] hexTable =
  12. {
  13. (byte)'0', (byte)'1', (byte)'2', (byte)'3', (byte)'4', (byte)'5', (byte)'6', (byte)'7',
  14. (byte)'8', (byte)'9', (byte)'a', (byte)'b', (byte)'c', (byte)'d', (byte)'e', (byte)'f'
  15. };
  16. /// <summary>
  17. /// Return encoded block size.
  18. /// </summary>
  19. /// <returns>2</returns>
  20. public int GetEncodedBlockSize()
  21. {
  22. return 2;
  23. }
  24. /// <summary>
  25. /// Encode some data.
  26. /// </summary>
  27. /// <param name="input">Input data array.</param>
  28. /// <param name="inOff">Start position within input data array.</param>
  29. /// <param name="length">The amount of data to process.</param>
  30. /// <param name="outBytes">The output data array.</param>
  31. /// <param name="outOff">The offset within the output data array to start writing from.</param>
  32. /// <returns>Amount of data encoded.</returns>
  33. public int Encode(
  34. byte[] input,
  35. int inOff,
  36. int length,
  37. byte[] outBytes,
  38. int outOff)
  39. {
  40. for (int i = 0, j = 0; i < length; i++, j += 2)
  41. {
  42. outBytes[outOff + j] = hexTable[(input[inOff] >> 4) & 0x0f];
  43. outBytes[outOff + j + 1] = hexTable[input[inOff] & 0x0f];
  44. inOff++;
  45. }
  46. return length * 2;
  47. }
  48. /// <summary>
  49. /// Returns the decoded block size.
  50. /// </summary>
  51. /// <returns>1</returns>
  52. public int GetDecodedBlockSize()
  53. {
  54. return 1;
  55. }
  56. /// <summary>
  57. /// Decode data from a byte array.
  58. /// </summary>
  59. /// <param name="input">The input data array.</param>
  60. /// <param name="inOff">Start position within input data array.</param>
  61. /// <param name="length">The amounty of data to process.</param>
  62. /// <param name="outBytes">The output data array.</param>
  63. /// <param name="outOff">The position within the output data array to start writing from.</param>
  64. /// <returns>The amount of data written.</returns>
  65. public int Decode(
  66. byte[] input,
  67. int inOff,
  68. int length,
  69. byte[] outBytes,
  70. int outOff)
  71. {
  72. int halfLength = length / 2;
  73. byte left, right;
  74. for (int i = 0; i < halfLength; i++)
  75. {
  76. left = input[inOff + i * 2];
  77. right = input[inOff + i * 2 + 1];
  78. if (left < (byte)'a')
  79. {
  80. outBytes[outOff] = (byte)((left - '0') << 4);
  81. }
  82. else
  83. {
  84. outBytes[outOff] = (byte)((left - 'a' + 10) << 4);
  85. }
  86. if (right < (byte)'a')
  87. {
  88. outBytes[outOff] += (byte)(right - '0');
  89. }
  90. else
  91. {
  92. outBytes[outOff] += (byte)(right - 'a' + 10);
  93. }
  94. outOff++;
  95. }
  96. return halfLength;
  97. }
  98. }
  99. }
  100. #pragma warning restore
  101. #endif