private static boolean Verify ( InputStream isSignature , InputStream isMessage , InputStream isPublicKey ) throws LRException { // Get the public key ring collection from the public key input stream PGPPublicKeyRingCollection pgpRings = null ; try { pgpRings = new PGPPublicKeyRingCollection ( PGPUtil . getDecoderStream ( isPublicKey ) ) ; } catch ( Exception e ) { throw new LRException ( LRException . INVALID_PUBLIC_KEY ) ; } // Add the Bouncy Castle security provider Security . addProvider ( new BouncyCastleProvider ( ) ) ; // Build an output stream from the message for verification boolean verify = false ; int ch ; ByteArrayOutputStream bOut = new ByteArrayOutputStream ( ) ; ArmoredInputStream aIn = null ; try { aIn = new ArmoredInputStream ( isMessage ) ; // We are making no effort to clean the input for this example // If this turns into a fully-featured verification utility in a future version, this will need to be handled while ( ( ch = aIn . read ( ) ) >= 0 && aIn . isClearText ( ) ) { bOut . write ( ( byte ) ch ) ; } bOut . close ( ) ; } catch ( Exception e ) { throw new LRException ( LRException . MESSAGE_INVALID ) ; } // Build an object factory from the signature input stream and try to get an object out of it Object o = null ; try { PGPObjectFactory pgpFact = new PGPObjectFactory ( PGPUtil . getDecoderStream ( isSignature ) ) ; o = pgpFact . nextObject ( ) ; } catch ( Exception e ) { throw new LRException ( LRException . SIGNATURE_INVALID ) ; } // Check if the object we fetched is a signature list and if it is, get the signature and use it to verfiy try { if ( o instanceof PGPSignatureList ) { PGPSignatureList list = ( PGPSignatureList ) o ; if ( list . size ( ) > 0 ) { PGPSignature sig = list . get ( 0 ) ; PGPPublicKey publicKey = pgpRings . getPublicKey ( sig . getKeyID ( ) ) ; sig . init ( new JcaPGPContentVerifierBuilderProvider ( ) . setProvider ( "BC" ) , publicKey ) ; sig . update ( bOut . toByteArray ( ) ) ; verify = sig . verify ( ) ; } } } catch ( Exception e ) { throw new LRException ( LRException . SIGNATURE_NOT_FOUND ) ; } return verify ; }
Verfies that the provided message and signature using the public key