Example usage for org.bouncycastle.openpgp PGPV3SignatureGenerator initSign

List of usage examples for org.bouncycastle.openpgp PGPV3SignatureGenerator initSign

Introduction

In this page you can find the example usage for org.bouncycastle.openpgp PGPV3SignatureGenerator initSign.

Prototype

public void initSign(int signatureType, PGPPrivateKey key) throws PGPException 

Source Link

Document

Initialise the generator for signing.

Usage

From source file:com.geekcommune.identity.EncryptionUtil.java

License:Open Source License

/**
 * UNUSED IN FRIENDLY BACKUP//from  w w  w  .  j  av  a 2  s  .co m
 * Sign the passed in message stream (version 3 signature)
 */
private void signDataV3(File inFile, OutputStream aOut, PGPPublicKey publicKey, PGPPrivateKey privateKey)
        throws PGPException {
    try {
        PGPCompressedDataGenerator cGen = new PGPCompressedDataGenerator(PGPCompressedData.ZIP);
        BCPGOutputStream bOut = new BCPGOutputStream(cGen.open(aOut));
        PGPLiteralDataGenerator lGen = new PGPLiteralDataGenerator(true);

        PGPV3SignatureGenerator s3Gen = new PGPV3SignatureGenerator(publicKey.getAlgorithm(), PGPUtil.SHA1,
                "BC");

        s3Gen.initSign(PGPSignature.BINARY_DOCUMENT, privateKey);

        s3Gen.generateOnePassVersion(false).encode(bOut);

        OutputStream lOut = lGen.open(bOut, PGPLiteralData.BINARY, inFile);

        FileInputStream fIn = new FileInputStream(inFile);

        int ch;
        while ((ch = fIn.read()) >= 0) {
            lOut.write(ch);
            s3Gen.update((byte) ch);
        }

        fIn.close();

        // close() finishes the writing of the literal data and flushes the stream
        // It does not close bOut so this is ok here
        lGen.close();

        // Generate the signature
        s3Gen.generate().encode(bOut);

        // Must not close bOut here
        bOut.finish();
        bOut.flush();

        cGen.close();
    } catch (PGPException e) {
        throw e;
    } catch (Exception e) {
        throw new PGPException("Error in signing", e);
    }
}