public static void ApiInterop_OldToNew_Stream()
        {
            const int TotalCount = 10;

            using (var rsa = new RSACryptoServiceProvider())
            using (PositionValueStream stream1 = new PositionValueStream(TotalCount))
            using (PositionValueStream stream2 = new PositionValueStream(TotalCount))
            {
                byte[] oldSignature = rsa.SignData(stream1, "SHA384");
                Assert.True(rsa.VerifyData(stream2, oldSignature, HashAlgorithmName.SHA384, RSASignaturePadding.Pkcs1));
            }
        }
示例#2
0
        private void StreamHashHelper(int byteCount)
        {
            byte[] result;

            using (var stream = new PositionValueStream(byteCount))
                using (HashAlgorithm hash = new Length32Hash())
                {
                    result = hash.ComputeHash(stream);
                }

            AssertCorrectAnswer((uint)byteCount, result);
        }
示例#3
0
        public static void ApiInterop_OldToNew_Stream()
        {
            const int TotalCount = 10;

            using (var rsa = new RSACryptoServiceProvider())
                using (PositionValueStream stream1 = new PositionValueStream(TotalCount))
                    using (PositionValueStream stream2 = new PositionValueStream(TotalCount))
                    {
                        byte[] oldSignature = rsa.SignData(stream1, "SHA384");
                        Assert.True(rsa.VerifyData(stream2, oldSignature, HashAlgorithmName.SHA384, RSASignaturePadding.Pkcs1));
                    }
        }
示例#4
0
        public void ClearIsDispose()
        {
            using (var stream = new PositionValueStream(0))
                using (HashAlgorithm hash = new Length32Hash())
                {
                    Assert.Throws <NullReferenceException>(() => hash.ComputeHash((Stream)null));

                    hash.Clear();

                    Assert.Throws <ObjectDisposedException>(() => hash.ComputeHash(stream));
                }
        }
示例#5
0
        public void ValidateStreamContents()
        {
            byte[]    result;
            const int ByteCount = 1026;

            using (var stream = new PositionValueStream(ByteCount))
                using (HashAlgorithm hash = new Sum32Hash())
                {
                    result = hash.ComputeHash(stream);
                }

            AssertCorrectAnswer(ExpectedSum(0, ByteCount), result);
        }
        public void ValidateStreamContents()
        {
            byte[] result;
            const int ByteCount = 1026;

            using (var stream = new PositionValueStream(ByteCount))
            using (HashAlgorithm hash = new Sum32Hash())
            {
                result = hash.ComputeHash(stream);
            }

            AssertCorrectAnswer(ExpectedSum(0, ByteCount), result);
        }
示例#7
0
        public static async Task AppendStreamAsyncSanityTest(int streamSize)
        {
            NonCryptographicHashAlgorithm hash = new CountingAlgorithm();

            using (PositionValueStream stream = new PositionValueStream(streamSize))
            {
                await hash.AppendAsync(stream);
            }

            byte[] val = new byte[sizeof(int)];
            hash.GetHashAndReset(val);
            int res = BinaryPrimitives.ReadInt32LittleEndian(val);

            Assert.Equal(streamSize, res);
        }
示例#8
0
        public static void AppendStreamSanityTest(int streamSize)
        {
            NonCryptographicHashAlgorithm hash = new CountingAlgorithm();

            using (PositionValueStream stream = new PositionValueStream(streamSize))
            {
                hash.Append(stream);
            }

            Span <byte> val = stackalloc byte[sizeof(int)];

            hash.GetHashAndReset(val);
            int res = BinaryPrimitives.ReadInt32LittleEndian(val);

            Assert.Equal(streamSize, res);
        }
示例#9
0
        public static void AppendStreamAsyncSupportsCancellation()
        {
            NonCryptographicHashAlgorithm hash = new CountingAlgorithm();

            using (PositionValueStream stream = new PositionValueStream(21))
                using (CancellationTokenSource cts = new CancellationTokenSource())
                {
                    cts.Cancel();

                    Task task = hash.AppendAsync(stream, cts.Token);
                    Assert.True(task.IsCompleted);
                    Assert.True(task.IsCanceled);
                }

            byte[] val = new byte[sizeof(int)];
            hash.GetHashAndReset(val);
            int res = BinaryPrimitives.ReadInt32LittleEndian(val);

            Assert.Equal(0, res);
        }
