using System;
using Renci.SshNet.Common;
using Renci.SshNet.Security.Cryptography;
namespace Renci.SshNet.Security
{
///
/// Contains DSA private and public key.
///
public class DsaKey : Key, IDisposable
{
private DsaDigitalSignature _digitalSignature;
private bool _isDisposed;
///
/// Gets the P.
///
public BigInteger P
{
get
{
return _privateKey[0];
}
}
///
/// Gets the Q.
///
public BigInteger Q
{
get
{
return _privateKey[1];
}
}
///
/// Gets the G.
///
public BigInteger G
{
get
{
return _privateKey[2];
}
}
///
/// Gets public key Y.
///
public BigInteger Y
{
get
{
return _privateKey[3];
}
}
///
/// Gets private key X.
///
public BigInteger X
{
get
{
return _privateKey[4];
}
}
///
/// Gets the length of the key.
///
///
/// The length of the key.
///
public override int KeyLength
{
get
{
return P.BitLength;
}
}
///
/// Gets the digital signature.
///
protected override DigitalSignature DigitalSignature
{
get
{
_digitalSignature ??= new DsaDigitalSignature(this);
return _digitalSignature;
}
}
///
/// Gets or sets the public.
///
///
/// The public.
///
public override BigInteger[] Public
{
get
{
return new[] { P, Q, G, Y };
}
set
{
if (value.Length != 4)
{
throw new InvalidOperationException("Invalid public key.");
}
_privateKey = value;
}
}
///
/// Initializes a new instance of the class.
///
public DsaKey()
{
_privateKey = new BigInteger[5];
}
///
/// Initializes a new instance of the class.
///
/// DER encoded private key data.
public DsaKey(byte[] data)
: base(data)
{
if (_privateKey.Length != 5)
{
throw new InvalidOperationException("Invalid private key.");
}
}
///
/// Initializes a new instance of the class.
///
/// The p.
/// The q.
/// The g.
/// The y.
/// The x.
public DsaKey(BigInteger p, BigInteger q, BigInteger g, BigInteger y, BigInteger x)
{
_privateKey = new BigInteger[5] { p, q, g, y, x };
}
///
/// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
///
public void Dispose()
{
Dispose(disposing: true);
GC.SuppressFinalize(this);
}
///
/// Releases unmanaged and - optionally - managed resources
///
/// true to release both managed and unmanaged resources; false to release only unmanaged resources.
protected virtual void Dispose(bool disposing)
{
if (_isDisposed)
{
return;
}
if (disposing)
{
var digitalSignature = _digitalSignature;
if (digitalSignature != null)
{
digitalSignature.Dispose();
_digitalSignature = null;
}
_isDisposed = true;
}
}
///
/// Finalizes an instance of the class.
///
~DsaKey()
{
Dispose(disposing: false);
}
}
}