当前位置: 首页>>代码示例>>Java>>正文


Java PGPSignatureGenerator.setHashedSubpackets方法代码示例

本文整理汇总了Java中org.bouncycastle.openpgp.PGPSignatureGenerator.setHashedSubpackets方法的典型用法代码示例。如果您正苦于以下问题:Java PGPSignatureGenerator.setHashedSubpackets方法的具体用法?Java PGPSignatureGenerator.setHashedSubpackets怎么用?Java PGPSignatureGenerator.setHashedSubpackets使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在org.bouncycastle.openpgp.PGPSignatureGenerator的用法示例。


在下文中一共展示了PGPSignatureGenerator.setHashedSubpackets方法的14个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: 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;
}
 
开发者ID:justinludwig,项目名称:jpgpj,代码行数:32,代码来源:Encryptor.java

示例2: 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();
}
 
开发者ID:sonatype-nexus-community,项目名称:nexus-repository-apt,代码行数:36,代码来源:AptSigningFacet.java

示例3: 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;
    }
 
开发者ID:HydAu,项目名称:Camel,代码行数:33,代码来源:PGPKeyAccessDataFormat.java

示例4: 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;
}
 
开发者ID:subshare,项目名称:subshare,代码行数:30,代码来源:BcWithLocalGnuPgPgp.java

示例5: generateTest

import org.bouncycastle.openpgp.PGPSignatureGenerator; //导入方法依赖的package包/类
/**
 * Generated signature test
 * 
 * @param sKey
 * @param pgpPrivKey
 */
public void generateTest(
    PGPSecretKeyRing sKey,
    PGPPublicKey     pgpPubKey,
    PGPPrivateKey    pgpPrivKey)
    throws Exception
{
    String                  data = "hello world!";
    ByteArrayOutputStream   bOut = new ByteArrayOutputStream();
    ByteArrayInputStream    testIn = new ByteArrayInputStream(data.getBytes());
    PGPSignatureGenerator   sGen = new PGPSignatureGenerator(new BcPGPContentSignerBuilder(PublicKeyAlgorithmTags.DSA, HashAlgorithmTags.SHA1));

    sGen.init(PGPSignature.BINARY_DOCUMENT, pgpPrivKey);

    PGPSignatureSubpacketGenerator spGen = new PGPSignatureSubpacketGenerator();
    
    Iterator        it = sKey.getSecretKey().getPublicKey().getUserIDs();
    String          primaryUserID = (String)it.next();
    
    spGen.setSignerUserID(true, primaryUserID);
    
    sGen.setHashedSubpackets(spGen.generate());

    sGen.generateOnePassVersion(false).encode(bOut);

    PGPLiteralDataGenerator lGen = new PGPLiteralDataGenerator();
    
    Date testDate = new Date((System.currentTimeMillis() / 1000) * 1000);
    OutputStream lOut = lGen.open(
        new UncloseableOutputStream(bOut),
        PGPLiteralData.BINARY,
        "_CONSOLE",
        data.getBytes().length,
        testDate);

    int ch;
    while ((ch = testIn.read()) >= 0)
    {
        lOut.write(ch);
        sGen.update((byte)ch);
    }

    lGen.close();

    sGen.generate().encode(bOut);

    PGPObjectFactory        pgpFact = new BcPGPObjectFactory(bOut.toByteArray());

    PGPOnePassSignatureList p1 = (PGPOnePassSignatureList)pgpFact.nextObject();
    PGPOnePassSignature     ops = p1.get(0);
    
    PGPLiteralData          p2 = (PGPLiteralData)pgpFact.nextObject();
    if (!p2.getModificationTime().equals(testDate))
    {
        fail("Modification time not preserved");
    }

    InputStream             dIn = p2.getInputStream();

    ops.init(new BcPGPContentVerifierBuilderProvider(), pgpPubKey);
    
    while ((ch = dIn.read()) >= 0)
    {
        ops.update((byte)ch);
    }

    PGPSignatureList p3 = (PGPSignatureList)pgpFact.nextObject();

    if (!ops.verify(p3.get(0)))
    {
        fail("Failed generated signature check");
    }
}
 
