DsaDigestSigner.cs :  » PDF » iTextSharp » Org » BouncyCastle » Crypto » Signers » C# / CSharp Open Source

Home
C# / CSharp Open Source
1.2.6.4 mono .net core
2.2.6.4 mono core
3.Aspect Oriented Frameworks
4.Bloggers
5.Build Systems
6.Business Application
7.Charting Reporting Tools
8.Chat Servers
9.Code Coverage Tools
10.Content Management Systems CMS
11.CRM ERP
12.Database
13.Development
14.Email
15.Forum
16.Game
17.GIS
18.GUI
19.IDEs
20.Installers Generators
21.Inversion of Control Dependency Injection
22.Issue Tracking
23.Logging Tools
24.Message
25.Mobile
26.Network Clients
27.Network Servers
28.Office
29.PDF
30.Persistence Frameworks
31.Portals
32.Profilers
33.Project Management
34.RSS RDF
35.Rule Engines
36.Script
37.Search Engines
38.Sound Audio
39.Source Control
40.SQL Clients
41.Template Engines
42.Testing
43.UML
44.Web Frameworks
45.Web Service
46.Web Testing
47.Wiki Engines
48.Windows Presentation Foundation
49.Workflows
50.XML Parsers
C# / C Sharp
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source » PDF » iTextSharp 
iTextSharp » Org » BouncyCastle » Crypto » Signers » DsaDigestSigner.cs
using System;
using System.Collections;
using System.IO;
using System.Text;

using Org.BouncyCastle.Asn1;
using Org.BouncyCastle.Crypto.Signers;
using Org.BouncyCastle.Crypto.Parameters;
using Org.BouncyCastle.Math;
using Org.BouncyCastle.Security;

namespace Org.BouncyCastle.Crypto.Signers{
    public class DsaDigestSigner
    : ISigner
    {
        private readonly IDigest digest;
        private readonly IDsa dsaSigner;
        private bool forSigning;

    public DsaDigestSigner(
      IDsa  signer,
      IDigest  digest)
        {
            this.digest = digest;
            this.dsaSigner = signer;
        }

    public string AlgorithmName
        {
            get { return digest.AlgorithmName + "with" + dsaSigner.AlgorithmName; }
        }

    public 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();

            dsaSigner.Init(forSigning, parameters);
        }

        /**
         * update the internal digest with the byte b
         */
        public void Update(
      byte input)
        {
            digest.Update(input);
        }

    /**
         * update the internal digest with the byte array in
         */
        public 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 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 = dsaSigner.GenerateSignature(hash);

      return DerEncode(sig[0], sig[1]);
    }

    /// <returns>true if the internal state represents the signature described in the passed in array.</returns>
        public 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 = DerDecode(signature);
        return dsaSigner.VerifySignature(hash, sig[0], sig[1]);
      }
      catch (IOException)
      {
        return false;
      }
        }

    /// <summary>Reset the internal state</summary>
        public void Reset()
        {
            digest.Reset();
        }

    private byte[] DerEncode(
            BigInteger  r,
            BigInteger  s)
        {
      return new DerSequence(new DerInteger(r), new DerInteger(s)).GetDerEncoded();
        }

    private BigInteger[] DerDecode(
      byte[] encoding)
        {
            Asn1Sequence s = (Asn1Sequence) Asn1Object.FromByteArray(encoding);

      return new BigInteger[]
      {
        ((DerInteger) s[0]).Value,
        ((DerInteger) s[1]).Value
      };
        }
    }
}
www.java2v.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.