FileDocCategorySizeDatePackage
JDKDSASigner.javaAPI DocAndroid 1.5 API11385Wed May 06 22:41:06 BST 2009org.bouncycastle.jce.provider

JDKDSASigner

public class JDKDSASigner extends Signature implements org.bouncycastle.asn1.x509.X509ObjectIdentifiers, org.bouncycastle.asn1.pkcs.PKCSObjectIdentifiers

Fields Summary
private org.bouncycastle.crypto.Digest
digest
private org.bouncycastle.crypto.DSA
signer
private SecureRandom
random
Constructors Summary
protected JDKDSASigner(String name, org.bouncycastle.crypto.Digest digest, org.bouncycastle.crypto.DSA signer)

        super(name);

        this.digest = digest;
        this.signer = signer;
    
Methods Summary
private java.math.BigInteger[]derDecode(byte[] encoding)

        ASN1InputStream         aIn = new ASN1InputStream(encoding);
        ASN1Sequence            s = (ASN1Sequence)aIn.readObject();

        BigInteger[]            sig = new BigInteger[2];

        sig[0] = ((DERInteger)s.getObjectAt(0)).getValue();
        sig[1] = ((DERInteger)s.getObjectAt(1)).getValue();

        return sig;
    
private byte[]derEncode(java.math.BigInteger r, java.math.BigInteger s)

        ByteArrayOutputStream   bOut = new ByteArrayOutputStream();
        DEROutputStream         dOut = new DEROutputStream(bOut);
        ASN1EncodableVector     v = new ASN1EncodableVector();

        v.add(new DERInteger(r));
        v.add(new DERInteger(s));

        dOut.writeObject(new DERSequence(v));

        return bOut.toByteArray();
    
protected java.lang.ObjectengineGetParameter(java.lang.String param)

deprecated

        throw new UnsupportedOperationException("engineSetParameter unsupported");
    
protected voidengineInitSign(java.security.PrivateKey privateKey, java.security.SecureRandom random)

        this.random = random;
        engineInitSign(privateKey);
    
protected voidengineInitSign(java.security.PrivateKey privateKey)

        CipherParameters    param = null;

        // BEGIN android-removed
        // if (privateKey instanceof ECKey)
        // {
        //     param = ECUtil.generatePrivateKeyParameter(privateKey);
        // }
        // else if (privateKey instanceof GOST3410Key)
        // {
        //     param = GOST3410Util.generatePrivateKeyParameter(privateKey);
        // }
        // else
        // {
        // END android-removed
            param = DSAUtil.generatePrivateKeyParameter(privateKey);
        // BEGIN android-removed
        // }
        // END android-removed

        digest.reset();

        if (random != null)
        {
            signer.init(true, new ParametersWithRandom(param, random));
        }
        else
        {
            signer.init(true, param);
        }
    
protected voidengineInitVerify(java.security.PublicKey publicKey)

        CipherParameters    param = null;

        // BEGIN android-removed
        // if (publicKey instanceof ECPublicKey)
        // {
        //     param = ECUtil.generatePublicKeyParameter(publicKey);
        // }
        // else if (publicKey instanceof GOST3410Key)
        // {
        //     param = GOST3410Util.generatePublicKeyParameter(publicKey);
        // }
        // else if (publicKey instanceof DSAKey)
        // END android-removed
        // BEGIN android-added
        if (publicKey instanceof DSAKey)
        // END android-added
        {
            param = DSAUtil.generatePublicKeyParameter(publicKey);
        }
        else
        {
            try
            {
                byte[]  bytes = publicKey.getEncoded();

                publicKey = JDKKeyFactory.createPublicKeyFromDERStream(bytes);

                // BEGIN android-removed
                // if (publicKey instanceof ECPublicKey)
                // {
                //     param = ECUtil.generatePublicKeyParameter(publicKey);
                // }
                // else if (publicKey instanceof DSAKey)
                // END android-removed
                // BEGIN android-added
                if (publicKey instanceof DSAKey)
                // END android-added
                {
                    param = DSAUtil.generatePublicKeyParameter(publicKey);
                }
                else
                {
                    throw new InvalidKeyException("can't recognise key type in DSA based signer");
                }
            }
            catch (Exception e)
            {
                throw new InvalidKeyException("can't recognise key type in DSA based signer");
            }
        }

        digest.reset();
        signer.init(false, param);
    
protected voidengineSetParameter(java.lang.String param, java.lang.Object value)

deprecated
replaced with

        throw new UnsupportedOperationException("engineSetParameter unsupported");
    
protected voidengineSetParameter(java.security.spec.AlgorithmParameterSpec params)

        throw new UnsupportedOperationException("engineSetParameter unsupported");
    
protected byte[]engineSign()

        byte[]  hash = new byte[digest.getDigestSize()];

        digest.doFinal(hash, 0);

        try
        {
            BigInteger[]    sig = signer.generateSignature(hash);

            return derEncode(sig[0], sig[1]);
        }
        catch (Exception e)
        {
            throw new SignatureException(e.toString());
        }
    
protected voidengineUpdate(byte b)

        digest.update(b);
    
protected voidengineUpdate(byte[] b, int off, int len)

        digest.update(b, off, len);
    
protected booleanengineVerify(byte[] sigBytes)

        byte[]  hash = new byte[digest.getDigestSize()];

        digest.doFinal(hash, 0);

        BigInteger[]    sig;

        try
        {
            sig = derDecode(sigBytes);
        }
        catch (Exception e)
        {
            throw new SignatureException("error decoding signature bytes.");
        }

        return signer.verifySignature(hash, sig[0], sig[1]);