RsaDigitalSignature.cs 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. using System;
  2. using System.Security.Cryptography;
  3. using Renci.SshNet.Common;
  4. using Renci.SshNet.Security.Cryptography.Ciphers;
  5. namespace Renci.SshNet.Security.Cryptography
  6. {
  7. /// <summary>
  8. /// Implements RSA digital signature algorithm.
  9. /// </summary>
  10. public class RsaDigitalSignature : CipherDigitalSignature, IDisposable
  11. {
  12. private HashAlgorithm _hash;
  13. /// <summary>
  14. /// Initializes a new instance of the <see cref="RsaDigitalSignature"/> class with the SHA-1 hash algorithm.
  15. /// </summary>
  16. /// <param name="rsaKey">The RSA key.</param>
  17. public RsaDigitalSignature(RsaKey rsaKey)
  18. : this(rsaKey, HashAlgorithmName.SHA1)
  19. { }
  20. /// <summary>
  21. /// Initializes a new instance of the <see cref="RsaDigitalSignature"/> class.
  22. /// </summary>
  23. /// <param name="rsaKey">The RSA key.</param>
  24. /// <param name="hashAlgorithmName">The hash algorithm to use in the digital signature.</param>
  25. public RsaDigitalSignature(RsaKey rsaKey, HashAlgorithmName hashAlgorithmName)
  26. : base(ObjectIdentifier.FromHashAlgorithmName(hashAlgorithmName), new RsaCipher(rsaKey))
  27. {
  28. _hash = CryptoConfig.CreateFromName(hashAlgorithmName.Name) as HashAlgorithm
  29. ?? throw new ArgumentException($"Could not create {nameof(HashAlgorithm)} from `{hashAlgorithmName}`.", nameof(hashAlgorithmName));
  30. }
  31. /// <summary>
  32. /// Hashes the specified input.
  33. /// </summary>
  34. /// <param name="input">The input.</param>
  35. /// <returns>
  36. /// Hashed data.
  37. /// </returns>
  38. protected override byte[] Hash(byte[] input)
  39. {
  40. return _hash.ComputeHash(input);
  41. }
  42. #region IDisposable Members
  43. private bool _isDisposed;
  44. /// <summary>
  45. /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
  46. /// </summary>
  47. public void Dispose()
  48. {
  49. Dispose(disposing: true);
  50. GC.SuppressFinalize(this);
  51. }
  52. /// <summary>
  53. /// Releases unmanaged and - optionally - managed resources
  54. /// </summary>
  55. /// <param name="disposing"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.</param>
  56. protected virtual void Dispose(bool disposing)
  57. {
  58. if (_isDisposed)
  59. {
  60. return;
  61. }
  62. if (disposing)
  63. {
  64. var hash = _hash;
  65. if (hash != null)
  66. {
  67. hash.Dispose();
  68. _hash = null;
  69. }
  70. _isDisposed = true;
  71. }
  72. }
  73. /// <summary>
  74. /// Releases unmanaged resources and performs other cleanup operations before the
  75. /// <see cref="RsaDigitalSignature"/> is reclaimed by garbage collection.
  76. /// </summary>
  77. ~RsaDigitalSignature()
  78. {
  79. Dispose(disposing: false);
  80. }
  81. #endregion
  82. }
  83. }