org.bouncycastle.bcpg.BCPGOutputStream Java Examples
The following examples show how to use
org.bouncycastle.bcpg.BCPGOutputStream.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: PGPSign.java From peer-os with Apache License 2.0 | 6 votes |
private static void produceSign( byte[] data, BCPGOutputStream bcOut, PGPSignatureGenerator signGen ) throws IOException, PGPException { PGPLiteralDataGenerator literalGen = new PGPLiteralDataGenerator(); OutputStream os = literalGen.open( bcOut, PGPLiteralData.BINARY, "", data.length, new Date() ); InputStream is = new ByteArrayInputStream( data ); int ch; while ( ( ch = is.read() ) >= 0 ) { signGen.update( ( byte ) ch ); os.write( ch ); } literalGen.close(); signGen.generate().encode( bcOut ); }
Example #2
Source File: AptSigningFacet.java From nexus-public with Eclipse Public License 1.0 | 6 votes |
public byte[] signExternal(final String input) throws IOException { ByteArrayOutputStream buffer = new ByteArrayOutputStream(); try { PGPSecretKey signKey = readSecretKey(); PGPPrivateKey privKey = signKey.extractPrivateKey( new JcePBESecretKeyDecryptorBuilder().setProvider("BC").build(config.passphrase.toCharArray())); PGPSignatureGenerator sigGenerator = new PGPSignatureGenerator( new JcaPGPContentSignerBuilder(signKey.getPublicKey().getAlgorithm(), PGPUtil.SHA256).setProvider("BC")); sigGenerator.init(PGPSignature.BINARY_DOCUMENT, privKey); try (ArmoredOutputStream aOut = new ArmoredOutputStream(buffer)) { BCPGOutputStream bOut = new BCPGOutputStream(aOut); sigGenerator.update(input.getBytes(Charsets.UTF_8)); sigGenerator.generate().encode(bOut); } } catch (PGPException ex) { throw new RuntimeException(ex); } return buffer.toByteArray(); }
Example #3
Source File: SigningStream.java From packagedrone with Eclipse Public License 1.0 | 6 votes |
@Override public void close () throws IOException { testInit (); if ( this.inline ) { this.armoredOutput.endClearText (); } try { final PGPSignature signature = this.signatureGenerator.generate (); signature.encode ( new BCPGOutputStream ( this.armoredOutput ) ); } catch ( final PGPException e ) { throw new IOException ( e ); } this.armoredOutput.close (); super.close (); }
Example #4
Source File: PGPSign.java From peer-os with Apache License 2.0 | 5 votes |
private static PGPSignatureGenerator getSignatureGenerator( PGPPrivateKey privateKey, BCPGOutputStream bcOut ) throws PGPException, IOException { PGPSignatureGenerator signGen = new PGPSignatureGenerator( new JcaPGPContentSignerBuilder( privateKey.getPublicKeyPacket().getAlgorithm(), PGPUtil.SHA1 ) .setProvider( "BC" ) ); signGen.init( PGPSignature.BINARY_DOCUMENT, privateKey ); signGen.generateOnePassVersion( false ).encode( bcOut ); return signGen; }
Example #5
Source File: AptSigningFacet.java From nexus-repository-apt with Eclipse Public License 1.0 | 5 votes |
public Content getPublicKey() throws IOException, PGPException { PGPSecretKey signKey = readSecretKey(); PGPPublicKey publicKey = signKey.getPublicKey(); ByteArrayOutputStream buffer = new ByteArrayOutputStream(); try (BCPGOutputStream os = new BCPGOutputStream(new ArmoredOutputStream(buffer))) { publicKey.encode(os); } return new Content(new BytesPayload(buffer.toByteArray(), AptMimeTypes.PUBLICKEY)); }
Example #6
Source File: AptSigningFacet.java From nexus-repository-apt with Eclipse Public License 1.0 | 5 votes |
public byte[] signInline(String input) throws IOException, PGPException { PGPSecretKey signKey = readSecretKey(); PGPPrivateKey privKey = signKey.extractPrivateKey( new JcePBESecretKeyDecryptorBuilder().setProvider("BC").build(config.passphrase.toCharArray())); PGPSignatureGenerator sigGenerator = new PGPSignatureGenerator( new JcaPGPContentSignerBuilder(signKey.getPublicKey().getAlgorithm(), PGPUtil.SHA256).setProvider("BC")); sigGenerator.init(PGPSignature.CANONICAL_TEXT_DOCUMENT, privKey); @SuppressWarnings("unchecked") Iterator<String> userIds = signKey.getUserIDs(); if (userIds.hasNext()) { PGPSignatureSubpacketGenerator sigSubpacketGenerator = new PGPSignatureSubpacketGenerator(); sigSubpacketGenerator.setSignerUserID(false, userIds.next()); sigGenerator.setHashedSubpackets(sigSubpacketGenerator.generate()); } String[] lines = input.split("\r?\n"); ByteArrayOutputStream buffer = new ByteArrayOutputStream(); try (ArmoredOutputStream aOut = new ArmoredOutputStream(buffer)) { aOut.beginClearText(PGPUtil.SHA256); boolean firstLine = true; for (String line : lines) { String sigLine = (firstLine ? "" : "\r\n") + line.replaceAll("\\s*$", ""); sigGenerator.update(sigLine.getBytes(Charsets.UTF_8)); aOut.write((line + "\n").getBytes(Charsets.UTF_8)); firstLine = false; } aOut.endClearText(); BCPGOutputStream bOut = new BCPGOutputStream(aOut); sigGenerator.generate().encode(bOut); } return buffer.toByteArray(); }
Example #7
Source File: AptSigningFacet.java From nexus-repository-apt with Eclipse Public License 1.0 | 5 votes |
public byte[] signExternal(String input) throws IOException, PGPException { PGPSecretKey signKey = readSecretKey(); PGPPrivateKey privKey = signKey.extractPrivateKey( new JcePBESecretKeyDecryptorBuilder().setProvider("BC").build(config.passphrase.toCharArray())); PGPSignatureGenerator sigGenerator = new PGPSignatureGenerator( new JcaPGPContentSignerBuilder(signKey.getPublicKey().getAlgorithm(), PGPUtil.SHA256).setProvider("BC")); sigGenerator.init(PGPSignature.BINARY_DOCUMENT, privKey); ByteArrayOutputStream buffer = new ByteArrayOutputStream(); try (ArmoredOutputStream aOut = new ArmoredOutputStream(buffer)) { BCPGOutputStream bOut = new BCPGOutputStream(aOut); sigGenerator.update(input.getBytes(Charsets.UTF_8)); sigGenerator.generate().encode(bOut); } return buffer.toByteArray(); }
Example #8
Source File: AptSigningFacet.java From nexus-public with Eclipse Public License 1.0 | 5 votes |
public Content getPublicKey() throws IOException { ByteArrayOutputStream buffer = new ByteArrayOutputStream(); PGPSecretKey signKey = readSecretKey(); PGPPublicKey publicKey = signKey.getPublicKey(); try (BCPGOutputStream os = new BCPGOutputStream(new ArmoredOutputStream(buffer))) { publicKey.encode(os); } return new Content(new BytesPayload(buffer.toByteArray(), AptMimeTypes.PUBLICKEY)); }
Example #9
Source File: AptSigningFacet.java From nexus-public with Eclipse Public License 1.0 | 5 votes |
public byte[] signInline(final String input) throws IOException { ByteArrayOutputStream buffer = new ByteArrayOutputStream(); try { PGPSecretKey signKey = readSecretKey(); PGPPrivateKey privKey = signKey.extractPrivateKey( new JcePBESecretKeyDecryptorBuilder().setProvider("BC").build(config.passphrase.toCharArray())); PGPSignatureGenerator sigGenerator = new PGPSignatureGenerator( new JcaPGPContentSignerBuilder(signKey.getPublicKey().getAlgorithm(), PGPUtil.SHA256).setProvider("BC")); sigGenerator.init(PGPSignature.CANONICAL_TEXT_DOCUMENT, privKey); Iterator<String> userIds = signKey.getUserIDs(); if (userIds.hasNext()) { PGPSignatureSubpacketGenerator sigSubpacketGenerator = new PGPSignatureSubpacketGenerator(); sigSubpacketGenerator.setSignerUserID(false, userIds.next()); sigGenerator.setHashedSubpackets(sigSubpacketGenerator.generate()); } String[] lines = input.split("\r?\n"); try (ArmoredOutputStream aOut = new ArmoredOutputStream(buffer)) { aOut.beginClearText(PGPUtil.SHA256); boolean firstLine = true; for (String line : lines) { String sigLine = (firstLine ? "" : "\r\n") + line.replaceAll("\\s*$", ""); sigGenerator.update(sigLine.getBytes(Charsets.UTF_8)); aOut.write((line + "\n").getBytes(Charsets.UTF_8)); firstLine = false; } aOut.endClearText(); BCPGOutputStream bOut = new BCPGOutputStream(aOut); sigGenerator.generate().encode(bOut); } } catch (PGPException ex) { throw new RuntimeException(ex); } return buffer.toByteArray(); }
Example #10
Source File: AbstractSecretKeySigningService.java From packagedrone with Eclipse Public License 1.0 | 5 votes |
@Override public void printPublicKey ( final OutputStream out ) throws IOException { final ArmoredOutputStream armoredOutput = new ArmoredOutputStream ( out ); armoredOutput.setHeader ( "Version", VersionInformation.VERSIONED_PRODUCT ); final PGPPublicKey pubKey = this.secretKey.getPublicKey (); pubKey.encode ( new BCPGOutputStream ( armoredOutput ) ); armoredOutput.close (); }
Example #11
Source File: PGPSign.java From peer-os with Apache License 2.0 | 4 votes |
public static byte[] sign( byte data[], PGPPrivateKey privateKey ) throws IOException, PGPException { ByteArrayOutputStream bos = new ByteArrayOutputStream(); ArmoredOutputStream aos = new ArmoredOutputStream( bos ); PGPCompressedDataGenerator compressGen = new PGPCompressedDataGenerator( PGPCompressedData.ZLIB ); BCPGOutputStream bcOut = new BCPGOutputStream( compressGen.open( aos ) ); PGPSignatureGenerator signGen = getSignatureGenerator( privateKey, bcOut ); produceSign( data, bcOut, signGen ); compressGen.close(); aos.close(); return bos.toByteArray(); }
Example #12
Source File: PGPEncryptionUtil.java From peer-os with Apache License 2.0 | 4 votes |
public static byte[] sign( byte[] message, PGPSecretKey secretKey, String secretPwd, boolean armor ) throws PGPException { try { ByteArrayOutputStream out = new ByteArrayOutputStream(); OutputStream theOut = armor ? new ArmoredOutputStream( out ) : out; PGPPrivateKey pgpPrivKey = secretKey.extractPrivateKey( new JcePBESecretKeyDecryptorBuilder().setProvider( provider ).build( secretPwd.toCharArray() ) ); PGPSignatureGenerator sGen = new PGPSignatureGenerator( new JcaPGPContentSignerBuilder( secretKey.getPublicKey().getAlgorithm(), PGPUtil.SHA1 ) .setProvider( provider ) ); sGen.init( PGPSignature.BINARY_DOCUMENT, pgpPrivKey ); Iterator it = secretKey.getPublicKey().getUserIDs(); if ( it.hasNext() ) { PGPSignatureSubpacketGenerator spGen = new PGPSignatureSubpacketGenerator(); spGen.setSignerUserID( false, ( String ) it.next() ); sGen.setHashedSubpackets( spGen.generate() ); } PGPCompressedDataGenerator cGen = new PGPCompressedDataGenerator( PGPCompressedData.ZLIB ); BCPGOutputStream bOut = new BCPGOutputStream( cGen.open( theOut ) ); sGen.generateOnePassVersion( false ).encode( bOut ); PGPLiteralDataGenerator lGen = new PGPLiteralDataGenerator(); OutputStream lOut = lGen.open( bOut, PGPLiteralData.BINARY, "filename", new Date(), new byte[4096] ); // InputStream fIn = new ByteArrayInputStream( message ); int ch; while ( ( ch = fIn.read() ) >= 0 ) { lOut.write( ch ); sGen.update( ( byte ) ch ); } lGen.close(); sGen.generate().encode( bOut ); cGen.close(); theOut.close(); return out.toByteArray(); } catch ( Exception e ) { throw new PGPException( "Error in sign", e ); } }
Example #13
Source File: PGPEncryptionUtil.java From peer-os with Apache License 2.0 | 4 votes |
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 ); // // note the last \n/\r/\r\n in the file is ignored // 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(); }
Example #14
Source File: AbstractSecretKeySigningService.java From packagedrone with Eclipse Public License 1.0 | 4 votes |
@Override public void sign ( final InputStream in, final OutputStream out, final boolean inline ) throws Exception { final int digest = HashAlgorithmTags.SHA1; final PGPSignatureGenerator signatureGenerator = new PGPSignatureGenerator ( new BcPGPContentSignerBuilder ( this.privateKey.getPublicKeyPacket ().getAlgorithm (), digest ) ); if ( inline ) { signatureGenerator.init ( PGPSignature.CANONICAL_TEXT_DOCUMENT, this.privateKey ); } else { signatureGenerator.init ( PGPSignature.BINARY_DOCUMENT, this.privateKey ); } final ArmoredOutputStream armoredOutput = new ArmoredOutputStream ( out ); armoredOutput.setHeader ( "Version", VersionInformation.VERSIONED_PRODUCT ); if ( inline ) { armoredOutput.beginClearText ( digest ); final LineNumberReader lnr = new LineNumberReader ( new InputStreamReader ( in, StandardCharsets.UTF_8 ) ); String line; while ( ( line = lnr.readLine () ) != null ) { if ( lnr.getLineNumber () > 1 ) { signatureGenerator.update ( NL_DATA ); } final byte[] data = trimTrailing ( line ).getBytes ( StandardCharsets.UTF_8 ); if ( inline ) { armoredOutput.write ( data ); armoredOutput.write ( NL_DATA ); } signatureGenerator.update ( data ); } armoredOutput.endClearText (); } else { final byte[] buffer = new byte[4096]; int rc; while ( ( rc = in.read ( buffer ) ) >= 0 ) { signatureGenerator.update ( buffer, 0, rc ); } } final PGPSignature signature = signatureGenerator.generate (); signature.encode ( new BCPGOutputStream ( armoredOutput ) ); armoredOutput.close (); }