1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283 |
- using System.Security.Cryptography;
- using System.Text;
- namespace FxSsh.Algorithms
- {
- public class DssKey : PublicKeyAlgorithm
- {
- private readonly DSACryptoServiceProvider _algorithm = new DSACryptoServiceProvider();
- public DssKey(string key = null)
- : base(key)
- {
- }
- public override string Name
- {
- get { return "ssh-dss"; }
- }
- public override void ImportKey(byte[] bytes)
- {
- _algorithm.ImportCspBlob(bytes);
- }
- public override byte[] ExportKey()
- {
- return _algorithm.ExportCspBlob(true);
- }
- public override void LoadKeyAndCertificatesData(byte[] data)
- {
- using (var worker = new SshDataWorker(data))
- {
- if (worker.ReadString(Encoding.ASCII) != this.Name)
- throw new CryptographicException("Key and certificates were not created with this algorithm.");
- var args = new DSAParameters();
- args.P = worker.ReadMpint();
- args.Q = worker.ReadMpint();
- args.G = worker.ReadMpint();
- args.Y = worker.ReadMpint();
- _algorithm.ImportParameters(args);
- }
- }
- public override byte[] CreateKeyAndCertificatesData()
- {
- using (var worker = new SshDataWorker())
- {
- var args = _algorithm.ExportParameters(false);
- worker.Write(this.Name, Encoding.ASCII);
- worker.WriteMpint(args.P);
- worker.WriteMpint(args.Q);
- worker.WriteMpint(args.G);
- worker.WriteMpint(args.Y);
- return worker.ToByteArray();
- }
- }
- public override bool VerifyData(byte[] data, byte[] signature)
- {
- return _algorithm.VerifyData(data, signature);
- }
- public override bool VerifyHash(byte[] hash, byte[] signature)
- {
- return _algorithm.VerifyHash(hash, "SHA1", signature);
- }
- public override byte[] SignData(byte[] data)
- {
- return _algorithm.SignData(data);
- }
- public override byte[] SignHash(byte[] hash)
- {
- return _algorithm.SignHash(hash, "SHA1");
- }
- }
- }
|