use of org.bouncycastle.openpgp.operator.bc.BcPBESecretKeyDecryptorBuilder in project ant-ivy by apache.
the class OpenPGPSignatureGenerator method sign.
public void sign(File src, File dest) throws IOException {
OutputStream out = null;
InputStream in = null;
InputStream keyIn = null;
try {
if (secring == null) {
secring = System.getProperty("user.home") + "/.gnupg/secring.gpg";
}
if (pgpSec == null) {
keyIn = new FileInputStream(secring);
pgpSec = readSecretKey(keyIn);
}
PBESecretKeyDecryptor decryptor = new BcPBESecretKeyDecryptorBuilder(new BcPGPDigestCalculatorProvider()).build(password.toCharArray());
PGPPrivateKey pgpPrivKey = pgpSec.extractPrivateKey(decryptor);
PGPSignatureGenerator sGen = new PGPSignatureGenerator(new BcPGPContentSignerBuilder(pgpSec.getPublicKey().getAlgorithm(), PGPUtil.SHA1));
sGen.init(PGPSignature.BINARY_DOCUMENT, pgpPrivKey);
in = new FileInputStream(src);
out = new BCPGOutputStream(new ArmoredOutputStream(new FileOutputStream(dest)));
int ch = 0;
while ((ch = in.read()) >= 0) {
sGen.update((byte) ch);
}
sGen.generate().encode(out);
} catch (PGPException e) {
throw new IOException(e);
} finally {
if (out != null) {
try {
out.close();
} catch (IOException e) {
}
}
if (in != null) {
try {
in.close();
} catch (IOException e) {
}
}
if (keyIn != null) {
try {
keyIn.close();
} catch (IOException e) {
}
}
}
}
Aggregations