RsaDigitalSignature.cs 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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.
  15. /// </summary>
  16. /// <param name="rsaKey">The RSA key.</param>
  17. public RsaDigitalSignature(RsaKey rsaKey)
  18. : base(new ObjectIdentifier(1, 3, 14, 3, 2, 26), new RsaCipher(rsaKey))
  19. {
  20. _hash = HashAlgorithmFactory.CreateSHA1();
  21. }
  22. /// <summary>
  23. /// Hashes the specified input.
  24. /// </summary>
  25. /// <param name="input">The input.</param>
  26. /// <returns>
  27. /// Hashed data.
  28. /// </returns>
  29. protected override byte[] Hash(byte[] input)
  30. {
  31. return _hash.ComputeHash(input);
  32. }
  33. #region IDisposable Members
  34. private bool _isDisposed;
  35. /// <summary>
  36. /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged ResourceMessages.
  37. /// </summary>
  38. public void Dispose()
  39. {
  40. Dispose(true);
  41. GC.SuppressFinalize(this);
  42. }
  43. /// <summary>
  44. /// Releases unmanaged and - optionally - managed resources
  45. /// </summary>
  46. /// <param name="disposing"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged ResourceMessages.</param>
  47. protected virtual void Dispose(bool disposing)
  48. {
  49. if (_isDisposed)
  50. return;
  51. if (disposing)
  52. {
  53. if (_hash != null)
  54. {
  55. _hash.Dispose();
  56. _hash = null;
  57. }
  58. _isDisposed = true;
  59. }
  60. }
  61. /// <summary>
  62. /// Releases unmanaged resources and performs other cleanup operations before the
  63. /// <see cref="SshCommand"/> is reclaimed by garbage collection.
  64. /// </summary>
  65. ~RsaDigitalSignature()
  66. {
  67. // Do not re-create Dispose clean-up code here.
  68. // Calling Dispose(false) is optimal in terms of
  69. // readability and maintainability.
  70. Dispose(false);
  71. }
  72. #endregion
  73. }
  74. }