| | | 1 | | using System.IO; |
| | | 2 | | using System.Text; |
| | | 3 | | |
| | | 4 | | namespace Renci.SshNet.Security.Org.BouncyCastle.Utilities.Encoders |
| | | 5 | | { |
| | | 6 | | /// <summary> |
| | | 7 | | /// Class to decode and encode Hex. |
| | | 8 | | /// </summary> |
| | | 9 | | internal sealed class Hex |
| | | 10 | | { |
| | 1 | 11 | | private static readonly HexEncoder encoder = new HexEncoder(); |
| | | 12 | | |
| | 0 | 13 | | private Hex() |
| | 0 | 14 | | { |
| | 0 | 15 | | } |
| | | 16 | | |
| | | 17 | | public static string ToHexString( |
| | | 18 | | byte[] data) |
| | 0 | 19 | | { |
| | 0 | 20 | | return ToHexString(data, 0, data.Length); |
| | 0 | 21 | | } |
| | | 22 | | |
| | | 23 | | public static string ToHexString( |
| | | 24 | | byte[] data, |
| | | 25 | | int off, |
| | | 26 | | int length) |
| | 0 | 27 | | { |
| | 0 | 28 | | byte[] hex = Encode(data, off, length); |
| | 0 | 29 | | return Encoding.UTF8.GetString(hex, 0, hex.Length); |
| | 0 | 30 | | } |
| | | 31 | | |
| | | 32 | | /** |
| | | 33 | | * encode the input data producing a Hex encoded byte array. |
| | | 34 | | * |
| | | 35 | | * @return a byte array containing the Hex encoded data. |
| | | 36 | | */ |
| | | 37 | | public static byte[] Encode( |
| | | 38 | | byte[] data) |
| | 0 | 39 | | { |
| | 0 | 40 | | return Encode(data, 0, data.Length); |
| | 0 | 41 | | } |
| | | 42 | | |
| | | 43 | | /** |
| | | 44 | | * encode the input data producing a Hex encoded byte array. |
| | | 45 | | * |
| | | 46 | | * @return a byte array containing the Hex encoded data. |
| | | 47 | | */ |
| | | 48 | | public static byte[] Encode( |
| | | 49 | | byte[] data, |
| | | 50 | | int off, |
| | | 51 | | int length) |
| | 0 | 52 | | { |
| | 0 | 53 | | MemoryStream bOut = new MemoryStream(length * 2); |
| | | 54 | | |
| | 0 | 55 | | encoder.Encode(data, off, length, bOut); |
| | | 56 | | |
| | 0 | 57 | | return bOut.ToArray(); |
| | 0 | 58 | | } |
| | | 59 | | |
| | | 60 | | /** |
| | | 61 | | * Hex encode the byte data writing it to the given output stream. |
| | | 62 | | * |
| | | 63 | | * @return the number of bytes produced. |
| | | 64 | | */ |
| | | 65 | | public static int Encode( |
| | | 66 | | byte[] data, |
| | | 67 | | Stream outStream) |
| | 0 | 68 | | { |
| | 0 | 69 | | return encoder.Encode(data, 0, data.Length, outStream); |
| | 0 | 70 | | } |
| | | 71 | | |
| | | 72 | | /** |
| | | 73 | | * Hex encode the byte data writing it to the given output stream. |
| | | 74 | | * |
| | | 75 | | * @return the number of bytes produced. |
| | | 76 | | */ |
| | | 77 | | public static int Encode( |
| | | 78 | | byte[] data, |
| | | 79 | | int off, |
| | | 80 | | int length, |
| | | 81 | | Stream outStream) |
| | 0 | 82 | | { |
| | 0 | 83 | | return encoder.Encode(data, off, length, outStream); |
| | 0 | 84 | | } |
| | | 85 | | |
| | | 86 | | /** |
| | | 87 | | * decode the Hex encoded input data. It is assumed the input data is valid. |
| | | 88 | | * |
| | | 89 | | * @return a byte array representing the decoded data. |
| | | 90 | | */ |
| | | 91 | | public static byte[] Decode( |
| | | 92 | | byte[] data) |
| | 0 | 93 | | { |
| | 0 | 94 | | MemoryStream bOut = new MemoryStream((data.Length + 1) / 2); |
| | | 95 | | |
| | 0 | 96 | | encoder.Decode(data, 0, data.Length, bOut); |
| | | 97 | | |
| | 0 | 98 | | return bOut.ToArray(); |
| | 0 | 99 | | } |
| | | 100 | | |
| | | 101 | | /** |
| | | 102 | | * decode the Hex encoded string data - whitespace will be ignored. |
| | | 103 | | * |
| | | 104 | | * @return a byte array representing the decoded data. |
| | | 105 | | */ |
| | | 106 | | public static byte[] Decode( |
| | | 107 | | string data) |
| | 18 | 108 | | { |
| | 18 | 109 | | MemoryStream bOut = new MemoryStream((data.Length + 1) / 2); |
| | | 110 | | |
| | 18 | 111 | | encoder.DecodeString(data, bOut); |
| | | 112 | | |
| | 18 | 113 | | return bOut.ToArray(); |
| | 18 | 114 | | } |
| | | 115 | | |
| | | 116 | | /** |
| | | 117 | | * decode the Hex encoded string data writing it to the given output stream, |
| | | 118 | | * whitespace characters will be ignored. |
| | | 119 | | * |
| | | 120 | | * @return the number of bytes produced. |
| | | 121 | | */ |
| | | 122 | | public static int Decode( |
| | | 123 | | string data, |
| | | 124 | | Stream outStream) |
| | 0 | 125 | | { |
| | 0 | 126 | | return encoder.DecodeString(data, outStream); |
| | 0 | 127 | | } |
| | | 128 | | } |
| | | 129 | | } |