开发者ID:ttt43ttt,项目名称:gwt-crypto,代码行数:79,代码来源:BcPGPDSATest.java

示例6: signAndEncrypt

import org.bouncycastle.openpgp.PGPSignatureGenerator; //导入方法依赖的package包/类
public static byte[] signAndEncrypt( final byte[] message, final PGPSecretKey secretKey, final String secretPwd,
                                     final PGPPublicKey publicKey, final boolean armored ) throws PGPException
{
    try
    {
        final ByteArrayOutputStream out = new ByteArrayOutputStream();
        final PGPEncryptedDataGenerator encryptedDataGenerator = new PGPEncryptedDataGenerator(
                new JcePGPDataEncryptorBuilder( SymmetricKeyAlgorithmTags.AES_256 ).setWithIntegrityPacket( true )
                                                                                   .setSecureRandom(
                                                                                           new SecureRandom() )
                                                                                   .setProvider( provider ) );

        encryptedDataGenerator.addMethod(
                new JcePublicKeyKeyEncryptionMethodGenerator( publicKey ).setSecureRandom( new SecureRandom() )
                                                                         .setProvider( provider ) );

        final OutputStream theOut = armored ? new ArmoredOutputStream( out ) : out;
        final OutputStream encryptedOut = encryptedDataGenerator.open( theOut, new byte[4096] );

        final PGPCompressedDataGenerator compressedDataGenerator =
                new PGPCompressedDataGenerator( CompressionAlgorithmTags.ZIP );
        final OutputStream compressedOut = compressedDataGenerator.open( encryptedOut, new byte[4096] );
        final PGPPrivateKey privateKey = secretKey.extractPrivateKey(
                new JcePBESecretKeyDecryptorBuilder().setProvider( provider ).build( secretPwd.toCharArray() ) );
        final PGPSignatureGenerator signatureGenerator = new PGPSignatureGenerator(
                new JcaPGPContentSignerBuilder( secretKey.getPublicKey().getAlgorithm(), HashAlgorithmTags.SHA1 )
                        .setProvider( provider ) );
        signatureGenerator.init( PGPSignature.BINARY_DOCUMENT, privateKey );
        final Iterator<?> it = secretKey.getPublicKey().getUserIDs();
        if ( it.hasNext() )
        {
            final PGPSignatureSubpacketGenerator spGen = new PGPSignatureSubpacketGenerator();
            spGen.setSignerUserID( false, ( String ) it.next() );
            signatureGenerator.setHashedSubpackets( spGen.generate() );
        }
        signatureGenerator.generateOnePassVersion( false ).encode( compressedOut );
        final PGPLiteralDataGenerator literalDataGenerator = new PGPLiteralDataGenerator();
        final OutputStream literalOut = literalDataGenerator
                .open( compressedOut, PGPLiteralData.BINARY, "filename", new Date(), new byte[4096] );
        final InputStream in = new ByteArrayInputStream( message );
        final byte[] buf = new byte[4096];
        for ( int len; ( len = in.read( buf ) ) > 0; )
        {
            literalOut.write( buf, 0, len );
            signatureGenerator.update( buf, 0, len );
        }
        in.close();
        literalDataGenerator.close();
        signatureGenerator.generate().encode( compressedOut );
        compressedDataGenerator.close();
        encryptedDataGenerator.close();
        theOut.close();
        return out.toByteArray();
    }
    catch ( Exception e )
    {
        throw new PGPException( "Error in signAndEncrypt", e );
    }
}
 
开发者ID:subutai-io,项目名称:base,代码行数:60,代码来源:PGPEncryptionUtil.java

示例7: sign

import org.bouncycastle.openpgp.PGPSignatureGenerator; //导入方法依赖的package包/类
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 );
    }
}
 
