コード例 #1
0
        private static void Sha256HmacTests()
        {
            byte[] key     = Encoding.UTF8.GetBytes("ABCD");
            byte[] message = Encoding.UTF8.GetBytes("ABCD");
            using ProtectedMemory protectedKey     = ProtectedMemory.Allocate(key.Length);
            using ProtectedMemory protectedMessage = ProtectedMemory.Allocate(message.Length);
            protectedKey.Write(key, 0);
            protectedMessage.Write(message, 0);
            Sha256ProtectedCryptoProvider sha256 = new Sha256ProtectedCryptoProvider();

            Console.WriteLine(sha256.ComputeHmac(protectedKey, protectedMessage));
        }
コード例 #2
0
        private static void Sha256HmacPerfTest()
        {
            byte[] key     = Encoding.UTF8.GetBytes("ABCD");
            byte[] message = Encoding.UTF8.GetBytes("ABCD");
            using ProtectedMemory protectedKey     = ProtectedMemory.Allocate(key.Length);
            using ProtectedMemory protectedMessage = ProtectedMemory.Allocate(message.Length);
            protectedKey.Write(key, 0);
            protectedMessage.Write(message, 0);
            Sha256ProtectedCryptoProvider sha256 = new Sha256ProtectedCryptoProvider();
            Stopwatch stopwatch = new Stopwatch();

            stopwatch.Start();
            for (int i = 0; i < 250000; i++)
            {
                _ = sha256.ComputeHmac(protectedKey, protectedMessage);
            }
            stopwatch.Stop();
            Console.WriteLine("250000 HMACs done in " + stopwatch.Elapsed.ToString());
            double t = stopwatch.ElapsedMilliseconds / 250000d;

            Console.WriteLine(" * " + t.ToString() + " ms per HMAC.");
            Console.WriteLine(" * " + (1000d / t).ToString() + " HMACs per second.");
        }