public void TestSignature() { const string TestData = "Hello world"; var cert = X509CertificateCache.GetCertificate(MyConfig.TestCertThumbPrint); var payload = Encoding.UTF8.GetBytes(TestData); string[] HashAlgorithmNames = { "MD5", "SHA1", "SHA256", "SHA384", "SHA512" }; foreach (var name in HashAlgorithmNames) { using (var hashAlgorithm = HashAlgorithm.Create(name)) { // Digest var hash = hashAlgorithm.ComputeHash(payload); Console.WriteLine($"Hash: {name} {hash.Length * 8} bits / {hash.Length} BYTES"); // Sign var signature = cert.CreateSignature(hash); Console.WriteLine($"Signature: {signature.Length * 8} bits / {signature.Length} BYTES"); // Verify var good = cert.VerifySignature(hash, signature); Assert.IsTrue(good); } } }
public void X509_Benchmark_EncryptionAndDecryption() { const int SampleDataSizeInKB = 8; const int BenchmarkLoopCount = 1000; var x509Cert = X509CertificateCache.GetCertificate(MyConfig.TestCertThumbPrint); // Generate some random data // Perform a dry run // Capture Encrypted and Decrypted version var SampleData = GenerateJunk(SampleDataSizeInKB); var encryptedBytes = EncryptBytes(x509Cert, SampleData); var decryptedBytes = DecryptBytes(x509Cert, encryptedBytes); Assert.IsTrue(decryptedBytes.SequenceEqual(SampleData), "Decrypted bytes doesn't match original data."); var timer = Stopwatch.StartNew(); for (int i = 0; i < BenchmarkLoopCount; i++) { EncryptBytes(x509Cert, decryptedBytes); } timer.Stop(); var totalMs = timer.Elapsed.TotalMilliseconds; var totalSec = timer.Elapsed.TotalSeconds; var avgMs = totalMs / BenchmarkLoopCount; var ratePerSec = BenchmarkLoopCount / totalSec; Console.WriteLine("Encryption Benchmark:"); Console.WriteLine($"SampleDataSize: {SampleData.Length / 1024:#,0} KB"); Console.WriteLine($"Elapsed: {timer.Elapsed}"); Console.WriteLine($"{BenchmarkLoopCount:#,0} iterations @ {ratePerSec:#,0.0} per Sec. / Average: {avgMs:#,0.00} milliSec"); timer = Stopwatch.StartNew(); for (int i = 0; i < BenchmarkLoopCount; i++) { DecryptBytes(x509Cert, encryptedBytes); } timer.Stop(); totalMs = timer.Elapsed.TotalMilliseconds; totalSec = timer.Elapsed.TotalSeconds; avgMs = totalMs / BenchmarkLoopCount; ratePerSec = BenchmarkLoopCount / totalSec; Console.WriteLine("Decryption Benchmark:"); Console.WriteLine($"SampleDataSize: {SampleData.Length / 1024:#,0} KB"); Console.WriteLine($"Elapsed: {timer.Elapsed}"); Console.WriteLine($"{BenchmarkLoopCount:#,0} iterations @ {ratePerSec:#,0.0} per Sec. / Average: {avgMs:#,0.00} milliSec"); }
static void PrintOptionsAndRunTest() { while (true) { Console.WriteLine("-------------------------------------------"); Console.WriteLine("[P] Print AsymmetricAlgorithm provider."); Console.WriteLine("[V] Validate Encryption/Decryption, ONCE."); Console.WriteLine("[E] Start ENcryption loop."); Console.WriteLine("[D] Start DEcryption loop."); Console.WriteLine("[Q] or Ctrl-C to quit"); Console.WriteLine("-------------------------------------------"); var input = (Console.ReadLine() ?? string.Empty).Trim().ToUpper(); var cert = X509CertificateCache.GetCertificate(X509Thumbprint); switch (input) { case "V": ValidateEncryptionAndDecryptionOnce(cert); break; case "E": BeginEncryptionLoop(cert, LoopCount); break; case "D": BeginDecryptionLoop(cert, LoopCount); break; case "P": PrintCSP(cert); break; case "Q": return; default: ValidateEncryptionAndDecryptionOnce(cert); break; } Console.WriteLine(); } }
public void X509_TripleRoundTripTest() { const string TEST = "Hello World!"; var x509Cert = X509CertificateCache.GetCertificate(MyConfig.TestCertThumbPrint); byte[] input = Encoding.UTF8.GetBytes(TEST); byte[] output1 = DecryptBytes(x509Cert, EncryptBytes(x509Cert, input)); byte[] output2 = DecryptBytes(x509Cert, EncryptBytes(x509Cert, input)); byte[] output3 = DecryptBytes(x509Cert, EncryptBytes(x509Cert, input)); Assert.IsTrue(input.SequenceEqual(output1)); Assert.IsTrue(input.SequenceEqual(output2)); Assert.IsTrue(input.SequenceEqual(output3)); // Seeing is believing... Console.WriteLine($"Original: {TEST}"); Console.WriteLine($"#1 {Encoding.UTF8.GetString(output1)}"); Console.WriteLine($"#2 {Encoding.UTF8.GetString(output2)}"); Console.WriteLine($"#3 {Encoding.UTF8.GetString(output3)}"); }