Exemplo n.º 1
0
            }             // func ReadStreamData

            private static (string hashName, byte[] hashValue) CopyData(bool shouldDeflate, Stream srcStream, byte[] buf, int readed, Stream dstStream)
            {
                // pack destination stream
                var dst = shouldDeflate
                                        ? new GZipStream(dstStream, CompressionMode.Compress, true)
                                        : dstStream;

                using (var dstHashStream = new HashStream(dst, HashStreamDirection.Write, false, SHA256.Create()))
                {
                    try
                    {
                        // copy stream into file
                        dstHashStream.Write(buf, 0, readed);
                        srcStream.CopyTo(dst);
                    }
                    finally
                    {
                        dstHashStream.Flush();
                        dstHashStream.Dispose();
                    }

                    dstHashStream.Close();

                    return("SHA2_256", dstHashStream.HashSum);
                }
            }             // func CopyData
        public void TestHashStreamWrite()
        {
            Random r = new Random();

            byte[][] test =
                new byte[][]
            {
                new byte[300],
                new byte[1],
                new byte[500],
                new byte[11],
                new byte[1],
                new byte[1000],
            };
            using (HashStream hs = new HashStream(new SHA256Managed()))
                using (MemoryStream ms = new MemoryStream())
                    using (HashStream hsWrap = new HashStream(new SHA256Managed(), ms))
                    {
                        Assert.IsTrue(hs.CanWrite);
                        long len = 0;
                        foreach (byte[] bytes in test)
                        {
                            len += bytes.Length;
                            r.NextBytes(bytes);
                            hsWrap.Write(bytes, 0, bytes.Length);
                            hs.Write(bytes, 0, bytes.Length);
                        }
                        for (int i = 0; i < 5; i++)
                        {
                            len += 1;
                            byte val = (byte)r.Next(255);
                            hsWrap.WriteByte(val);
                            hs.WriteByte(val);
                        }

                        Assert.AreEqual(len, ms.Position);
                        Hash expect = Hash.SHA256(ms.ToArray());
                        Hash actual = hs.Close();

                        Assert.AreEqual(expect, actual);
                        Assert.AreEqual(expect.ToArray(), actual.ToArray());
                        Assert.AreEqual(expect.ToString(), actual.ToString());

                        //wrapped test
                        actual = hsWrap.FinalizeHash();
                        Assert.AreEqual(expect, actual);
                        Assert.AreEqual(expect.ToArray(), actual.ToArray());
                        Assert.AreEqual(expect.ToString(), actual.ToString());
                    }
        }
Exemplo n.º 3
0
        public void InitiateTaggedWorks()
        {
#if HAS_SPAN
            var sha = new NBitcoin.Secp256k1.SHA256();
            sha.InitializeTagged("lol");
            sha.Write(new byte[32]);
            byte[] buff = new byte[32];
            sha.GetHash(buff);
            Assert.Equal("9185788706ad8d475d2410ce07554aeff7a212418159a8fa8ef2b3cb4a883b62", new uint256(buff).ToString());
#endif
            HashStream stream = new HashStream();
            stream.SingleSHA256 = true;
            stream.InitializeTagged("lol");
            stream.Write(new byte[32], 0, 32);
            var actual = stream.GetHash();
            Assert.Equal("9185788706ad8d475d2410ce07554aeff7a212418159a8fa8ef2b3cb4a883b62", actual.ToString());
        }
Exemplo n.º 4
0
        public override void Write(byte[] buffer, int offset, int count)
        {
            long blockNum = CurrentSector;
            int  toWrite  = (int)Math.Min(count, Length - Position);

            byte[] hash = DoHash(buffer, offset, toWrite);

            if (Type == IntegrityStreamType.Save && buffer.IsEmpty())
            {
                Array.Clear(hash, 0, DigestSize);
            }

            base.Write(buffer, offset, count);

            HashStream.Position = blockNum * DigestSize;
            HashStream.Write(hash, 0, DigestSize);
        }
Exemplo n.º 5
0
        public unsafe void TestByteArrayAndStream(int length)
        {
            var array  = new byte[length];
            var random = new Random(0);

            random.NextBytes(array);

            var hash = Create();

            hash.Write(array, 0, array.Length);
            var value = ComputeAndPrint(hash);

            var hash1 = Create();

            fixed(void *parray = array)
            {
                hash1.Write(new IntPtr(parray), (ulong)array.Length);
            }

            var value1 = ComputeAndPrint(hash);

            var stream     = new MemoryStream();
            var hashStream = new HashStream <THash>(stream, Create());

            hashStream.Write(array, 0, array.Length);
            var valueStreamWrite = ComputeAndPrint(hashStream.Hash);

            // Check that backend stream is fine as well
            var outputArray = stream.ToArray();

            Assert.AreEqual(array, outputArray);

            // Reset stream
            hashStream.Position = 0;
            Reset(ref hashStream.Hash);

            var inputArray = new byte[array.Length];
            var readCount  = hashStream.Read(inputArray, 0, array.Length);

            Assert.AreEqual(array.Length, readCount);
            var valueStreamRead = ComputeAndPrint(hashStream.Hash);

            Assert.AreEqual(value, value1);
            Assert.AreEqual(value, valueStreamWrite);
            Assert.AreEqual(value, valueStreamRead);
        }