开发者ID:subutai-io,项目名称:base,代码行数:59,代码来源:PGPEncryptionUtil.java

示例8: signFile

import org.bouncycastle.openpgp.PGPSignatureGenerator; //导入方法依赖的package包/类
public static byte[] signFile(byte[] content, String fileName, InputStream keyIn,
		char[] pass, Provider securityProvider, TimeProvider timeProvider) throws IOException, NoSuchAlgorithmException,
		NoSuchProviderException, PGPException, SignatureException {

	ByteArrayOutputStream byteArrayOut = new ByteArrayOutputStream();
	ArmoredOutputStream armoredOut = new ArmoredOutputStream(byteArrayOut);
	PGPLiteralDataGenerator literalDataGenerator = new PGPLiteralDataGenerator();
	PGPCompressedDataGenerator compressedDataGenerator = new PGPCompressedDataGenerator(PGPCompressedData.ZLIB);

	try {
		PGPSecretKey pgpSecretKey = BouncyCastlePGPExampleUtil.readSecretKey(keyIn);
		PGPPrivateKey pgpPrivateKey = pgpSecretKey.extractPrivateKey(pass, securityProvider);
		PGPSignatureGenerator signatureGenerator = new PGPSignatureGenerator(pgpSecretKey
				.getPublicKey().getAlgorithm(), PGPUtil.SHA1, securityProvider);

		signatureGenerator.initSign(PGPSignature.BINARY_DOCUMENT, pgpPrivateKey);

		Iterator<?> it = pgpSecretKey.getPublicKey().getUserIDs();
		if (it.hasNext()) { //get first user ID
			PGPSignatureSubpacketGenerator spGen = new PGPSignatureSubpacketGenerator();

			spGen.setSignerUserID(false, (String) it.next());
			signatureGenerator.setHashedSubpackets(spGen.generate());
		} else {
			throw new PGPException("No user ID found in the public key of the certificate.");
		}
		

		BCPGOutputStream bOut = new BCPGOutputStream(compressedDataGenerator.open(armoredOut));

		signatureGenerator.generateOnePassVersion(false).encode(bOut);

		OutputStream literalOut = literalDataGenerator.open(bOut, PGPLiteralData.BINARY, fileName, timeProvider.nowDate(), content);

		literalOut.write(content);
		signatureGenerator.update(content);

		signatureGenerator.generate().encode(bOut);

	} finally {
		literalDataGenerator.close();
		compressedDataGenerator.close();
		armoredOut.close();
	}
	
	return byteArrayOut.toByteArray();
}
 
开发者ID:petrsmid,项目名称:unicredit-connector,代码行数:48,代码来源:BouncyCastleSigner.java

示例9: newSignedCert

import org.bouncycastle.openpgp.PGPSignatureGenerator; //导入方法依赖的package包/类
private PushCertificate newSignedCert(String nonce, TestKey signingKey, Date now)
    throws Exception {
  PushCertificateIdent ident =
      new PushCertificateIdent(signingKey.getFirstUserId(), System.currentTimeMillis(), -7 * 60);
  String payload =
      "certificate version 0.1\n"
          + "pusher "
          + ident.getRaw()
          + "\n"
          + "pushee test://localhost/repo.git\n"
          + "nonce "
          + nonce
          + "\n"
          + "\n"
          + "0000000000000000000000000000000000000000"
          + " deadbeefdeadbeefdeadbeefdeadbeefdeadbeef"
          + " refs/heads/master\n";
  PGPSignatureGenerator gen =
      new PGPSignatureGenerator(
          new BcPGPContentSignerBuilder(signingKey.getPublicKey().getAlgorithm(), PGPUtil.SHA1));

  if (now != null) {
    PGPSignatureSubpacketGenerator subGen = new PGPSignatureSubpacketGenerator();
    subGen.setSignatureCreationTime(false, now);
    gen.setHashedSubpackets(subGen.generate());
  }

  gen.init(PGPSignature.BINARY_DOCUMENT, signingKey.getPrivateKey());
  gen.update(payload.getBytes(UTF_8));
  PGPSignature sig = gen.generate();

  ByteArrayOutputStream bout = new ByteArrayOutputStream();
  try (BCPGOutputStream out = new BCPGOutputStream(new ArmoredOutputStream(bout))) {
    sig.encode(out);
  }

  String cert = payload + new String(bout.toByteArray(), UTF_8);
  Reader reader = new InputStreamReader(new ByteArrayInputStream(cert.getBytes(UTF_8)));
  PushCertificateParser parser = new PushCertificateParser(repo, signedPushConfig);
  return parser.parse(reader);
}
 
