コード例 #1
0
        internal static string Encode(byte prefixbyte, bool seed, byte[] src)
        {
            if (!IsValidPublicPrefixByte(prefixbyte))
            {
                throw new NATSException("Invalid prefix");
            }

            if (src.Length != 32)
            {
                throw new NATSException("Invalid seed size");
            }

            MemoryStream stream = new MemoryStream();

            if (seed)
            {
                // In order to make this human printable for both bytes, we need to do a little
                // bit manipulation to setup for base32 encoding which takes 5 bits at a time.
                byte b1 = (byte)(PrefixByteSeed | (prefixbyte >> 5));
                byte b2 = (byte)((prefixbyte & 31) << 3);  // 31 = 00011111

                stream.WriteByte(b1);
                stream.WriteByte(b2);
            }
            else
            {
                stream.WriteByte(prefixbyte);
            }

            // write payload
            stream.Write(src, 0, src.Length);

            // Calculate and write crc16 checksum
            byte[] checksum = BitConverter.GetBytes(Crc16.Checksum(stream.ToArray()));
            stream.Write(checksum, 0, checksum.Length);

            return(Base32.Encode(stream.ToArray()));
        }