using System; using System.Collections.Generic; using Renci.SshNet.Common; using Renci.SshNet.Security.Cryptography; namespace Renci.SshNet.Security { /// /// Base class for asymmetric cipher algorithms /// public abstract class Key { /// /// Specifies array of big integers that represent private key /// protected BigInteger[] _privateKey; /// /// Gets the key specific digital signature. /// protected abstract DigitalSignature DigitalSignature { get; } /// /// Gets or sets the public key. /// /// /// The public. /// public abstract BigInteger[] Public { get; set; } /// /// Gets the length of the key. /// /// /// The length of the key. /// public abstract int KeyLength { get; } /// /// Initializes a new instance of the class. /// /// DER encoded private key data. protected Key(byte[] data) { if (data == null) throw new ArgumentNullException("data"); var der = new DerData(data); der.ReadBigInteger(); // skip version var keys = new List(); while (!der.IsEndOfData) { keys.Add(der.ReadBigInteger()); } _privateKey = keys.ToArray(); } /// /// Initializes a new instance of the class. /// protected Key() { } /// /// Signs the specified data with the key. /// /// The data to sign. /// /// Signed data. /// public byte[] Sign(byte[] data) { return DigitalSignature.Sign(data); } /// /// Verifies the signature. /// /// The data to verify. /// The signature to verify against. /// True is signature was successfully verifies; otherwise false. public bool VerifySignature(byte[] data, byte[] signature) { return DigitalSignature.Verify(data, signature); } } }