开发者ID:gerrit-review,项目名称:gerrit,代码行数:42,代码来源:PushCertificateCheckerTest.java

示例10: run

import org.bouncycastle.openpgp.PGPSignatureGenerator; //导入方法依赖的package包/类
public void run(final char[] passphrase, final InputStream inputStream, final OutputStream outputStream) throws IOException, CryptoException {
  try {
    final OutputStream armor = new ArmoredOutputStream(outputStream);

    final PGPEncryptedDataGenerator encryptedDataGenerator = new PGPEncryptedDataGenerator(new JcePGPDataEncryptorBuilder(
        SymmetricKeyAlgorithmTags.AES_128).setWithIntegrityPacket(true).setSecureRandom(new SecureRandom()).setProvider(new BouncyCastleProvider()));
    encryptedDataGenerator.addMethod(new JcePublicKeyKeyEncryptionMethodGenerator(publicKey).setSecureRandom(new SecureRandom()).setProvider(
        new BouncyCastleProvider()));

    final OutputStream encryptedOut = encryptedDataGenerator.open(armor, new byte[4096]);

    final PGPCompressedDataGenerator compressedDataGenerator = new PGPCompressedDataGenerator(CompressionAlgorithmTags.ZIP);
    final OutputStream compressedOut = compressedDataGenerator.open(encryptedOut, new byte[4096]);

    final PGPPrivateKey privateKey = secretKey.extractPrivateKey(new JcePBESecretKeyDecryptorBuilder().setProvider(new BouncyCastleProvider())
        .build(passphrase));

    final PGPSignatureGenerator signatureGenerator = new PGPSignatureGenerator(new JcaPGPContentSignerBuilder(secretKey.getPublicKey()
        .getAlgorithm(), HashAlgorithmTags.SHA1).setProvider(new BouncyCastleProvider()));
    signatureGenerator.init(PGPSignature.BINARY_DOCUMENT, privateKey);
    final Iterator<?> it = secretKey.getPublicKey().getUserIDs();
    if (it.hasNext()) {
      final PGPSignatureSubpacketGenerator spGen = new PGPSignatureSubpacketGenerator();
      spGen.setSignerUserID(false, (String) it.next());
      signatureGenerator.setHashedSubpackets(spGen.generate());
    }
    signatureGenerator.generateOnePassVersion(false).encode(compressedOut);

    final PGPLiteralDataGenerator literalDataGenerator = new PGPLiteralDataGenerator();
    final OutputStream literalOut = literalDataGenerator.open(compressedOut, PGPLiteralData.BINARY, "", new Date(), new byte[4096]);
    final byte[] buf = new byte[4096];
    for (int len = 0; (len = inputStream.read(buf)) > 0;) {
      literalOut.write(buf, 0, len);
      signatureGenerator.update(buf, 0, len);
    }
    literalDataGenerator.close();
    signatureGenerator.generate().encode(compressedOut);
    compressedDataGenerator.close();
    encryptedDataGenerator.close();
    armor.close();
  }
  catch (final Exception e) {
    throw new CryptoException(e);
  }

}
 
开发者ID:hsch,项目名称:bcpg-simple,代码行数:47,代码来源:BcPGPSignEncryptTransform.java

