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

JDKDigestSignature

public class JDKDigestSignature 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.AsymmetricBlockCipher
cipher
private org.bouncycastle.asn1.x509.AlgorithmIdentifier
algId
Constructors Summary
protected JDKDigestSignature(String name, org.bouncycastle.asn1.DERObjectIdentifier objId, org.bouncycastle.crypto.Digest digest, org.bouncycastle.crypto.AsymmetricBlockCipher cipher)

        super(name);

        this.digest = digest;
        this.cipher = cipher;
        this.algId = new AlgorithmIdentifier(objId);
    
Methods Summary
private byte[]derEncode(byte[] hash)

        DigestInfo              dInfo = new DigestInfo(algId, hash);

        return dInfo.getEncoded(ASN1Encodable.DER);
    
protected java.lang.ObjectengineGetParameter(java.lang.String param)

deprecated

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

        if (!(privateKey instanceof RSAPrivateKey))
        {
            throw new InvalidKeyException("Supplied key (" + getType(privateKey) + ") is not a RSAPrivateKey instance");
        }

        CipherParameters    param = RSAUtil.generatePrivateKeyParameter((RSAPrivateKey)privateKey);

        digest.reset();

        cipher.init(true, param);
    
protected voidengineInitVerify(java.security.PublicKey publicKey)

        if (!(publicKey instanceof RSAPublicKey))
        {
            throw new InvalidKeyException("Supplied key (" + getType(publicKey) + ") is not a RSAPublicKey instance");
        }

        CipherParameters    param = RSAUtil.generatePublicKeyParameter((RSAPublicKey)publicKey);

        digest.reset();
        cipher.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
        {
            byte[]  bytes = derEncode(hash);

            return cipher.processBlock(bytes, 0, bytes.length);
        }
        catch (ArrayIndexOutOfBoundsException e)
        {
            throw new SignatureException("key too small for signature type");
        }
        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);

        byte[]      sig;
        byte[]      expected;

        try
        {
            sig = cipher.processBlock(sigBytes, 0, sigBytes.length);

            expected = derEncode(hash);
        }
        catch (Exception e)
        {
            return false;
        }

        if (sig.length == expected.length)
        {
            for (int i = 0; i < sig.length; i++)
            {
                if (sig[i] != expected[i])
                {
                    return false;
                }
            }
        }
        else if (expected.length == sig.length - 2)  // NULL left out
        {
            int sigOffset = sig.length - hash.length - 2;
            int expectedOffset = expected.length - hash.length - 2;

            sig[1] -= 2;      // adjust lengths
            sig[3] -= 2;

            for (int i = 0; i < hash.length; i++)
            {
                if (sig[sigOffset + i] != expected[expectedOffset + i])  // check hash
                {
                    return false;
                }
            }

            for (int i = 0; i < expectedOffset; i++)
            {
                if (sig[i] != expected[i])  // check header less NULL
                {
                    return false;
                }
            }
        }
        else
        {
            return false;
        }

        return true;
    
private java.lang.StringgetType(java.lang.Object o)

        if (o == null)
        {
            return null;
        }
        
        return o.getClass().getName();