public void TestVector(BlockHashAlgorithm hasher, byte[] key, byte[] input, byte[] expectedOutput)
        {
            Hmac hmac = new Hmac(hasher, key);

            byte[] result = hmac.ComputeHash(input);

            CustomAssert.AreEqual(
                expectedOutput,
                result,
                string.Format("{0}({1}) on {2} should have produced {3} but instead got {4}", hmac, Conversions.ByteToHexadecimal(key), Conversions.ByteToHexadecimal(input), Conversions.ByteToHexadecimal(expectedOutput), Conversions.ByteToHexadecimal(result))
                );
        }
Example #2
0
        /// <summary>Initializes a new instance of the HMAC class.</summary>
        /// <param name="hashAlgorithm">The base hash algorithm to use.</param>
        /// <param name="key">The key to use for the HMAC.</param>
        /// <exception cref="ArgumentNullException">When the specified HashAlgorithm is null.</exception>
        /// <remarks>If the specified key is null, a random key will be generated and used.</remarks>
        public Hmac(BlockHashAlgorithm hashAlgorithm, byte[] key)
        {
            lock (syncLock) {
                if (hashAlgorithm == null)
                {
                    throw new ArgumentNullException("hashAlgorithm", Properties.Resources.hashCantBeNull);
                }
                this.hashAlgorithm = hashAlgorithm;

                if (key == null)
                {
                    Random r    = new Random((int)System.DateTime.Now.Ticks);
                    byte[] temp = new byte[HashAlgorithm.BlockSize];
                    r.NextBytes(temp);
                    InitializeKey(temp);
                }
                else
                {
                    InitializeKey(key);
                }
            }
        }
        public void HashSizeTest(BlockHashAlgorithm hasher)
        {
            Hmac hmac = new Hmac(hasher);

            Assert.AreEqual(hasher.HashSize, hmac.HashSize);
        }
Example #4
0
 /// <summary>Initializes a new instance of the HMAC class.</summary>
 /// <param name="hashAlgorithm">The base hash algorithm to use.</param>
 /// <remarks>A random key will be generated.</remarks>
 public Hmac(BlockHashAlgorithm hashAlgorithm) : this(hashAlgorithm, null)
 {
 }