public static UInt160 Sha1(this ReadOnlyByteSequence data)
        {
            var hash = new UInt160();

            Sha1(data, hash.Span);
            return(hash);
        }
        public static void TransformBlock(this HashAlgorithm alg, ReadOnlyByteSequence data)
        {
            var length = data.Length;
            var array  = ArrayPool <byte> .Shared.Rent((int)Math.Min(MaxBufferSize, length));

            try
            {
                foreach (var m in data)
                {
                    var mOff = 0;
                    do
                    {
                        var mLen = Math.Min(array.Length, m.Length - mOff);
                        m.Data.Span.Slice(mOff, mLen).CopyTo(array);
                        mOff += mLen;
                        alg.TransformBlock(array, 0, mLen, null, 0);
                    } while (mOff < m.Length);
                }
            }
            finally
            {
                Array.Clear(array, 0, array.Length);
                ArrayPool <byte> .Shared.Return(array);
            }
        }
        public static UInt160 Ripemd160(this ReadOnlyByteSequence data)
        {
            var h = new UInt160();

            Ripemd160(data, h.Span);
            return(h);
        }
        public static byte[] ComputeHash(this HashAlgorithm alg, ReadOnlyByteSequence buffer)
        {
            var hash = new byte[alg.HashSize];

            alg.TransformFinalBlock(buffer, hash);
            return(hash);
        }
        /// <summary>
        /// Computes double SHA256 of data: SHA256(SHA256(data))
        /// </summary>
        /// <param name="data">Input: bytes to be hashed.</param>
        /// <returns>SHA256 of SHA256 of data.</returns>
        public static UInt256 Hash256(this ReadOnlyByteSequence data)
        {
            var h2 = new UInt256();

            data.Hash256(h2.Span);
            return(h2);
        }
        public static UInt256 Sha256(this ReadOnlyByteSequence data)
        {
            var hash = new UInt256();

            Sha256(data, hash.Span);
            return(hash);
        }
        public static UInt512 HmacSha512(this ReadOnlyByteSpan key, ReadOnlyByteSequence data)
        {
            var hash = new UInt512();

            new HMACSHA512(key).TransformFinalBlock(data, hash.Span);
            return(hash);
        }
        /// <summary>
        /// Computes double SHA256 of data: SHA256(SHA256(data))
        /// </summary>
        /// <param name="data">Input: bytes to be hashed.</param>
        /// <param name="hash">Output: SHA256 of SHA256 of data.</param>
        public static void Hash256(this ReadOnlyByteSequence data, ByteSpan hash)
        {
            var h1 = new UInt256();

            using var sha = new SHA256Managed();
            TransformFinalBlock(sha, data, h1.Span);
            TransformFinalBlock(sha, h1.Span, hash);
        }
Example #9
0
        public static (bool ok, Script script) ParseHex(string rawScriptHex, bool withoutLength = false)
        {
            var bytes = rawScriptHex.HexToBytes();
            var s     = new Script();
            var ros   = new ReadOnlyByteSequence(bytes);
            var sr    = new ByteSequenceReader(ros);

            return(s.TryReadScript(ref sr, withoutLength), s);
        }
Example #10
0
        public bool TryParseBlock(ref ReadOnlyByteSequence ros, int height, IBlockParser bp)
        {
            var r = new ByteSequenceReader(ros);

            if (!TryParseBlock(ref r, height, bp))
            {
                return(false);
            }
            ros = ros.Data.Slice(r.Data.Consumed);
            return(true);
        }
Example #11
0
        public bool TryReadBlock(ref ReadOnlyByteSequence ros)
        {
            var r = new ByteSequenceReader(ros);

            if (!TryReadBlock(ref r))
            {
                return(false);
            }
            ros = ros.Data.Slice(r.Data.Consumed);
            return(true);
        }
Example #12
0
        /// <summary>
        /// Check whether a signature is normalized (lower-S).
        /// </summary>
        /// <param name="vchSig"></param>
        /// <returns></returns>
        public static bool CheckLowS(ReadOnlyByteSequence vchSig)
        {
            var sig   = new byte[64];
            var input = (ReadOnlyByteSpan)vchSig;

            using var library = new Secp256k1();
            if (!library.SignatureParseDerLax(sig, input))
            {
                return(false);
            }
            return(!library.SignatureNormalize(Span <byte> .Empty, input));
        }
        public void TestRemoveSlice()
        {
            var a   = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 };
            var ros = new ReadOnlyByteSequence(a);

            Assert.Equal(new byte[] { 1, 2 }, ros.RemoveSlice(2, 12).ToArray());
            Assert.Equal(new byte[] { 11, 12 }, ros.RemoveSlice(0, 10).ToArray());
            Assert.Equal(new byte[] { 1, 12 }, ros.RemoveSlice(1, 11).ToArray());
            var r1 = ros.RemoveSlice(1, 5);
            var r2 = r1.RemoveSlice(3, 7);

            Assert.Equal(new byte[] { 1, 6, 7, 12 }, r2.ToArray());
        }
