PGPEncryptionUtil { public static byte[] clearSign( byte[] message, PGPSecretKey pgpSecKey, char[] pass, String digestName ) throws IOException, PGPException, SignatureException { ByteArrayOutputStream out = new ByteArrayOutputStream(); int digest; if ( "SHA256".equals( digestName ) ) { digest = PGPUtil.SHA256; } else if ( "SHA384".equals( digestName ) ) { digest = PGPUtil.SHA384; } else if ( "SHA512".equals( digestName ) ) { digest = PGPUtil.SHA512; } else if ( "MD5".equals( digestName ) ) { digest = PGPUtil.MD5; } else if ( "RIPEMD160".equals( digestName ) ) { digest = PGPUtil.RIPEMD160; } else { digest = PGPUtil.SHA1; } PGPPrivateKey pgpPrivKey = pgpSecKey.extractPrivateKey( new JcePBESecretKeyDecryptorBuilder().setProvider( "BC" ).build( pass ) ); PGPSignatureGenerator sGen = new PGPSignatureGenerator( new JcaPGPContentSignerBuilder( pgpSecKey.getPublicKey().getAlgorithm(), digest ).setProvider( "BC" ) ); PGPSignatureSubpacketGenerator spGen = new PGPSignatureSubpacketGenerator(); sGen.init( PGPSignature.CANONICAL_TEXT_DOCUMENT, pgpPrivKey ); Iterator it = pgpSecKey.getPublicKey().getUserIDs(); if ( it.hasNext() ) { spGen.setSignerUserID( false, ( String ) it.next() ); sGen.setHashedSubpackets( spGen.generate() ); } InputStream fIn = new ByteArrayInputStream( message ); ArmoredOutputStream aOut = new ArmoredOutputStream( out ); aOut.beginClearText( digest ); ByteArrayOutputStream lineOut = new ByteArrayOutputStream(); int lookAhead = readInputLine( lineOut, fIn ); processLine( aOut, sGen, lineOut.toByteArray() ); if ( lookAhead != -1 ) { do { lookAhead = readInputLine( lineOut, lookAhead, fIn ); sGen.update( ( byte ) '\r' ); sGen.update( ( byte ) '\n' ); processLine( aOut, sGen, lineOut.toByteArray() ); } while ( lookAhead != -1 ); } fIn.close(); aOut.endClearText(); BCPGOutputStream bOut = new BCPGOutputStream( aOut ); sGen.generate().encode( bOut ); aOut.close(); return out.toByteArray(); } private  PGPEncryptionUtil(); static byte[] encrypt( final byte[] message, final PGPPublicKey publicKey, boolean armored ); static byte[] decrypt( final byte[] encryptedMessage, final PGPSecretKeyRing secretKeyRing,
                                  final String secretPwd ); static byte[] decrypt( final byte[] encryptedMessage, final InputStream secretKeyRing,
                                  final String secretPwd ); static boolean verifySignature( ContentAndSignatures contentAndSignatures, PGPPublicKey publicKey ); static ContentAndSignatures decryptAndReturnSignatures( final byte[] encryptedMessage,
                                                                   final PGPSecretKeyRing secretKeyRing,
                                                                   final String secretPwd ); static byte[] decryptAndVerify( byte[] encryptedMessage, final PGPSecretKey secretKey,
                                           final String secretPwd, final PGPPublicKey publicKey ); static byte[] signAndEncrypt( final byte[] message, final PGPSecretKey secretKey, final String secretPwd,
                                         final PGPPublicKey publicKey, final boolean armored ); static KeyPair generateKeyPair( String userId, String secretPwd, boolean armored ); static String BytesToHex( byte[] bytes ); static byte[] armorByteArray( byte[] data ); static boolean verify( byte[] signedMessage, PGPPublicKey publicKey ); static byte[] sign( byte[] message, PGPSecretKey secretKey, String secretPwd, boolean armor ); static byte[] extractContentFromClearSign( byte[] signedMessage ); static boolean verifyClearSign( byte[] message, PGPPublicKeyRing pgpRings ); static byte[] clearSign( byte[] message, PGPSecretKey pgpSecKey, char[] pass, String digestName ); static PGPPublicKey findPublicKeyById( InputStream publicKeyRing, String keyId ); static PGPPublicKey findPublicKeyByFingerprint( InputStream publicKeyRing, String fingerprint ); static PGPSecretKey findSecretKeyById( InputStream secretKeyRing, String keyId ); static PGPSecretKey findSecretKeyByFingerprint( InputStream secretKeyRing, String fingerprint ); static X509Certificate getX509CertificateFromPgpKeyPair( PGPPublicKey pgpPublicKey,
                                                                    PGPSecretKey pgpSecretKey, String secretPwd,
                                                                    String issuer, String subject, Date dateOfIssue,
                                                                    Date dateOfExpiry, BigInteger serial ); static PGPPrivateKey getPrivateKey( final PGPSecretKey secretKey, final String secretPwd ); static String armorByteArrayToString( byte[] data ); static PGPPublicKeyRing signPublicKey( PGPPublicKeyRing publicKeyRing, String id, PGPSecretKey secretKey,
                                                  String secretKeyPassword ); static boolean verifyPublicKey( PGPPublicKey keyToVerify, String id, PGPPublicKey keyToVerifyWith ); static PGPPublicKeyRing removeSignature( PGPPublicKeyRing keyToRemoveFrom, String id ); static PGPPublicKeyRing removeSignature( PGPPublicKeyRing keyToRemoveFrom,
                                                    PGPPublicKey keySignatureToRemove );  }
@Test public void testClearSign() throws Exception { InputStream secondSecretStream = findFile( PLUGIN_PRIVATE_KEY ); InputStream secondPublicStream = findFile( PLUGIN_PUBLIC_KEY ); PGPSecretKeyRingCollection secretKeyRingCollection = new PGPSecretKeyRingCollection( PGPUtil.getDecoderStream( secondSecretStream ), new JcaKeyFingerprintCalculator() ); PGPSecretKeyRing secretKeyRing = secretKeyRingCollection .getSecretKeyRing( secretKeyRingCollection.iterator().next().getPublicKey().getKeyID() ); PGPSecretKey secondSecretKey = secretKeyRing.getSecretKey(); PGPPublicKeyRingCollection secondPublicKeyRingCollection = new PGPPublicKeyRingCollection( PGPUtil.getDecoderStream( secondPublicStream ), new JcaKeyFingerprintCalculator() ); PGPPublicKeyRing pgpKeyring = secondPublicKeyRingCollection .getPublicKeyRing( secondPublicKeyRingCollection.iterator().next().getPublicKey().getKeyID() ); byte[] signedMessageArmor = PGPEncryptionUtil .clearSign( IOUtils.toString( findFile( "message.txt" ) ).getBytes(), secondSecretKey, "123".toCharArray(), "" ); String signedMessage = new String( signedMessageArmor, StandardCharsets.UTF_8 ); logger.info( "\n" + signedMessage ); boolean result = PGPEncryptionUtil.verifyClearSign( signedMessage.getBytes(), pgpKeyring ); if ( result ) { logger.info( "signature verified." ); } else { logger.info( "signature verification failed." ); } assertEquals( true, result ); }