}
public PGPContentSigner build(final int signatureType, final PGPPrivateKey privateKey)
throws PGPException
{
final PGPDigestCalculator digestCalculator = digestCalculatorProviderBuilder.build().get(hashAlgorithm);
final Signature signature = helper.createSignature(keyAlgorithm, hashAlgorithm);
try
{
if (random != null)
{
signature.initSign(keyConverter.getPrivateKey(privateKey), random);
}
else
{
signature.initSign(keyConverter.getPrivateKey(privateKey));
}
}
catch (InvalidKeyException e)
{
throw new PGPException("invalid key.", e);
}
return new PGPContentSigner()
{
public int getType()
{
return signatureType;
}
public int getHashAlgorithm()
{
return hashAlgorithm;
}
public int getKeyAlgorithm()
{
return keyAlgorithm;
}
public long getKeyID()
{
return privateKey.getKeyID();
}
public OutputStream getOutputStream()
{
return new TeeOutputStream(new SignatureOutputStream(signature), digestCalculator.getOutputStream());
}
public byte[] getSignature()
{
try
{
return signature.sign();
}
catch (SignatureException e)
{ // TODO: need a specific runtime exception for PGP operators.
throw new IllegalStateException("unable to create signature");
}
}
public byte[] getDigest()
{
return digestCalculator.getDigest();
}
};
}