示例11: generateTest

import org.bouncycastle.openpgp.PGPSignatureGenerator; //导入方法依赖的package包/类
private void generateTest(
    String message,
    String type)
    throws Exception
{
    PGPSecretKey                    pgpSecKey = readSecretKey(new ByteArrayInputStream(secretKey));
    PGPPrivateKey                   pgpPrivKey = pgpSecKey.extractPrivateKey("".toCharArray(), "BC");
    PGPSignatureGenerator           sGen = new PGPSignatureGenerator(pgpSecKey.getPublicKey().getAlgorithm(), PGPUtil.SHA256, "BC");
    PGPSignatureSubpacketGenerator  spGen = new PGPSignatureSubpacketGenerator();

    sGen.initSign(PGPSignature.CANONICAL_TEXT_DOCUMENT, pgpPrivKey);

    Iterator    it = pgpSecKey.getPublicKey().getUserIDs();
    if (it.hasNext())
    {
        spGen.setSignerUserID(false, (String)it.next());
        sGen.setHashedSubpackets(spGen.generate());
    }
    
    ByteArrayOutputStream  bOut = new ByteArrayOutputStream();
    ArmoredOutputStream    aOut = new ArmoredOutputStream(bOut);
    ByteArrayInputStream   bIn = new ByteArrayInputStream(message.getBytes());

    aOut.beginClearText(PGPUtil.SHA256);

    //
    // note the last \n in the file is ignored
    //
    ByteArrayOutputStream lineOut = new ByteArrayOutputStream();
    int lookAhead = readInputLine(lineOut, bIn);

    processLine(aOut, sGen, lineOut.toByteArray());

    if (lookAhead != -1)
    {
        do
        {
            lookAhead = readInputLine(lineOut, lookAhead, bIn);

            sGen.update((byte)'\r');
            sGen.update((byte)'\n');

            processLine(aOut, sGen, lineOut.toByteArray());
        }
        while (lookAhead != -1);
    }

    aOut.endClearText();

    BCPGOutputStream            bcpgOut = new BCPGOutputStream(aOut);

    sGen.generate().encode(bcpgOut);

    aOut.close();
    
    messageTest(new String(bOut.toByteArray()), type);
}
 
开发者ID:NoYouShutup,项目名称:CryptMeme,代码行数:58,代码来源:PGPClearSignedSignatureTest.java

示例12: generateTest

import org.bouncycastle.openpgp.PGPSignatureGenerator; //导入方法依赖的package包/类
/**
 * Generated signature test
 * 
 * @param sKey
 * @param pgpPrivKey
 */
public void generateTest(
    PGPSecretKeyRing sKey,
    PGPPublicKey     pgpPubKey,
    PGPPrivateKey    pgpPrivKey)
    throws Exception
{
    String                  data = "hello world!";
    ByteArrayOutputStream   bOut = new ByteArrayOutputStream();
    ByteArrayInputStream    testIn = new ByteArrayInputStream(data.getBytes());
    PGPSignatureGenerator   sGen = new PGPSignatureGenerator(new BcPGPContentSignerBuilder(PublicKeyAlgorithmTags.DSA, HashAlgorithmTags.SHA1));

    sGen.init(PGPSignature.BINARY_DOCUMENT, pgpPrivKey);

    PGPSignatureSubpacketGenerator spGen = new PGPSignatureSubpacketGenerator();
    
    Iterator        it = sKey.getSecretKey().getPublicKey().getUserIDs();
    String          primaryUserID = (String)it.next();
    
    spGen.setSignerUserID(true, primaryUserID);
    
    sGen.setHashedSubpackets(spGen.generate());

    sGen.generateOnePassVersion(false).encode(bOut);

    PGPLiteralDataGenerator lGen = new PGPLiteralDataGenerator();
    
    Date testDate = new Date((System.currentTimeMillis() / 1000) * 1000);
    OutputStream lOut = lGen.open(
        new UncloseableOutputStream(bOut),
        PGPLiteralData.BINARY,
        "_CONSOLE",
        data.getBytes().length,
        testDate);

    int ch;
    while ((ch = testIn.read()) >= 0)
    {
        lOut.write(ch);
        sGen.update((byte)ch);
    }

    lGen.close();

    sGen.generate().encode(bOut);

    PGPObjectFactory        pgpFact = new PGPObjectFactory(bOut.toByteArray());

    PGPOnePassSignatureList p1 = (PGPOnePassSignatureList)pgpFact.nextObject();
    PGPOnePassSignature     ops = p1.get(0);
    
    PGPLiteralData          p2 = (PGPLiteralData)pgpFact.nextObject();
    if (!p2.getModificationTime().equals(testDate))
    {
        fail("Modification time not preserved");
    }

    InputStream             dIn = p2.getInputStream();

    ops.init(new BcPGPContentVerifierBuilderProvider(), pgpPubKey);
    
    while ((ch = dIn.read()) >= 0)
    {
        ops.update((byte)ch);
    }

    PGPSignatureList p3 = (PGPSignatureList)pgpFact.nextObject();

    if (!ops.verify(p3.get(0)))
    {
        fail("Failed generated signature check");
    }
}
 
