org.bouncycastle.openpgp.operator.jcajce.JcaPGPContentVerifierBuilderProvider Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of bcpg-jdk15on Show documentation
Show all versions of bcpg-jdk15on Show documentation
The Bouncy Castle Java API for handling the OpenPGP protocol. This jar contains the OpenPGP API for JDK 1.5 to JDK 1.7. The APIs can be used in conjunction with a JCE/JCA provider such as the one provided with the Bouncy Castle Cryptography APIs.
package org.bouncycastle.openpgp.operator.jcajce;
import java.io.OutputStream;
import java.security.InvalidKeyException;
import java.security.Provider;
import java.security.Signature;
import java.security.SignatureException;
import org.bouncycastle.jcajce.DefaultJcaJceHelper;
import org.bouncycastle.jcajce.NamedJcaJceHelper;
import org.bouncycastle.jcajce.ProviderJcaJceHelper;
import org.bouncycastle.openpgp.PGPException;
import org.bouncycastle.openpgp.PGPPublicKey;
import org.bouncycastle.openpgp.operator.PGPContentVerifier;
import org.bouncycastle.openpgp.operator.PGPContentVerifierBuilder;
import org.bouncycastle.openpgp.operator.PGPContentVerifierBuilderProvider;
public class JcaPGPContentVerifierBuilderProvider
implements PGPContentVerifierBuilderProvider
{
private OperatorHelper helper = new OperatorHelper(new DefaultJcaJceHelper());
private JcaPGPKeyConverter keyConverter = new JcaPGPKeyConverter();
public JcaPGPContentVerifierBuilderProvider()
{
}
public JcaPGPContentVerifierBuilderProvider setProvider(Provider provider)
{
this.helper = new OperatorHelper(new ProviderJcaJceHelper(provider));
keyConverter.setProvider(provider);
return this;
}
public JcaPGPContentVerifierBuilderProvider setProvider(String providerName)
{
this.helper = new OperatorHelper(new NamedJcaJceHelper(providerName));
keyConverter.setProvider(providerName);
return this;
}
public PGPContentVerifierBuilder get(int keyAlgorithm, int hashAlgorithm)
throws PGPException
{
return new JcaPGPContentVerifierBuilder(keyAlgorithm, hashAlgorithm);
}
private class JcaPGPContentVerifierBuilder
implements PGPContentVerifierBuilder
{
private int hashAlgorithm;
private int keyAlgorithm;
public JcaPGPContentVerifierBuilder(int keyAlgorithm, int hashAlgorithm)
{
this.keyAlgorithm = keyAlgorithm;
this.hashAlgorithm = hashAlgorithm;
}
public PGPContentVerifier build(final PGPPublicKey publicKey)
throws PGPException
{
final Signature signature = helper.createSignature(keyAlgorithm, hashAlgorithm);
try
{
signature.initVerify(keyConverter.getPublicKey(publicKey));
}
catch (InvalidKeyException e)
{
throw new PGPException("invalid key.", e);
}
return new PGPContentVerifier()
{
public int getHashAlgorithm()
{
return hashAlgorithm;
}
public int getKeyAlgorithm()
{
return keyAlgorithm;
}
public long getKeyID()
{
return publicKey.getKeyID();
}
public boolean verify(byte[] expected)
{
try
{
return signature.verify(expected);
}
catch (SignatureException e)
{ // TODO: need a specific runtime exception for PGP operators.
throw new IllegalStateException("unable to verify signature");
}
}
public OutputStream getOutputStream()
{
return new SignatureOutputStream(signature);
}
};
}
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy