本文整理汇总了Java中org.bouncycastle.openpgp.PGPSignatureGenerator.init方法的典型用法代码示例。如果您正苦于以下问题:Java PGPSignatureGenerator.init方法的具体用法?Java PGPSignatureGenerator.init怎么用?Java PGPSignatureGenerator.init使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.bouncycastle.openpgp.PGPSignatureGenerator
的用法示例。
在下文中一共展示了PGPSignatureGenerator.init方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: signArmoredAscii
import org.bouncycastle.openpgp.PGPSignatureGenerator; //导入方法依赖的package包/类
static String signArmoredAscii(PGPPrivateKey privateKey, String data, int signatureAlgo) throws IOException, PGPException {
String signature = null;
final PGPSignatureGenerator signatureGenerator = new PGPSignatureGenerator(new BcPGPContentSignerBuilder(privateKey.getPublicKeyPacket().getAlgorithm(), signatureAlgo));
signatureGenerator.init(PGPSignature.BINARY_DOCUMENT, privateKey);
ByteArrayOutputStream signatureOutput = new ByteArrayOutputStream();
try( BCPGOutputStream outputStream = new BCPGOutputStream( new ArmoredOutputStream(signatureOutput)) ) {
Utils.processStringAsStream(data, new StreamHandler() {
@Override
public void handleStreamBuffer(byte[] buffer, int offset, int length) throws IOException {
signatureGenerator.update(buffer, offset, length);
}
});
signatureGenerator.generate().encode(outputStream);
}
signature = new String(signatureOutput.toByteArray(), "UTF-8");
return signature;
}
示例2: signExternal
import org.bouncycastle.openpgp.PGPSignatureGenerator; //导入方法依赖的package包/类
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();
}
示例3: createSignature
import org.bouncycastle.openpgp.PGPSignatureGenerator; //导入方法依赖的package包/类
private void createSignature(OutputStream out) throws Exception {
PGPSecretKey pgpSec = readSecretKey();
PGPPrivateKey pgpPrivKey = pgpSec.extractPrivateKey(new JcePBESecretKeyDecryptorBuilder().setProvider(getProvider()).build(
"sdude".toCharArray()));
PGPSignatureGenerator sGen = new PGPSignatureGenerator(new JcaPGPContentSignerBuilder(pgpSec.getPublicKey().getAlgorithm(),
HashAlgorithmTags.SHA1).setProvider(getProvider()));
sGen.init(PGPSignature.BINARY_DOCUMENT, pgpPrivKey);
BCPGOutputStream bOut = new BCPGOutputStream(out);
InputStream fIn = new ByteArrayInputStream("Test Signature".getBytes("UTF-8"));
int ch;
while ((ch = fIn.read()) >= 0) {
sGen.update((byte) ch);
}
fIn.close();
sGen.generate().encode(bOut);
}
示例4: createSignatureGenerator
import org.bouncycastle.openpgp.PGPSignatureGenerator; //导入方法依赖的package包/类
public PGPSignatureGenerator createSignatureGenerator() {
try {
PGPSignatureGenerator generator = new PGPSignatureGenerator(new BcPGPContentSignerBuilder(secretKey.getPublicKey().getAlgorithm(), PGPUtil.SHA1));
generator.init(PGPSignature.BINARY_DOCUMENT, privateKey);
return generator;
} catch (PGPException e) {
throw new UncheckedException(e);
}
}
示例5: buildSigner
import org.bouncycastle.openpgp.PGPSignatureGenerator; //导入方法依赖的package包/类
/**
* Builds a PGPSignatureGenerator for the specified key and content.
*/
protected PGPSignatureGenerator buildSigner(Key key, FileMetadata meta)
throws PGPException {
Subkey subkey = key.getSigning();
if (log.isLoggable(Level.INFO))
log.info("using signing key " + subkey);
PGPContentSignerBuilder builder = buildSignerBuilder(
subkey.getPublicKey().getAlgorithm(),
signingAlgorithm.ordinal()
);
PGPSignatureGenerator generator = new PGPSignatureGenerator(builder);
generator.init(meta.getSignatureType(), subkey.getPrivateKey());
String uid = key.getSigningUid();
if (!Util.isEmpty(uid)) {
if (log.isLoggable(Level.FINE))
log.fine("using signing uid " + uid);
PGPSignatureSubpacketGenerator signer =
new PGPSignatureSubpacketGenerator();
signer.setSignerUserID(false, uid);
generator.setHashedSubpackets(signer.generate());
}
return generator;
}
示例6: signPublicKey
import org.bouncycastle.openpgp.PGPSignatureGenerator; //导入方法依赖的package包/类
/**
* Signs a public key
*
* @param publicKeyRing a public key ring containing the single public key to sign
* @param id the id we are certifying against the public key
* @param secretKey the signing key
* @param secretKeyPassword the signing key password
*
* @return a public key ring with the signed public key
*/
public static PGPPublicKeyRing signPublicKey( PGPPublicKeyRing publicKeyRing, String id, PGPSecretKey secretKey,
String secretKeyPassword ) throws PGPException
{
try
{
PGPPublicKey oldKey = publicKeyRing.getPublicKey();
PGPPrivateKey pgpPrivKey = secretKey.extractPrivateKey(
new JcePBESecretKeyDecryptorBuilder().setProvider( provider )
.build( secretKeyPassword.toCharArray() ) );
PGPSignatureGenerator signatureGenerator = new PGPSignatureGenerator(
new JcaPGPContentSignerBuilder( secretKey.getPublicKey().getAlgorithm(), PGPUtil.SHA1 ) );
signatureGenerator.init( PGPSignature.DEFAULT_CERTIFICATION, pgpPrivKey );
PGPSignature signature = signatureGenerator.generateCertification( id, oldKey );
PGPPublicKey newKey = PGPPublicKey.addCertification( oldKey, signature );
PGPPublicKeyRing newPublicKeyRing = PGPPublicKeyRing.removePublicKey( publicKeyRing, oldKey );
return PGPPublicKeyRing.insertPublicKey( newPublicKeyRing, newKey );
}
catch ( Exception e )
{
//throw custom exception
throw new PGPException( "Error signing public key", e );
}
}
示例7: getSignatureGenerator
import org.bouncycastle.openpgp.PGPSignatureGenerator; //导入方法依赖的package包/类
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;
}
示例8: RydePgpSigningOutputStream
import org.bouncycastle.openpgp.PGPSignatureGenerator; //导入方法依赖的package包/类
/**
* Create a signer that wraps {@code os} and generates a detached signature using
* {@code signingKey}. After closing, you should call {@link #getSignature()} to get the detached
* signature.
*
* @param os is the upstream {@link OutputStream} which is not closed by this object
* @throws RuntimeException to rethrow {@link PGPException}
*/
public RydePgpSigningOutputStream(
@WillNotClose OutputStream os,
PGPKeyPair signingKey) {
super(os, false, -1);
try {
signer = new PGPSignatureGenerator(
new BcPGPContentSignerBuilder(RSA_GENERAL, SHA256));
signer.init(BINARY_DOCUMENT, signingKey.getPrivateKey());
} catch (PGPException e) {
throw new RuntimeException(e);
}
addUserInfoToSignature(signingKey.getPublicKey(), signer);
}
示例9: testSignVerify_Detached
import org.bouncycastle.openpgp.PGPSignatureGenerator; //导入方法依赖的package包/类
@Test
public void testSignVerify_Detached() throws Exception {
// Load the keys.
PGPPublicKeyRing publicKeyRing = new BcPGPPublicKeyRing(PUBLIC_KEY);
PGPSecretKeyRing privateKeyRing = new BcPGPSecretKeyRing(PRIVATE_KEY);
PGPPublicKey publicKey = publicKeyRing.getPublicKey();
PGPPrivateKey privateKey = extractPrivateKey(privateKeyRing.getSecretKey());
// Sign the data and write signature data to "signatureFile".
// Note: RSA_GENERAL will encrypt AND sign. RSA_SIGN and RSA_ENCRYPT are deprecated.
PGPSignatureGenerator signer = new PGPSignatureGenerator(
new BcPGPContentSignerBuilder(RSA_GENERAL, SHA256));
signer.init(PGPSignature.BINARY_DOCUMENT, privateKey);
addUserInfoToSignature(publicKey, signer);
signer.update(FALL_OF_HYPERION_A_DREAM.getBytes(UTF_8));
ByteArrayOutputStream output = new ByteArrayOutputStream();
signer.generate().encode(output);
byte[] signatureFileData = output.toByteArray();
logger.info(".sig file data: " + dumpHex(signatureFileData));
// Load algorithm information and signature data from "signatureFileData".
PGPSignature sig;
try (ByteArrayInputStream input = new ByteArrayInputStream(signatureFileData)) {
PGPObjectFactory pgpFact = new BcPGPObjectFactory(input);
PGPSignatureList sigList = (PGPSignatureList) pgpFact.nextObject();
assertThat(sigList.size()).isEqualTo(1);
sig = sigList.get(0);
}
// Use "onePass" and "sig" to verify "publicKey" signed the text.
sig.init(new BcPGPContentVerifierBuilderProvider(), publicKey);
sig.update(FALL_OF_HYPERION_A_DREAM.getBytes(UTF_8));
assertThat(sig.verify()).isTrue();
// Verify that they DIDN'T sign the text "hello monster".
sig.init(new BcPGPContentVerifierBuilderProvider(), publicKey);
sig.update("hello monster".getBytes(UTF_8));
assertThat(sig.verify()).isFalse();
}
示例10: signInline
import org.bouncycastle.openpgp.PGPSignatureGenerator; //导入方法依赖的package包/类
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();
}
示例11: createSignatureGenerator
import org.bouncycastle.openpgp.PGPSignatureGenerator; //导入方法依赖的package包/类
protected List<PGPSignatureGenerator> createSignatureGenerator(Exchange exchange, OutputStream out) throws Exception { //NOPMD
if (secretKeyAccessor == null) {
return null;
}
List<String> sigKeyUserids = determineSignaturenUserIds(exchange);
List<PGPSecretKeyAndPrivateKeyAndUserId> sigSecretKeysWithPrivateKeyAndUserId = secretKeyAccessor.getSignerKeys(exchange,
sigKeyUserids);
if (sigSecretKeysWithPrivateKeyAndUserId.isEmpty()) {
return null;
}
exchange.getOut().setHeader(NUMBER_OF_SIGNING_KEYS, Integer.valueOf(sigSecretKeysWithPrivateKeyAndUserId.size()));
List<PGPSignatureGenerator> sigGens = new ArrayList<PGPSignatureGenerator>();
for (PGPSecretKeyAndPrivateKeyAndUserId sigSecretKeyWithPrivateKeyAndUserId : sigSecretKeysWithPrivateKeyAndUserId) {
PGPPrivateKey sigPrivateKey = sigSecretKeyWithPrivateKeyAndUserId.getPrivateKey();
PGPSignatureSubpacketGenerator spGen = new PGPSignatureSubpacketGenerator();
spGen.setSignerUserID(false, sigSecretKeyWithPrivateKeyAndUserId.getUserId());
int algorithm = sigSecretKeyWithPrivateKeyAndUserId.getSecretKey().getPublicKey().getAlgorithm();
PGPSignatureGenerator sigGen = new PGPSignatureGenerator(
new JcaPGPContentSignerBuilder(algorithm, findHashAlgorithm(exchange)).setProvider(getProvider()));
sigGen.init(PGPSignature.BINARY_DOCUMENT, sigPrivateKey);
sigGen.setHashedSubpackets(spGen.generate());
sigGen.generateOnePassVersion(false).encode(out);
sigGens.add(sigGen);
}
return sigGens;
}
示例12: feedHeader
import org.bouncycastle.openpgp.PGPSignatureGenerator; //导入方法依赖的package包/类
@Override
public void feedHeader ( final ByteBuffer header )
{
try
{
final BcPGPContentSignerBuilder contentSignerBuilder = new BcPGPContentSignerBuilder ( this.privateKey.getPublicKeyPacket ().getAlgorithm (), this.hashAlgorithm );
final PGPSignatureGenerator signatureGenerator = new PGPSignatureGenerator ( contentSignerBuilder );
signatureGenerator.init ( PGPSignature.BINARY_DOCUMENT, this.privateKey );
if ( header.hasArray () )
{
signatureGenerator.update ( header.array (), header.position (), header.remaining () );
}
else
{
final byte[] buffer = new byte[header.remaining ()];
header.get ( buffer );
signatureGenerator.update ( buffer );
}
this.value = signatureGenerator.generate ().getEncoded ();
logger.info ( "RSA HEADER: {}", this.value );
}
catch ( final Exception e )
{
throw new RuntimeException ( e );
}
}
示例13: addUserId
import org.bouncycastle.openpgp.PGPSignatureGenerator; //导入方法依赖的package包/类
private static PGPPublicKeyRing addUserId(
final PGPPublicKeyRing publicKeyRing, final String userId,
final PGPSecretKeyRing secretKeyRing,
final char[] passphrase,
PGPContentSignerBuilder signerBuilder,
final PGPSignatureSubpacketVector hashedSubpackets,
final PGPSignatureSubpacketVector unhashedSubpackets) throws PGPException {
assertNotNull(publicKeyRing, "publicKeyRing");
assertNotNull(userId, "userId");
final PGPPublicKey masterPublicKey = getMasterKeyOrFail(publicKeyRing);
final PGPSecretKey masterSecretKey = secretKeyRing.getSecretKey(masterPublicKey.getKeyID());
assertNotNull(masterSecretKey, "masterSecretKey");
final PGPPrivateKey privateKey = extractPrivateKey(masterSecretKey, passphrase);
final PGPSignatureGenerator sGen = new PGPSignatureGenerator(signerBuilder);
sGen.init(PGPSignature.POSITIVE_CERTIFICATION, privateKey);
sGen.setHashedSubpackets(hashedSubpackets);
sGen.setUnhashedSubpackets(unhashedSubpackets);
final PGPSignature certification = sGen.generateCertification(userId, masterPublicKey);
final PGPPublicKey newMasterPublicKey = PGPPublicKey.addCertification(masterPublicKey, userId, certification);
PGPPublicKeyRing result = PGPPublicKeyRing.removePublicKey(publicKeyRing, masterPublicKey);
result = PGPPublicKeyRing.insertPublicKey(result, newMasterPublicKey);
return result;
}
示例14: OpenPGPSignature
import org.bouncycastle.openpgp.PGPSignatureGenerator; //导入方法依赖的package包/类
public OpenPGPSignature(OpenPGPSecretKey key) throws PGPException {
PGPDigestCalculatorProvider pgpDigestCalculator = new JcaPGPDigestCalculatorProviderBuilder().setProvider(BouncyCastleProvider.PROVIDER_NAME).build();
PBESecretKeyDecryptor pbeSecretKeyDecryptor = new JcePBESecretKeyDecryptorBuilder(pgpDigestCalculator).setProvider(BouncyCastleProvider.PROVIDER_NAME).build(key.getPassword());
JcaPGPContentSignerBuilder pgpContentSigner = new JcaPGPContentSignerBuilder(key.getSecretKey().getPublicKey().getAlgorithm(), HashAlgorithmTags.SHA1).setProvider(BouncyCastleProvider.PROVIDER_NAME).setDigestProvider(BouncyCastleProvider.PROVIDER_NAME);
signature = new PGPSignatureGenerator(pgpContentSigner);
PGPPrivateKey privateKey = key.getSecretKey().extractPrivateKey(pbeSecretKeyDecryptor);
signature.init(PGPSignature.BINARY_DOCUMENT, privateKey);
}
示例15: sign
import org.bouncycastle.openpgp.PGPSignatureGenerator; //导入方法依赖的package包/类
@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 ) );
signatureGenerator.init ( PGPSignature.BINARY_DOCUMENT, this.privateKey );
final ArmoredOutputStream armoredOutput = new ArmoredOutputStream ( out );
if ( inline )
{
armoredOutput.beginClearText ( digest );
}
final byte[] buffer = new byte[4096];
int rc;
while ( ( rc = in.read ( buffer ) ) >= 0 )
{
if ( inline )
{
armoredOutput.write ( buffer, 0, rc );
}
signatureGenerator.update ( buffer, 0, rc );
}
armoredOutput.endClearText ();
final PGPSignature signature = signatureGenerator.generate ();
signature.encode ( new BCPGOutputStream ( armoredOutput ) );
armoredOutput.close ();
}