public static byte[] DecodeCheck(VersionByte versionByte, string encoded) { byte[] decoded = Base32Encoding.ToBytes(encoded); byte decodedVersionByte = decoded[0]; byte[] payload = new byte[decoded.Length - 2]; Array.Copy(decoded, payload, decoded.Length - 2); byte[] data = new byte[payload.Length - 1]; Array.Copy(payload, 1, data, 0, payload.Length - 1); byte[] checksum = new byte[2]; Array.Copy(decoded, decoded.Length - 2, checksum, 0, 2); if (decodedVersionByte != (byte)versionByte) { throw new FormatException("Version byte is invalid"); } byte[] expectedChecksum = StrKey.CalculateChecksum(payload); if (!expectedChecksum.IsIdentical(checksum)) { throw new FormatException("Checksum invalid"); } return(data); }
public static string EncodeCheck(VersionByte versionByte, byte[] data) { var writer = new Generated.ByteWriter(); writer.Write((byte)versionByte); writer.Write(data); byte[] checksum = StrKey.CalculateChecksum(writer.ToArray()); writer.Write(checksum); return(Base32Encoding.ToString(writer.ToArray())); }