-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathBase58.cs
79 lines (69 loc) · 2.51 KB
/
Base58.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
using System;
using System.Linq;
using System.Numerics;
namespace ElectricShimmer
{
public static class Base58
{
private const string DIGITS = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz";
public static string Encode(byte[] data)
{
try
{
// Decode byte[] to BigInteger
var intData = data.Aggregate<byte, BigInteger>(0, (current, t) => current * 256 + t);
// Encode BigInteger to Base58 string
var result = string.Empty;
while (intData > 0)
{
var remainder = (int)(intData % 58);
intData /= 58;
result = DIGITS[remainder] + result;
}
// Append `1` for each leading 0 byte
for (var i = 0; i < data.Length && data[i] == 0; i++)
{
result = '1' + result;
}
return result;
}
catch (Exception exc)
{
Log.Write(exc.Message, LogLevel.EXCEPTION);
return null;
}
}
public static byte[] Decode(string data)
{
try
{
// Decode Base58 string to BigInteger
BigInteger intData = 0;
for (var i = 0; i < data.Length; i++)
{
var digit = DIGITS.IndexOf(data[i]); //Slow
if (digit < 0)
{
throw new FormatException(string.Format("Invalid Base58 character `{0}` at position {1}", data[i], i));
}
intData = intData * 58 + digit;
}
// Encode BigInteger to byte[]
// Leading zero bytes get encoded as leading `1` characters
var leadingZeroCount = data.TakeWhile(c => c == '1').Count();
var leadingZeros = Enumerable.Repeat((byte)0, leadingZeroCount);
var bytesWithoutLeadingZeros =
intData.ToByteArray()
.Reverse()// to big endian
.SkipWhile(b => b == 0);//strip sign byte
var result = leadingZeros.Concat(bytesWithoutLeadingZeros).ToArray();
return result;
}
catch (Exception exc)
{
Log.Write(exc.Message, LogLevel.EXCEPTION);
return null;
}
}
}
}