RsaCipherBenchmarks.cs 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. using BenchmarkDotNet.Attributes;
  2. using Renci.SshNet.Security;
  3. using Renci.SshNet.Security.Cryptography.Ciphers;
  4. namespace Renci.SshNet.Benchmarks.Security.Cryptography.Ciphers
  5. {
  6. [MemoryDiagnoser]
  7. public class RsaCipherBenchmarks
  8. {
  9. private readonly RsaKey _privateKey;
  10. private readonly RsaKey _publicKey;
  11. private readonly byte[] _data;
  12. public RsaCipherBenchmarks()
  13. {
  14. _data = new byte[128];
  15. Random random = new(Seed: 12345);
  16. random.NextBytes(_data);
  17. using (var s = typeof(RsaCipherBenchmarks).Assembly.GetManifestResourceStream("Renci.SshNet.Benchmarks.Data.Key.RSA.txt"))
  18. {
  19. _privateKey = (RsaKey)new PrivateKeyFile(s).Key;
  20. // The implementations of RsaCipher.Encrypt/Decrypt differ based on whether the supplied RsaKey has private key information
  21. // or only public. So we extract out the public key information to a separate variable.
  22. _publicKey = new RsaKey()
  23. {
  24. Public = _privateKey.Public
  25. };
  26. }
  27. }
  28. [Benchmark]
  29. public byte[] Encrypt()
  30. {
  31. return new RsaCipher(_publicKey).Encrypt(_data);
  32. }
  33. // RSA Decrypt does not work
  34. // [Benchmark]
  35. // public byte[] Decrypt()
  36. // {
  37. // return new RsaCipher(_privateKey).Decrypt(_data);
  38. // }
  39. }
  40. }