Beispiel #1
0
        private void SHA1_TestRepetitionVector(char input, int repetition, string expected = null)
        {
            // Arrange
            var shaHasher1 = new AdapterUtilities.TestIdProvider.Sha1Implementation();
            var shaHasher2 = new AdapterUtilities.TestIdProvider.Sha1Implementation();

            var bytes = new byte[repetition];

            for (int i = 0; i < repetition; i++)
            {
                bytes[i] = (byte)input;
            }

            if (string.IsNullOrEmpty(expected))
            {
                using (var hasher = System.Security.Cryptography.SHA1.Create())
                {
                    expected = ToHex(hasher.ComputeHash(bytes));
                }
            }
            else
            {
                expected = expected.Replace(" ", "").ToLowerInvariant();
            }

            // Act
            var digest1 = ToHex(shaHasher1.ComputeHash(bytes));
            var blocks  = bytes.Length / AdapterUtilities.TestIdProvider.BlockBytes;

            byte[] block;
            for (var i = 0; i < blocks; i += 1)
            {
                block = new byte[AdapterUtilities.TestIdProvider.BlockBytes];
                Buffer.BlockCopy(bytes, i * block.Length, block, 0, block.Length);
                shaHasher2.ProcessBlock(block, 0, block.Length);
            }

            var rest = bytes.Length - blocks * AdapterUtilities.TestIdProvider.BlockBytes;

            if (rest != 0)
            {
                block = new byte[rest];
                Buffer.BlockCopy(bytes, blocks * block.Length, block, 0, block.Length);
                shaHasher2.PadMessage(ref block, block.Length);
                shaHasher2.ProcessBlock(block, 0, block.Length);
            }

            var digest2 = ToHex(shaHasher2.ProcessFinalBlock());

            // Assert
            Assert.AreEqual(expected, digest1, $"Test vector '{input}'*{repetition} failed! (normal path)");
            Assert.AreEqual(expected, digest2, $"Test vector '{input}'*{repetition} failed! (padding path)");
        }
Beispiel #2
0
        private void SHA1_TestVector(string message, string expected)
        {
            // Arrange
            expected = expected.Replace(" ", "").ToLowerInvariant();
            var shaHasher1 = new AdapterUtilities.TestIdProvider.Sha1Implementation();

            // Act
            var bytes   = UTF8Encoding.UTF8.GetBytes(message);
            var digest1 = ToHex(shaHasher1.ComputeHash(bytes));

            // Assert
            Assert.AreEqual(expected, digest1, $"Test vector '{message}' failed!");
        }