Exemplo n.º 1
0
        protected void Verify(byte[] input, byte[] prepend, string output)
        {
            byte[] expected = ByteUtils.HexToByteArray(output);
            byte[] actual;

            // Too small
            actual = new byte[expected.Length - 1];
            Assert.Throws <ArgumentException>("destination", () => Sha.ComputeHash(input, prepend, actual));

            // Just right
            actual = new byte[expected.Length];
            Sha.ComputeHash(input, prepend, actual);
            Assert.Equal(expected, actual);

            // Bigger than needed
            actual = new byte[expected.Length + 1];
            actual[actual.Length - 1] = 42;
            Sha.ComputeHash(input, prepend, actual);
            Assert.Equal(expected, actual.AsSpan(0, expected.Length).ToArray());
            Assert.Equal(42, actual[actual.Length - 1]);
        }
Exemplo n.º 2
0
        protected void VerifyHmac(
            int testCaseId,
            string digest,
            int truncateSize = -1)
        {
            var hmac = Create(_testKeys[testCaseId]);

            byte[] digestBytes    = ByteUtils.HexToByteArray(digest);
            byte[] computedDigest = new byte[hmac.HashSize];
            hmac.ComputeHash(_testData[testCaseId], computedDigest);

            if (truncateSize != -1)
            {
                byte[] tmp = new byte[truncateSize];
                Array.Copy(computedDigest, tmp, truncateSize);
                computedDigest = tmp;
                tmp            = new byte[truncateSize];
                Array.Copy(digestBytes, tmp, truncateSize);
                digestBytes = tmp;
            }

            Assert.Equal(digestBytes, computedDigest);
        }
Exemplo n.º 3
0
 protected void Verify(string input, string prepend, string output)
 {
     Verify(ByteUtils.AsciiBytes(input), ByteUtils.AsciiBytes(prepend), output);
 }