123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150 |
- #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
- #pragma warning disable
- using System;
- using BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto.Parameters;
- using BestHTTP.SecureProtocol.Org.BouncyCastle.Math;
- using BestHTTP.SecureProtocol.Org.BouncyCastle.Security;
- namespace BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto.Signers
- {
- public class DsaDigestSigner
- : ISigner
- {
- private readonly IDsa dsa;
- private readonly IDigest digest;
- private readonly IDsaEncoding encoding;
- private bool forSigning;
- public DsaDigestSigner(
- IDsa dsa,
- IDigest digest)
- {
- this.dsa = dsa;
- this.digest = digest;
- this.encoding = StandardDsaEncoding.Instance;
- }
- public DsaDigestSigner(
- IDsaExt dsa,
- IDigest digest,
- IDsaEncoding encoding)
- {
- this.dsa = dsa;
- this.digest = digest;
- this.encoding = encoding;
- }
- public virtual string AlgorithmName
- {
- get { return digest.AlgorithmName + "with" + dsa.AlgorithmName; }
- }
- public virtual void Init(
- bool forSigning,
- ICipherParameters parameters)
- {
- this.forSigning = forSigning;
- AsymmetricKeyParameter k;
- if (parameters is ParametersWithRandom)
- {
- k = (AsymmetricKeyParameter)((ParametersWithRandom)parameters).Parameters;
- }
- else
- {
- k = (AsymmetricKeyParameter)parameters;
- }
- if (forSigning && !k.IsPrivate)
- throw new InvalidKeyException("Signing Requires Private Key.");
- if (!forSigning && k.IsPrivate)
- throw new InvalidKeyException("Verification Requires Public Key.");
- Reset();
- dsa.Init(forSigning, parameters);
- }
- /**
- * update the internal digest with the byte b
- */
- public virtual void Update(
- byte input)
- {
- digest.Update(input);
- }
- /**
- * update the internal digest with the byte array in
- */
- public virtual void BlockUpdate(
- byte[] input,
- int inOff,
- int length)
- {
- digest.BlockUpdate(input, inOff, length);
- }
- /**
- * Generate a signature for the message we've been loaded with using
- * the key we were initialised with.
- */
- public virtual byte[] GenerateSignature()
- {
- if (!forSigning)
- throw new InvalidOperationException("DSADigestSigner not initialised for signature generation.");
- byte[] hash = new byte[digest.GetDigestSize()];
- digest.DoFinal(hash, 0);
- BigInteger[] sig = dsa.GenerateSignature(hash);
- try
- {
- return encoding.Encode(GetOrder(), sig[0], sig[1]);
- }
- catch (Exception)
- {
- throw new InvalidOperationException("unable to encode signature");
- }
- }
- /// <returns>true if the internal state represents the signature described in the passed in array.</returns>
- public virtual bool VerifySignature(
- byte[] signature)
- {
- if (forSigning)
- throw new InvalidOperationException("DSADigestSigner not initialised for verification");
- byte[] hash = new byte[digest.GetDigestSize()];
- digest.DoFinal(hash, 0);
- try
- {
- BigInteger[] sig = encoding.Decode(GetOrder(), signature);
- return dsa.VerifySignature(hash, sig[0], sig[1]);
- }
- catch (Exception)
- {
- return false;
- }
- }
- /// <summary>Reset the internal state</summary>
- public virtual void Reset()
- {
- digest.Reset();
- }
- protected virtual BigInteger GetOrder()
- {
- return dsa is IDsaExt ? ((IDsaExt)dsa).Order : null;
- }
- }
- }
- #pragma warning restore
- #endif
|