當前位置: 首頁>>代碼示例>>Java>>正文


Java PGPUtil.SHA1屬性代碼示例

本文整理匯總了Java中org.bouncycastle.openpgp.PGPUtil.SHA1屬性的典型用法代碼示例。如果您正苦於以下問題:Java PGPUtil.SHA1屬性的具體用法?Java PGPUtil.SHA1怎麽用?Java PGPUtil.SHA1使用的例子?那麽, 這裏精選的屬性代碼示例或許可以為您提供幫助。您也可以進一步了解該屬性所在org.bouncycastle.openpgp.PGPUtil的用法示例。


在下文中一共展示了PGPUtil.SHA1屬性的5個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: createSignatureGenerator

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);
    }
}
 
開發者ID:lxxlxx888,項目名稱:Reer,代碼行數:9,代碼來源:PgpSignatory.java

示例2: signPublicKey

/**
 * 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 );
    }
}
 
開發者ID:subutai-io,項目名稱:base,代碼行數:40,代碼來源:PGPEncryptionUtil.java

示例3: signAndEncrypt

@Override
public String signAndEncrypt(String message) throws IOException {
	
	try {
		/* Final < Armored < Crypted < Clear PGP */
		ByteArrayOutputStream out = new ByteArrayOutputStream();
		ArmoredOutputStream armoredOutput = new ArmoredOutputStream(out);
		PGPEncryptedDataGenerator crypter = new PGPEncryptedDataGenerator(PGPEncryptedDataGenerator.S2K_SHA1, new SecureRandom(), _provider);
		crypter.addMethod(getRemotePublicKey());
		BCPGOutputStream pgpOut = new BCPGOutputStream(crypter.open(armoredOutput, new byte[512]));
		
		/* Prepare for signing */
        PGPSignatureGenerator signer = new PGPSignatureGenerator(getSigningPublicKey().getAlgorithm(), 
                PGPUtil.SHA1, _provider
                );
        signer.initSign(PGPSignature.BINARY_DOCUMENT, getSigningPrivateKey());
        
        /* Output the standard header */
        signer.generateOnePassVersion(false).encode(pgpOut);

        /* Output the literal data */
        PGPLiteralDataGenerator literalDataGenerator = new PGPLiteralDataGenerator(true);
        literalDataGenerator.open(pgpOut, 'b', "bar", message.getBytes().length, new Date()).write(message.getBytes());
        
        /* Calculate signature and output it */
        signer.update(message.getBytes());
        signer.generate().encode(pgpOut);

        pgpOut.close();
        armoredOutput.close();
        out.close();
        
        byte[] result = out.toByteArray();
        
        // brain dead UMAPI adds an extra base64 encoding on top of the ASCII armored string. Go figure.
		return new String(Base64.encode(result));
		
	} catch (PGPException pgpException) {
		throw new IOException("PGP subsystem problem.", pgpException);
		
	} catch (NoSuchAlgorithmException noSuchAlgorithmException) {
		throw new IOException("Missing algorithm. Are you running a compatible JVM/Bouncycastle version?", noSuchAlgorithmException);
		
	} catch (SignatureException signatureException) {
		throw new IOException("PGP subsystem problem.", signatureException);
		
	} catch (NoSuchProviderException noSuchProviderException) {
		throw new IOException("Missing provider. Are you running a compatible JVM/Bouncycastle version?", noSuchProviderException);
		
	}
	
	
}
 
開發者ID:neopeak,項目名稱:brownsocks-payments-enets,代碼行數:53,代碼來源:BCPGPProvider.java

示例4: signFile

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,代碼行數:47,代碼來源:BouncyCastleSigner.java

示例5: newSignedCert

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,代碼行數:41,代碼來源:PushCertificateCheckerTest.java


注:本文中的org.bouncycastle.openpgp.PGPUtil.SHA1屬性示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。