public PGPContentSigner build(final int signatureType, PGPPrivateKey privateKey) throws PGPException { if (privateKey instanceof JcaPGPPrivateKey) { return build(signatureType, privateKey.getKeyID(), ((JcaPGPPrivateKey)privateKey).getPrivateKey()); } else { return build(signatureType, privateKey.getKeyID(), keyConverter.getPrivateKey(privateKey)); } }
public PGPContentSigner build(final int signatureType, final PGPPrivateKey privateKey) throws PGPException { final PGPDigestCalculator digestCalculator = digestCalculatorProvider.get(hashAlgorithm); final Signer signer = BcImplProvider.createSigner(keyAlgorithm, hashAlgorithm); if (random != null) { signer.init(true, new ParametersWithRandom(keyConverter.getPrivateKey(privateKey), random)); } else { signer.init(true, keyConverter.getPrivateKey(privateKey)); } 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 SignerOutputStream(signer), digestCalculator.getOutputStream()); } public byte[] getSignature() { try { return signer.generateSignature(); } catch (CryptoException e) { // TODO: need a specific runtime exception for PGP operators. throw new IllegalStateException("unable to create signature"); } } public byte[] getDigest() { return digestCalculator.getDigest(); } }; }