예제 #1
0
        /// <summary>
        /// The checksum is a substring of the binary representation of the SHA256 hash of entropy.
        /// For every four bytes of entropy, one additional bit of the hash is used.
        /// </summary>
        /// <param name="entropy"></param>
        /// <returns></returns>
        private static string GetChecksum(ReadOnlyByteSequence entropy)
        {
            var hash = entropy.Sha256();
            var bits = (int)entropy.Length * 8;
            var cs   = bits / 32;

            var sb = new StringBuilder();

            foreach (var b in hash.Span)
            {
                sb.Append(Convert.ToString(b, 2).PadLeft(8, '0'));
                cs -= 8;
                if (cs <= 0)
                {
                    break;
                }
            }
            if (cs < 0)
            {
                sb.Length += cs;
            }

            return(sb.ToString());
        }
        /// <summary>
        /// Computes RIPEMD160 of SHA256 of data: RIPEMD160(SHA256(data))
        /// </summary>
        /// <param name="data">Input: bytes to be hashed.</param>
        /// <param name="hash">Output: RIPEMD160 of SHA256 of data.</param>
        public static void Hash160(this ReadOnlyByteSequence data, ByteSpan hash)
        {
            var h = data.Sha256();

            Ripemd160(h.Span, hash);
        }