本文整理汇总了Java中org.bouncycastle.openpgp.PGPSignatureGenerator类的典型用法代码示例。如果您正苦于以下问题:Java PGPSignatureGenerator类的具体用法?Java PGPSignatureGenerator怎么用?Java PGPSignatureGenerator使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
PGPSignatureGenerator类属于org.bouncycastle.openpgp包,在下文中一共展示了PGPSignatureGenerator类的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: produceSign
import org.bouncycastle.openpgp.PGPSignatureGenerator; //导入依赖的package包/类
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 );
}
示例3: 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();
}
示例4: 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);
}
示例5: feedGeneratorWith
import org.bouncycastle.openpgp.PGPSignatureGenerator; //导入依赖的package包/类
private void feedGeneratorWith(InputStream toSign, PGPSignatureGenerator generator) throws IOException {
byte[] buffer = new byte[1024];
int read = toSign.read(buffer);
while (read > 0) {
generator.update(buffer, 0, read);
read = toSign.read(buffer);
}
}
示例6: 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);
}
}
示例7: 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;
}
示例8: init
import org.bouncycastle.openpgp.PGPSignatureGenerator; //导入依赖的package包/类
protected void init(List<Key> keys) throws IOException, PGPException {
// initialize signature generators
sigs = new ArrayList<PGPSignatureGenerator>(keys.size());
for (Key key : keys)
sigs.add(buildSigner(key, meta));
// write one-pass signature packets
// with multiple signatures, all but last must be flagged "nested"
for (int i = 0; i < sigs.size(); i++) {
boolean nested = i != sigs.size() - 1;
sigs.get(i).generateOnePassVersion(nested).encode(out);
}
}
示例9: processLine
import org.bouncycastle.openpgp.PGPSignatureGenerator; //导入依赖的package包/类
private static void processLine( OutputStream aOut, PGPSignatureGenerator sGen, byte[] line )
throws SignatureException, IOException
{
// note: trailing white space needs to be removed from the end of
// each line for signature calculation RFC 4880 Section 7.1
int length = getLengthWithoutWhiteSpace( line );
if ( length > 0 )
{
sGen.update( line, 0, length );
}
aOut.write( line, 0, line.length );
}
示例10: 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 );
}
}
示例11: 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;
}
示例12: 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);
}
示例13: 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();
}
示例14: 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();
}
示例15: 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;
}