| | | 1 | | using System; |
| | | 2 | | using Renci.SshNet.Common; |
| | | 3 | | using Renci.SshNet.Security.Chaos.NaCl; |
| | | 4 | | |
| | | 5 | | namespace Renci.SshNet.Security.Cryptography |
| | | 6 | | { |
| | | 7 | | /// <summary> |
| | | 8 | | /// Implements ECDSA digital signature algorithm. |
| | | 9 | | /// </summary> |
| | | 10 | | public class ED25519DigitalSignature : DigitalSignature, IDisposable |
| | | 11 | | { |
| | | 12 | | private readonly ED25519Key _key; |
| | | 13 | | private bool _isDisposed; |
| | | 14 | | |
| | | 15 | | /// <summary> |
| | | 16 | | /// Initializes a new instance of the <see cref="ED25519DigitalSignature" /> class. |
| | | 17 | | /// </summary> |
| | | 18 | | /// <param name="key">The ED25519Key key.</param> |
| | | 19 | | /// <exception cref="ArgumentNullException"><paramref name="key"/> is <see langword="null"/>.</exception> |
| | 10 | 20 | | public ED25519DigitalSignature(ED25519Key key) |
| | 10 | 21 | | { |
| | 10 | 22 | | if (key is null) |
| | 0 | 23 | | { |
| | 0 | 24 | | throw new ArgumentNullException(nameof(key)); |
| | | 25 | | } |
| | | 26 | | |
| | 10 | 27 | | _key = key; |
| | 10 | 28 | | } |
| | | 29 | | |
| | | 30 | | /// <summary> |
| | | 31 | | /// Verifies the signature. |
| | | 32 | | /// </summary> |
| | | 33 | | /// <param name="input">The input.</param> |
| | | 34 | | /// <param name="signature">The signature.</param> |
| | | 35 | | /// <returns> |
| | | 36 | | /// <see langword="true"/> if signature was successfully verified; otherwise <see langword="false"/>. |
| | | 37 | | /// </returns> |
| | | 38 | | /// <exception cref="InvalidOperationException">Invalid signature.</exception> |
| | | 39 | | public override bool Verify(byte[] input, byte[] signature) |
| | 3 | 40 | | { |
| | 3 | 41 | | return Ed25519.Verify(signature, input, _key.PublicKey); |
| | 3 | 42 | | } |
| | | 43 | | |
| | | 44 | | /// <summary> |
| | | 45 | | /// Creates the signature. |
| | | 46 | | /// </summary> |
| | | 47 | | /// <param name="input">The input.</param> |
| | | 48 | | /// <returns> |
| | | 49 | | /// Signed input data. |
| | | 50 | | /// </returns> |
| | | 51 | | /// <exception cref="SshException">Invalid ED25519Key key.</exception> |
| | | 52 | | public override byte[] Sign(byte[] input) |
| | 1 | 53 | | { |
| | 1 | 54 | | return Ed25519.Sign(input, _key.PrivateKey); |
| | 1 | 55 | | } |
| | | 56 | | |
| | | 57 | | /// <summary> |
| | | 58 | | /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources. |
| | | 59 | | /// </summary> |
| | | 60 | | public void Dispose() |
| | 0 | 61 | | { |
| | 0 | 62 | | Dispose(disposing: true); |
| | 0 | 63 | | GC.SuppressFinalize(this); |
| | 0 | 64 | | } |
| | | 65 | | |
| | | 66 | | /// <summary> |
| | | 67 | | /// Releases unmanaged and - optionally - managed resources. |
| | | 68 | | /// </summary> |
| | | 69 | | /// <param name="disposing"><see langword="true"/> to release both managed and unmanaged resources; <see langwor |
| | | 70 | | protected virtual void Dispose(bool disposing) |
| | 10 | 71 | | { |
| | 10 | 72 | | if (_isDisposed) |
| | 0 | 73 | | { |
| | 0 | 74 | | return; |
| | | 75 | | } |
| | | 76 | | |
| | 10 | 77 | | if (disposing) |
| | 0 | 78 | | { |
| | 0 | 79 | | _isDisposed = true; |
| | 0 | 80 | | } |
| | 10 | 81 | | } |
| | | 82 | | |
| | | 83 | | /// <summary> |
| | | 84 | | /// Finalizes an instance of the <see cref="ED25519DigitalSignature"/> class. |
| | | 85 | | /// </summary> |
| | | 86 | | ~ED25519DigitalSignature() |
| | 20 | 87 | | { |
| | 10 | 88 | | Dispose(disposing: false); |
| | 20 | 89 | | } |
| | | 90 | | } |
| | | 91 | | } |