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


Java PGPSignatureGenerator.generate方法代码示例

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


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

示例1: 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 ();
}
 
开发者ID:ctron,项目名称:package-drone,代码行数:34,代码来源:PgpSigningService.java

示例2: 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 ) );

    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 ();
}
 
开发者ID:eclipse,项目名称:packagedrone,代码行数:61,代码来源:AbstractSecretKeySigningService.java

示例3: 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

示例4: generateAndSign

import org.bouncycastle.openpgp.PGPSignatureGenerator; //导入方法依赖的package包/类
private void generateAndSign()
    throws Exception
{
    KeyPairGenerator        keyGen = KeyPairGenerator.getInstance("ECDSA", "BC");

    keyGen.initialize(new ECGenParameterSpec("P-256"));

    KeyPair kpSign = keyGen.generateKeyPair();

    PGPKeyPair ecdsaKeyPair = new JcaPGPKeyPair(PGPPublicKey.ECDSA, kpSign, new Date());

    //
    // try a signature
    //
    PGPSignatureGenerator signGen = new PGPSignatureGenerator(new JcaPGPContentSignerBuilder(PGPPublicKey.ECDSA, HashAlgorithmTags.SHA256).setProvider("BC"));

    signGen.init(PGPSignature.BINARY_DOCUMENT, ecdsaKeyPair.getPrivateKey());

    signGen.update("hello world!".getBytes());

    PGPSignature sig = signGen.generate();

    sig.init(new JcaPGPContentVerifierBuilderProvider().setProvider("BC"), ecdsaKeyPair.getPublicKey());

    sig.update("hello world!".getBytes());

    if (!sig.verify())
    {
        fail("signature failed to verify!");
    }

    //
    // generate a key ring
    //
    char[] passPhrase = "test".toCharArray();
    PGPDigestCalculator sha1Calc = new JcaPGPDigestCalculatorProviderBuilder().build().get(HashAlgorithmTags.SHA1);
    PGPKeyRingGenerator keyRingGen = new PGPKeyRingGenerator(PGPSignature.POSITIVE_CERTIFICATION, ecdsaKeyPair,
             "[email protected]", sha1Calc, null, null, new JcaPGPContentSignerBuilder(ecdsaKeyPair.getPublicKey().getAlgorithm(), HashAlgorithmTags.SHA1), new JcePBESecretKeyEncryptorBuilder(PGPEncryptedData.AES_256, sha1Calc).setProvider("BC").build(passPhrase));

    PGPPublicKeyRing pubRing = keyRingGen.generatePublicKeyRing();

    PGPSecretKeyRing secRing = keyRingGen.generateSecretKeyRing();

    KeyFingerPrintCalculator fingerCalc = new JcaKeyFingerprintCalculator();

    PGPPublicKeyRing pubRingEnc = new PGPPublicKeyRing(pubRing.getEncoded(), fingerCalc);

    if (!Arrays.areEqual(pubRing.getEncoded(), pubRingEnc.getEncoded()))
    {
        fail("public key ring encoding failed");
    }

    PGPSecretKeyRing secRingEnc = new PGPSecretKeyRing(secRing.getEncoded(), fingerCalc);

    if (!Arrays.areEqual(secRing.getEncoded(), secRingEnc.getEncoded()))
    {
        fail("secret key ring encoding failed");
    }
}
 
开发者ID:NoYouShutup,项目名称:CryptMeme,代码行数:60,代码来源:PGPECDSATest.java

示例5: testTextSig

import org.bouncycastle.openpgp.PGPSignatureGenerator; //导入方法依赖的package包/类
private void testTextSig(
    int            encAlgorithm,
    int            hashAlgorithm,
    PGPPublicKey   pubKey,
    PGPPrivateKey  privKey,
    byte[]         data,
    byte[]         canonicalData)
    throws Exception
{            
    PGPSignatureGenerator sGen = new PGPSignatureGenerator(new JcaPGPContentSignerBuilder(encAlgorithm, HashAlgorithmTags.SHA1).setProvider("BC"));
    ByteArrayOutputStream bOut = new ByteArrayOutputStream();
    ByteArrayInputStream  testIn = new ByteArrayInputStream(data);
    Date                  creationTime = new Date();
    
    sGen.init(PGPSignature.CANONICAL_TEXT_DOCUMENT, privKey);
    sGen.generateOnePassVersion(false).encode(bOut);

    PGPLiteralDataGenerator    lGen = new PGPLiteralDataGenerator();
    OutputStream               lOut = lGen.open(
        new UncloseableOutputStream(bOut),
        PGPLiteralData.TEXT,
        "_CONSOLE",
        data.length * 2,
        creationTime);

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

    lOut.write(data);
    sGen.update(data);
    
    lGen.close();

    PGPSignature sig = sGen.generate();

    if (sig.getCreationTime().getTime() == 0)
    {
        fail("creation time not set in v4 signature");
    }

    sig.encode(bOut);

    verifySignature(bOut.toByteArray(), hashAlgorithm, pubKey, canonicalData);
}
 
开发者ID:NoYouShutup,项目名称:CryptMeme,代码行数:49,代码来源:PGPSignatureTest.java


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