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
{
///
/// 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;
}
}
private DsaDigitalSignature _digitalSignature;
///
/// Gets the digital signature.
///
protected override DigitalSignature DigitalSignature
{
get
{
if (_digitalSignature == null)
{
_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];
_privateKey[0] = p;
_privateKey[1] = q;
_privateKey[2] = g;
_privateKey[3] = y;
_privateKey[4] = x;
}
#region IDisposable Members
private bool _isDisposed;
///
/// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
///
public void Dispose()
{
Dispose(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;
}
}
///
/// Releases unmanaged resources and performs other cleanup operations before the
/// is reclaimed by garbage collection.
///
~DsaKey()
{
Dispose(false);
}
#endregion
}
}