开发者ID:credentials,项目名称:irma_future_id,代码行数:79,代码来源:BcPGPDSATest.java

示例13: addUserInfoToSignature

import org.bouncycastle.openpgp.PGPSignatureGenerator; //导入方法依赖的package包/类
/**
 * Add user ID to signature file.
 *
 * <p>This adds information about the identity of the signer to the signature file. It's not
 * required, but I'm guessing it could be a lifesaver if somewhere down the road, people lose
 * track of the public keys and need to figure out how to verify a couple blobs. This would at
 * least tell them which key to download from the MIT keyserver.
 *
 * <p>But the main reason why I'm using this is because I copied it from the code of another
 * googler who was also uncertain about the precise reason why it's needed.
 */
private static void addUserInfoToSignature(PGPPublicKey publicKey, PGPSignatureGenerator signer) {
  @SuppressWarnings("unchecked") // safe by specification.
  Iterator<String> uidIter = publicKey.getUserIDs();
  if (uidIter.hasNext()) {
    PGPSignatureSubpacketGenerator spg = new PGPSignatureSubpacketGenerator();
    spg.setSignerUserID(false, uidIter.next());
    signer.setHashedSubpackets(spg.generate());
  }
}
 
开发者ID:google,项目名称:nomulus,代码行数:21,代码来源:RydePgpSigningOutputStream.java

示例14: addUserInfoToSignature

import org.bouncycastle.openpgp.PGPSignatureGenerator; //导入方法依赖的package包/类
/**
 * Add user ID to signature file.
 *
 * <p>This adds information about the identity of the signer to the signature file. It's not
 * required, but I'm guessing it could be a lifesaver if somewhere down the road, people lose
 * track of the public keys and need to figure out how to verify a couple blobs. This would at
 * least tell them which key to download from the MIT keyserver.
 *
 * <p>But the main reason why I'm using this is because I copied it from the code of another
 * Googler who was also uncertain about the precise reason why it's needed.
 */
private void addUserInfoToSignature(PGPPublicKey publicKey, PGPSignatureGenerator signer) {
  @SuppressWarnings("unchecked") // Safe by specification.
  Iterator<String> uidIter = publicKey.getUserIDs();
  if (uidIter.hasNext()) {
    PGPSignatureSubpacketGenerator spg = new PGPSignatureSubpacketGenerator();
    spg.setSignerUserID(false, uidIter.next());
    signer.setHashedSubpackets(spg.generate());
  }
}
 
开发者ID:google,项目名称:nomulus,代码行数:21,代码来源:BouncyCastleTest.java


注:本文中的org.bouncycastle.openpgp.PGPSignatureGenerator.setHashedSubpackets方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。