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