Example usage for org.bouncycastle.openpgp PGPPrivateKey getPublicKeyPacket

List of usage examples for org.bouncycastle.openpgp PGPPrivateKey getPublicKeyPacket

Introduction

In this page you can find the example usage for org.bouncycastle.openpgp PGPPrivateKey getPublicKeyPacket.

Prototype

public PublicKeyPacket getPublicKeyPacket() 

Source Link

Document

Return the public key packet associated with this private key, if available.

Usage

From source file:google.registry.keyring.api.ComparatorKeyring.java

License:Open Source License

@VisibleForTesting
static boolean compare(@Nullable PGPPrivateKey a, @Nullable PGPPrivateKey b) {
    if (a == null || b == null) {
        return a == null && b == null;
    }/*from  ww w. ja v a2 s .  co m*/
    return a.getKeyID() == b.getKeyID() && compare(a.getPrivateKeyDataPacket(), b.getPrivateKeyDataPacket())
            && compare(a.getPublicKeyPacket(), b.getPublicKeyPacket());
}

From source file:org.elasticsearch.plugins.InstallPluginCommandTests.java

License:Apache License

private String signature(final byte[] bytes, final PGPSecretKey secretKey) {
    try {//from  w  ww.  j  a  v a2s.c o m
        final PGPPrivateKey privateKey = secretKey.extractPrivateKey(
                new BcPBESecretKeyDecryptorBuilder(new JcaPGPDigestCalculatorProviderBuilder().build())
                        .build("passphrase".toCharArray()));
        final PGPSignatureGenerator generator = new PGPSignatureGenerator(new BcPGPContentSignerBuilder(
                privateKey.getPublicKeyPacket().getAlgorithm(), HashAlgorithmTags.SHA512));
        generator.init(PGPSignature.BINARY_DOCUMENT, privateKey);
        final ByteArrayOutputStream output = new ByteArrayOutputStream();
        try (BCPGOutputStream pout = new BCPGOutputStream(new ArmoredOutputStream(output));
                InputStream is = new ByteArrayInputStream(bytes)) {
            final byte[] buffer = new byte[1024];
            int read;
            while ((read = is.read(buffer)) != -1) {
                generator.update(buffer, 0, read);
            }
            generator.generate().encode(pout);
        }
        return new String(output.toByteArray(), "UTF-8");
    } catch (IOException | PGPException e) {
        throw new RuntimeException(e);
    }
}

From source file:org.sufficientlysecure.keychain.pgp.UncachedKeyRing.java

License:Open Source License

@VisibleForTesting
public static UncachedKeyRing forTestingOnlyAddDummyLocalSignature(UncachedKeyRing uncachedKeyRing,
        String passphrase) throws Exception {
    PGPSecretKeyRing sKR = (PGPSecretKeyRing) uncachedKeyRing.mRing;

    PBESecretKeyDecryptor keyDecryptor = new JcePBESecretKeyDecryptorBuilder()
            .setProvider(Constants.BOUNCY_CASTLE_PROVIDER_NAME).build(passphrase.toCharArray());
    PGPPrivateKey masterPrivateKey = sKR.getSecretKey().extractPrivateKey(keyDecryptor);
    PGPPublicKey masterPublicKey = uncachedKeyRing.mRing.getPublicKey();

    // add packet with "pin" notation data
    PGPContentSignerBuilder signerBuilder = new JcaPGPContentSignerBuilder(
            masterPrivateKey.getPublicKeyPacket().getAlgorithm(),
            PgpSecurityConstants.SECRET_KEY_BINDING_SIGNATURE_HASH_ALGO)
                    .setProvider(Constants.BOUNCY_CASTLE_PROVIDER_NAME);
    PGPSignatureGenerator sGen = new PGPSignatureGenerator(signerBuilder);
    { // set subpackets
        PGPSignatureSubpacketGenerator hashedPacketsGen = new PGPSignatureSubpacketGenerator();
        hashedPacketsGen.setExportable(false, false);
        hashedPacketsGen.setNotationData(false, true, "dummynotationdata", "some data");
        sGen.setHashedSubpackets(hashedPacketsGen.generate());
    }/*from  w w  w .ja  v  a2s . c  o m*/
    sGen.init(PGPSignature.DIRECT_KEY, masterPrivateKey);
    PGPSignature emptySig = sGen.generateCertification(masterPublicKey);

    masterPublicKey = PGPPublicKey.addCertification(masterPublicKey, emptySig);
    sKR = PGPSecretKeyRing.insertSecretKey(sKR,
            PGPSecretKey.replacePublicKey(sKR.getSecretKey(), masterPublicKey));

    return new UncachedKeyRing(sKR);
}