示例#1
0
        public void VerifyHMAC(MessageDigest digest, string[] results)
        {
            byte[] hash = HMAC.Digest(digest, key, small_data);
            string str  = BitConverter.ToString(hash);

            if (str != results[0])
            {
                Console.WriteLine("{0} - Failed to calculate hash on {1}", digest.Name, small_data);
                Console.WriteLine("got {0} instead of {1}", str, results[0]);
            }
            else
            {
                Console.WriteLine("{0} - Test 1 passed.", digest.Name);
            }
            // Compute the large hash
            using (HMAC hmac = new HMAC())
            {
                byte[] buf = Encoding.ASCII.GetBytes(new string('a', 1000));
                hmac.Init(key, digest);
                for (int i = 0; i < 1000; i++)
                {
                    hmac.Update(buf);
                }
                hash = hmac.DigestFinal();
                // Check for error
                str = BitConverter.ToString(hash);
                if (str != results[1])
                {
                    Console.WriteLine("{0} - Failed to calculate hash on a*1000", digest.Name);
                    Console.WriteLine("got {0} instead of {1}", str, results[1]);
                }
                else
                {
                    Console.WriteLine("{0} - Test 2 passed.", digest.Name);
                }
            }
        }