FileDocCategorySizeDatePackage
JDKDigestSignature.javaAPI DocAzureus 3.0.3.47574Tue Jun 08 05:12:56 BST 2004org.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, null);
    
Methods Summary
private org.bouncycastle.asn1.x509.DigestInfoderDecode(byte[] encoding)

        ByteArrayInputStream    bIn = new ByteArrayInputStream(encoding);
        DERInputStream          dIn = new DERInputStream(bIn);

        return new DigestInfo((ASN1Sequence)dIn.readObject());
    
private byte[]derEncode(byte[] hash)

        ByteArrayOutputStream   bOut = new ByteArrayOutputStream();
        DEROutputStream         dOut = new DEROutputStream(bOut);
        DigestInfo              dInfo = new DigestInfo(algId, hash);

        dOut.writeObject(dInfo);

        return bOut.toByteArray();
    
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 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 is not a RSAPublicKey instance");
		}

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

        digest.reset();
        cipher.init(false, param);
    
protected voidengineSetParameter(java.security.spec.AlgorithmParameterSpec params)

        throw new UnsupportedOperationException("engineSetParameter unsupported");
    
protected voidengineSetParameter(java.lang.String param, java.lang.Object value)

deprecated
replaced with

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

        DigestInfo  digInfo;
        byte[]      sig;

        try
        {
            sig = cipher.processBlock(sigBytes, 0, sigBytes.length);
            digInfo = derDecode(sig);
        }
        catch (Exception e)
        {
            return false;
        }

        if (!digInfo.getAlgorithmId().equals(algId))
        {
            return false;
        }

        byte[]  sigHash = digInfo.getDigest();

        if (hash.length != sigHash.length)
        {
            return false;
        }

        for (int i = 0; i < hash.length; i++)
        {
            if (sigHash[i] != hash[i])
            {
                return false;
            }
        }

        return true;