simplepack-dotnet/SimplePack/SimplePack.cs

41 lines
1.3 KiB
C#
Raw Permalink Normal View History

2020-05-27 06:52:48 +00:00
using Base58Check;
namespace simplepack
{
public class SimplePack
{
private string head;
private string foot;
2020-05-27 08:00:18 +00:00
public SimplePack(string header, string footer){
2020-05-27 06:52:48 +00:00
// Specify human meaningful header/footer string
head = header;
foot = footer;
2020-05-27 06:52:48 +00:00
// header footer need to be >= 1 len
if (header.Length == 0 | footer.Length == 0){
throw new InvalidSimplePackHeader();
}
}
public byte[] decode(string data){
2020-05-27 06:52:48 +00:00
// Not valid if it doesn't have head/foot
if (! data.Contains(head) | ! data.Contains(foot)){ throw new InvalidSimplePackHeader();}
string base58Data = "";
2020-05-27 06:52:48 +00:00
// Extract the encoded part without header/footer
for (int i = head.Length; i < data.Length - foot.Length; i++){
base58Data += data[i];
}
2020-05-27 06:52:48 +00:00
// Will raise System.FormatException if checksum is not valid (or invalid footer/header)
return Base58CheckEncoding.Decode(base58Data);
}
public string encode(byte[] data){
// OutOfMemoryException may be thrown for large payloads
return head + Base58CheckEncoding.Encode(data) + foot;
}
}
}