45 lines
919 B
C#
45 lines
919 B
C#
using System;
|
|
using System.Text;
|
|
namespace sharp.cryptonote
|
|
{
|
|
public class Base58
|
|
{
|
|
private static string Base58Letters = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz";
|
|
|
|
public static string encode(UInt64 value){
|
|
StringBuilder sb = new StringBuilder();
|
|
|
|
do
|
|
{
|
|
sb.Append( Base58Letters[(int)(value % 58)] );
|
|
value /= 58;
|
|
} while (value != 0);
|
|
|
|
char[] encoded = sb.ToString().PadRight(11,'1').ToCharArray();
|
|
Array.Reverse(encoded);
|
|
|
|
return new string(encoded);
|
|
}
|
|
|
|
public static UInt64 decode(string encoded){
|
|
UInt64 value = 0;
|
|
|
|
char[] chars = encoded.ToCharArray();
|
|
//Array.Reverse(chars);
|
|
|
|
foreach (char ch in chars){
|
|
uint i = (uint)Base58Letters.IndexOf(ch);
|
|
if (i < 0){
|
|
throw new FormatException(String.Format("Illegal Character found in Base58 string: {0}",ch));
|
|
}
|
|
value *= 58;
|
|
value += i;
|
|
}
|
|
|
|
return value;
|
|
}
|
|
|
|
|
|
}
|
|
}
|