Example #14
0
        private static Block GetBlock(int height)
        {
            var bytes = GetBlockBytes(height);

            var kzb = new Block {
                Height = height
            };
            var ros = new ReadOnlyByteSequence(bytes);
            var ok  = kzb.TryReadBlock(ref ros);

            Assert.True(ros.Length == 0);
            Assert.True(ok);

            return(kzb);
        }
Example #15
0
        public bool TryReadTransaction(ref ReadOnlyByteSequence ros)
        {
            var r = new ByteSequenceReader(ros);

            if (!TryReadTransaction(ref r))
            {
                goto fail;
            }

            ros = ros.Data.Slice(r.Data.Consumed);

            return(true);

fail:
            return(false);
        }
Example #16
0
        public override string Encode(ReadOnlyByteSequence bytes)
        {
            var s = new char[bytes.Length * 2];
            var i = 0;

            foreach (var m in bytes)
            {
                foreach (var b in m.Data.Span)
                {
                    var chs = ByteToChs[b];
                    s[i++] = chs[0];
                    s[i++] = chs[1];
                }
            }
            return(new string(s));
        }
Example #17
0
        /// <summary>
        /// Return the Template String representation of script bytes.
        /// If the returned string does not include all the script opcodes, either because the scriptLen or limitLen
        /// arguments are greater than zero, or if the script sequence ends with an incomplete multi-byte opcode,
        /// then "..." is appended following the last complete opcode.
        ///
        /// scriptLen argument should be used when the actual script is longer than the script sequence provided,
        /// which must then be a subsequence from the start of the script.
        /// If greater than zero it may be longer than the sequence provided in which case "..." will be appended
        /// after the last opcode.
        ///
        /// limitLen argument stops converting opcodes to their template string format after processing this many bytes.
        /// </summary>
        /// <param name="script"></param>
        /// <param name="scriptLen">How long the entire script is, or zero.</param>
        /// <param name="limitLen">How many bytes to process, or zero.</param>
        /// <returns></returns>
        public static string ToTemplateString(ReadOnlyByteSequence script, long scriptLen = 0, long limitLen = 0)
        {
            var ros = script;

            if (limitLen == 0)
            {
                limitLen = long.MaxValue;
            }
            var ok    = true;
            var count = 0L;
            var sb    = new StringBuilder();

            while (ros.Length > 0 && ok && limitLen > count)
            {
                var op = new Operand();
                ok     = op.TryReadOperand(ref ros, out var consumed);
                count += consumed;
                if (ok && limitLen >= count)
                {
                    var len = op.Data.Length;
                    sb.Append(len == 0 ? $"{op.CodeName} " : $"[{op.Data.Length}] ");
                }
            }
            if (sb.Length > 0)
            {
                sb.Length--;
            }
            if (scriptLen == 0)
            {
                scriptLen = count;
            }
            if (!ok || limitLen < count || count < scriptLen)
            {
                sb.Append("...");
            }
            return(sb.ToString());
        }
Example #18
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());
        }
Example #19
0
 private VarType()
 {
     _sequence = new ReadOnlyByteSequence(Array.Empty <byte>());
 }
Example #20
0
 /// <summary>
 /// Encodes a sequence of bytes as a string.///
 /// </summary>
 /// <param name="data">Byte sequence to be encoded.</param>
 /// <returns></returns>
 public virtual string Encode(ReadOnlyByteSequence data) => Encode((ReadOnlyByteSpan)data);
Example #21
0
 public IBitcoinWriter Add(ReadOnlyByteSequence data)
 {
     _alg.TransformBlock(data.ToArray());
     return(this);
 }
 /// <summary>
 /// Computes RIPEMD160 of SHA256 of data: RIPEMD160(SHA256(data))
 /// </summary>
 /// <param name="data">Input: bytes to be hashed.</param>
 /// <returns>KzHash160 RIPEMD160 of SHA256 of data.</returns>
 public static UInt160 Hash160(this ReadOnlyByteSequence data)
 {
     return(Ripemd160(Sha256(data).Span));
 }
Example #23
0
 public override string Encode(ReadOnlyByteSequence data) => System.Text.Encoding.UTF8.GetString(data);
 public static void HmacSha512(this ReadOnlyByteSpan key, ReadOnlyByteSequence data, ByteSpan hash)
 {
     new HMACSHA512(key).TransformFinalBlock(data, hash);
 }
        /// <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);
        }
 public static void Ripemd160(this ReadOnlyByteSequence data, ByteSpan hash)
 {
     Ripemd.Ripemd160(data, hash);
 }
Example #27
0
 public VarType(ReadOnlyByteSequence data)
 {
     _sequence = data;
 }
Example #28
0
 public static string ToHexReverse(this ReadOnlyByteSequence data) => Encoders.Hex.Encode(data);
 public static void Sha512(this ReadOnlyByteSequence data, ByteSpan hash)
 {
     using var sha = new SHA512Managed();
     sha.TransformFinalBlock(data, hash);
 }
Example #30
0
 public IBitcoinWriter Add(ReadOnlyByteSequence data)
 {
     Length += data.Length;
     return(this);
 }