示例#10
0
        public async Task VerifyComputeHashAsync(int size)
        {
            int fullCycles = size / 256;
            int partial    = size % 256;
            // SUM(0..255) is 32640
            const long CycleSum = 32640L;

            // The formula for additive sum IS n*(n+1)/2, but size is a count and the first n is 0,
            // which happens to turn it into (n-1) * n / 2, aka n * (n - 1) / 2.
            long expectedSum = CycleSum * fullCycles + (partial * (partial - 1) / 2);

            using (PositionValueStream stream = new PositionValueStream(size))
                using (HashAlgorithm hash = new SummingTestHashAlgorithm())
                {
                    byte[] result = await hash.ComputeHashAsync(stream);

                    byte[] expected = BitConverter.GetBytes(expectedSum);

                    Assert.Equal(expected, result);
                }
        }
示例#11
0
        private void ValidateOffset(int arraySize, int hashOffset, int hashCount)
        {
            byte[] input;

            using (var stream = new PositionValueStream(arraySize))
                using (var reader = new BinaryReader(stream))
                {
                    input = reader.ReadBytes(arraySize);
                }

            byte[] result;

            using (HashAlgorithm hash = new Sum32Hash())
            {
                result = hash.ComputeHash(input, hashOffset, hashCount);
            }

            uint expectedSum = ExpectedSum(hashOffset, hashCount);

            AssertCorrectAnswer(expectedSum, result);
        }
示例#12
0
        public static void ExpectSignature_SHA256_1024_Stream()
        {
            byte[] expectedSignature = new byte[]
            {
                0x78, 0x6F, 0x42, 0x00, 0xF4, 0x5A, 0xDB, 0x09,
                0x72, 0xB9, 0xCD, 0xBE, 0xB8, 0x46, 0x54, 0xE0,
                0xCF, 0x02, 0xB5, 0xA1, 0xF1, 0x7C, 0xA7, 0x5A,
                0xCF, 0x09, 0x60, 0xB6, 0xFF, 0x6B, 0x8A, 0x92,
                0x8E, 0xB4, 0xD5, 0x2C, 0x64, 0x90, 0x3E, 0x38,
                0x8B, 0x1D, 0x7D, 0x0E, 0xE8, 0x3C, 0xF0, 0xB9,
                0xBB, 0xEF, 0x90, 0x49, 0x7E, 0x6A, 0x1C, 0xEC,
                0x51, 0xB9, 0x13, 0x9B, 0x02, 0x02, 0x66, 0x59,
                0xC6, 0xB1, 0x51, 0xBD, 0x17, 0x2E, 0x03, 0xEC,
                0x93, 0x2B, 0xE9, 0x41, 0x28, 0x57, 0x8C, 0xB2,
                0x42, 0x60, 0xDE, 0xB4, 0x18, 0x85, 0x81, 0x55,
                0xAE, 0x09, 0xD9, 0xC4, 0x87, 0x57, 0xD1, 0x90,
                0xB3, 0x18, 0xD2, 0x96, 0x18, 0x91, 0x2D, 0x38,
                0x98, 0x0E, 0x68, 0x3C, 0xA6, 0x2E, 0xFE, 0x0D,
                0xD0, 0x50, 0x18, 0x55, 0x75, 0xA9, 0x85, 0x40,
                0xAB, 0x72, 0xE6, 0x7F, 0x9F, 0xDC, 0x30, 0xB9,
            };

            byte[] signature;

            using (Stream stream = new PositionValueStream(10))
            using (RSA rsa = RSAFactory.Create())
            {
                rsa.ImportParameters(TestData.RSA1024Params);
                signature = rsa.SignData(stream, HashAlgorithmName.SHA256, RSASignaturePadding.Pkcs1);
            }

            Assert.Equal(expectedSignature, signature);
        }
        private void ValidateOffset(int arraySize, int hashOffset, int hashCount)
        {
            byte[] input;

            using (var stream = new PositionValueStream(arraySize))
            using (var reader = new BinaryReader(stream))
            {
                input = reader.ReadBytes(arraySize);
            }

            byte[] result;

            using (HashAlgorithm hash = new Sum32Hash())
            {
                result = hash.ComputeHash(input, hashOffset, hashCount);
            }

            uint expectedSum = ExpectedSum(hashOffset, hashCount);

            AssertCorrectAnswer(expectedSum, result);
        }
        private void StreamHash(int byteCount)
        {
            byte[] result;

            using (var stream = new PositionValueStream(byteCount))
            using (HashAlgorithm hash = new Length32Hash())
            {
                result = hash.ComputeHash(stream);
            }

            AssertCorrectAnswer((uint)byteCount, result);
        }
        public void StreamHashInvalidArguments()
        {
            using (var stream = new PositionValueStream(0))
            using (HashAlgorithm hash = new Length32Hash())
            {
                Assert.Throws<NullReferenceException>(() => hash.ComputeHash((Stream)null));

                hash.Dispose();

                Assert.Throws<ObjectDisposedException>(() => hash.ComputeHash(stream));
            }
        }