-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
EncodeDecode.cs
171 lines (152 loc) · 5.03 KB
/
EncodeDecode.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MechaChat.API
{
public class EncodeDecode
{
public static readonly char[] _alphabet = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz".ToCharArray();
private static int[] _indexes = new int[128];
static EncodeDecode()
{
for (int i = 0; i < _indexes.Length; i++)
{
_indexes[i] = -1;
}
for (int i = 0; i < _alphabet.Length; i++)
{
_indexes[_alphabet[i]] = i;
}
}
public static string Encode(byte[] input)
{
if (0 == input.Length)
{
return String.Empty;
}
input = CopyOfRange(input, 0, input.Length);
// Count leading zeroes.
int zeroCount = 0;
while (zeroCount < input.Length && input[zeroCount] == 0)
{
zeroCount++;
}
// The actual encoding.
byte[] temp = new byte[input.Length * 2];
int j = temp.Length;
int startAt = zeroCount;
while (startAt < input.Length)
{
byte mod = DivMod58(input, startAt);
if (input[startAt] == 0)
{
startAt++;
}
temp[--j] = (byte)_alphabet[mod];
}
// Strip extra '1' if there are some after decoding.
while (j < temp.Length && temp[j] == _alphabet[0])
{
++j;
}
// Add as many leading '1' as there were leading zeros.
while (--zeroCount >= 0)
{
temp[--j] = (byte)_alphabet[0];
}
byte[] output = CopyOfRange(temp, j, temp.Length);
try
{
return Encoding.ASCII.GetString(output);
}
catch (DecoderFallbackException e)
{
Console.WriteLine(e.ToString());
return String.Empty;
}
}
public static byte[] Decode(string input)
{
if (0 == input.Length)
{
return new byte[0];
}
byte[] input58 = new byte[input.Length];
// Transform the String to a base58 byte sequence
for (int i = 0; i < input.Length; i++)
{
char c = input[i];
int digit58 = -1;
if (c >= 0 && c < 128)
{
digit58 = _indexes[c];
}
if (digit58 < 0)
{
throw new ArgumentException("Illegal character " + c + " at " + i);
}
input58[i] = (byte)digit58;
}
// Count leading zeroes
int zeroCount = 0;
while (zeroCount < input58.Length && input58[zeroCount] == 0)
{
zeroCount++;
}
// The encoding
byte[] temp = new byte[input.Length];
int j = temp.Length;
int startAt = zeroCount;
while (startAt < input58.Length)
{
byte mod = DivMod256(input58, startAt);
if (input58[startAt] == 0)
{
++startAt;
}
temp[--j] = mod;
}
// Do no add extra leading zeroes, move j to first non null byte.
while (j < temp.Length && temp[j] == 0)
{
j++;
}
return CopyOfRange(temp, j - zeroCount, temp.Length);
}
static byte DivMod58(byte[] number, int startAt)
{
int remainder = 0;
for (int i = startAt; i < number.Length; i++)
{
int digit256 = (int)number[i] & 0xFF;
int temp = remainder * 256 + digit256;
number[i] = (byte)(temp / 58);
remainder = temp % 58;
}
return (byte)remainder;
}
static byte DivMod256(byte[] number58, int startAt)
{
int remainder = 0;
for (int i = startAt; i < number58.Length; i++)
{
int digit58 = (int)number58[i] & 0xFF;
int temp = remainder * 58 + digit58;
number58[i] = (byte)(temp / 256);
remainder = temp % 256;
}
return (byte)remainder;
}
static byte[] CopyOfRange(byte[] source, int from, int to)
{
byte[] range = new byte[to - from];
for (int i = 0; i < to - from; i++)
{
range[i] = source[from + i];
}
return range;
}
}
}