private static unsafe void Sha256Tests() { byte[] bytes = Encoding.UTF8.GetBytes("ABCD"); using ProtectedMemory protectedMemory = ProtectedMemory.Allocate(bytes.Length); protectedMemory.Write(bytes, 0); Sha256ProtectedCryptoProvider sha256 = new Sha256ProtectedCryptoProvider(); Console.WriteLine(sha256.ComputeHash(protectedMemory)); fixed(byte *b = bytes) { Console.WriteLine(sha256.ComputeHashUnsafe(b, bytes.Length)); } }
private static void Sha256PerfTest() { byte[] bytes = Encoding.UTF8.GetBytes("Lorem ipsum dolor sit amet, consectetur adipiscing elit."); using ProtectedMemory protectedMemory = ProtectedMemory.Allocate(bytes.Length); protectedMemory.Write(bytes, 0); Sha256ProtectedCryptoProvider sha256 = new Sha256ProtectedCryptoProvider(); Stopwatch stopwatch = new Stopwatch(); stopwatch.Start(); for (int i = 0; i < 500000; i++) { _ = sha256.ComputeHash(protectedMemory); } stopwatch.Stop(); Console.WriteLine("500000 hashes done in " + stopwatch.Elapsed.ToString()); double t = stopwatch.ElapsedMilliseconds / 500000d; Console.WriteLine(" * " + t.ToString() + " ms per digest."); Console.WriteLine(" * " + (1000d / t).ToString() + " hashes per second."); }