RsaDigitalSignature.cs 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. this._hash = new SHA1Hash();
  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 this._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. // Check to see if Dispose has already been called.
  50. if (!this._isDisposed)
  51. {
  52. // If disposing equals true, dispose all managed
  53. // and unmanaged ResourceMessages.
  54. if (disposing)
  55. {
  56. // Dispose managed ResourceMessages.
  57. if (this._hash != null)
  58. {
  59. this._hash.Clear();
  60. this._hash = null;
  61. }
  62. }
  63. // Note disposing has been done.
  64. this._isDisposed = true;
  65. }
  66. }
  67. /// <summary>
  68. /// Releases unmanaged resources and performs other cleanup operations before the
  69. /// <see cref="SshCommand"/> is reclaimed by garbage collection.
  70. /// </summary>
  71. ~RsaDigitalSignature()
  72. {
  73. // Do not re-create Dispose clean-up code here.
  74. // Calling Dispose(false) is optimal in terms of
  75. // readability and maintainability.
  76. Dispose(false);
  77. }
  78. #endregion
  79. }
  80. }