RsaDigitalSignatureBenchmarks.cs 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. using System.Security.Cryptography;
  2. using BenchmarkDotNet.Attributes;
  3. using Renci.SshNet.Security;
  4. using Renci.SshNet.Security.Cryptography;
  5. namespace Renci.SshNet.Benchmarks.Security.Cryptography
  6. {
  7. [MemoryDiagnoser]
  8. public class RsaDigitalSignatureBenchmarks
  9. {
  10. private readonly RsaKey _key;
  11. private readonly byte[] _data;
  12. private readonly byte[] _signature;
  13. public RsaDigitalSignatureBenchmarks()
  14. {
  15. _data = new byte[128];
  16. Random random = new(Seed: 12345);
  17. random.NextBytes(_data);
  18. using (var s = typeof(RsaDigitalSignatureBenchmarks).Assembly.GetManifestResourceStream("Renci.SshNet.Benchmarks.Data.Key.OPENSSH.RSA.txt"))
  19. {
  20. _key = (RsaKey) new PrivateKeyFile(s).Key;
  21. }
  22. _signature = new RsaDigitalSignature(_key, HashAlgorithmName.SHA256).Sign(_data);
  23. }
  24. [Benchmark]
  25. public byte[] Sign()
  26. {
  27. return new RsaDigitalSignature(_key, HashAlgorithmName.SHA256).Sign(_data);
  28. }
  29. [Benchmark]
  30. public bool Verify()
  31. {
  32. return new RsaDigitalSignature(_key, HashAlgorithmName.SHA256).Verify(_data, _signature);
  33. }
  34. }
  35. }