5 using Org.BouncyCastle.Math;
10 public class Base58Exception : Exception
12 public Base58Exception()
16 public Base58Exception(string message)
21 public Base58Exception(string message, Exception inner)
22 : base(message, inner)
27 public class AddressTools
29 private const string strDigits = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz";
30 private static readonly BigInteger _base = BigInteger.ValueOf(58);
33 /// Encode a byte sequence as a base58-encoded string
35 /// <param name="bytes">Byte sequence</param>
36 /// <returns>Encoding result</returns>
37 public static string Base58Encode(byte[] input)
39 // TODO: This could be a lot more efficient.
40 var bi = new BigInteger(1, input);
41 var s = new StringBuilder();
42 while (bi.CompareTo(_base) >= 0)
44 var mod = bi.Mod(_base);
45 s.Insert(0, new[] { strDigits[mod.IntValue] });
46 bi = bi.Subtract(mod).Divide(_base);
48 s.Insert(0, new[] { strDigits[bi.IntValue] });
49 // Convert leading zeros too.
50 foreach (var anInput in input)
53 s.Insert(0, new[] { strDigits[0] });
62 /// Encode a byte sequence to a base58-encoded string, including checksum
64 /// <param name="bytes">Byte sequence</param>
65 /// <returns>Base58(data+checksum)</returns>
66 public static string Base58EncodeCheck(byte[] bytes)
68 var dataBytes = new byte[bytes.Length + 4];
69 bytes.CopyTo(dataBytes, 0);
70 var checkSum = Hash256.ComputeRaw256(bytes).Take(4).ToArray();
71 checkSum.CopyTo(dataBytes, dataBytes.Length - 4); // add 4-byte hash check to the end
73 return Base58Encode(dataBytes);
77 /// // Decode a base58-encoded string into byte array
79 /// <param name="strBase58">Base58 data string</param>
80 /// <returns>Byte array</returns>
81 public static byte[] Base58Decode(string input)
83 var bytes = DecodeToBigInteger(input).ToByteArray();
84 // We may have got one more byte than we wanted, if the high bit of the next-to-last byte was not zero. This
85 // is because BigIntegers are represented with twos-compliment notation, thus if the high bit of the last
86 // byte happens to be 1 another 8 zero bits will be added to ensure the number parses as positive. Detect
87 // that case here and chop it off.
88 var stripSignByte = bytes.Length > 1 && bytes[0] == 0 && bytes[1] >= 0x80;
89 // Count the leading zeros, if any.
91 for (var i = 0; input[i] == strDigits[0]; i++)
95 var tmp = new byte[bytes.Length - (stripSignByte ? 1 : 0) + leadingZeros];
96 Array.Copy(bytes, stripSignByte ? 1 : 0, tmp, leadingZeros, tmp.Length - leadingZeros);
100 public static BigInteger DecodeToBigInteger(string input)
102 var bi = BigInteger.ValueOf(0);
103 // Work backwards through the string.
104 for (var i = input.Length - 1; i >= 0; i--)
106 var alphaIndex = strDigits.IndexOf(input[i]);
107 if (alphaIndex == -1)
109 throw new FormatException("Illegal character " + input[i] + " at " + i);
111 bi = bi.Add(BigInteger.ValueOf(alphaIndex).Multiply(_base.Pow(input.Length - 1 - i)));
116 public static byte[] Base58DecodeCheck(string strBase58Check)
118 var rawData = Base58Decode(strBase58Check).ToArray();
120 if (rawData.Length < 4)
122 throw new Base58Exception("Data is too short.");
125 var result = new byte[rawData.Length - 4];
126 var resultCheckSum = new byte[4];
128 Array.Copy(rawData, result, result.Length);
129 Array.Copy(rawData, result.Length, resultCheckSum, 0, 4);
131 var checkSum = Hash256.ComputeRaw256(result).Take(4).ToArray();
133 if (!checkSum.SequenceEqual(resultCheckSum))
135 throw new Base58Exception("Incorrect checksum.");