本文整理汇总了Java中org.bouncycastle.openpgp.PGPSignatureGenerator.initSign方法的典型用法代码示例。如果您正苦于以下问题:Java PGPSignatureGenerator.initSign方法的具体用法?Java PGPSignatureGenerator.initSign怎么用?Java PGPSignatureGenerator.initSign使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.bouncycastle.openpgp.PGPSignatureGenerator
的用法示例。
在下文中一共展示了PGPSignatureGenerator.initSign方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: signAndEncrypt
import org.bouncycastle.openpgp.PGPSignatureGenerator; //导入方法依赖的package包/类
@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);
}
}
示例2: 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();
}
示例3: embeddedJpegTest
import org.bouncycastle.openpgp.PGPSignatureGenerator; //导入方法依赖的package包/类
private void embeddedJpegTest()
throws Exception
{
PGPPublicKeyRing pgpPub = new PGPPublicKeyRing(testPubKey, new JcaKeyFingerprintCalculator());
PGPSecretKeyRing pgpSec = new PGPSecretKeyRing(testPrivKey, new JcaKeyFingerprintCalculator());
PGPPublicKey pubKey = pgpPub.getPublicKey();
PGPUserAttributeSubpacketVectorGenerator vGen = new PGPUserAttributeSubpacketVectorGenerator();
vGen.setImageAttribute(ImageAttribute.JPEG, jpegImage);
PGPUserAttributeSubpacketVector uVec = vGen.generate();
PGPSignatureGenerator sGen = new PGPSignatureGenerator(PublicKeyAlgorithmTags.RSA_GENERAL, HashAlgorithmTags.SHA1, "BC");
sGen.initSign(PGPSignature.POSITIVE_CERTIFICATION, pgpSec.getSecretKey().extractPrivateKey(pass, "BC"));
PGPSignature sig = sGen.generateCertification(uVec, pubKey);
PGPPublicKey nKey = PGPPublicKey.addCertification(pubKey, uVec, sig);
Iterator it = nKey.getUserAttributes();
int count = 0;
while (it.hasNext())
{
PGPUserAttributeSubpacketVector attributes = (PGPUserAttributeSubpacketVector)it.next();
Iterator sigs = nKey.getSignaturesForUserAttribute(attributes);
int sigCount = 0;
while (sigs.hasNext())
{
PGPSignature s = (PGPSignature)sigs.next();
s.initVerify(pubKey, "BC");
if (!s.verifyCertification(attributes, pubKey))
{
fail("added signature failed verification");
}
sigCount++;
}
if (sigCount != 1)
{
fail("Failed added user attributes signature check");
}
count++;
}
if (count != 1)
{
fail("didn't find added user attributes");
}
nKey = PGPPublicKey.removeCertification(nKey, uVec);
count = 0;
for (it = nKey.getUserAttributes(); it.hasNext();)
{
count++;
}
if (count != 0)
{
fail("found attributes where none expected");
}
}
示例4: doSigGenerateTest
import org.bouncycastle.openpgp.PGPSignatureGenerator; //导入方法依赖的package包/类
private void doSigGenerateTest(String privateKeyFile, String publicKeyFile, int digest)
throws Exception
{
PGPSecretKeyRing secRing = loadSecretKey(privateKeyFile);
PGPPublicKeyRing pubRing = loadPublicKey(publicKeyFile);
String data = "hello world!";
ByteArrayOutputStream bOut = new ByteArrayOutputStream();
ByteArrayInputStream testIn = new ByteArrayInputStream(data.getBytes());
PGPSignatureGenerator sGen = new PGPSignatureGenerator(PublicKeyAlgorithmTags.DSA, digest, "BC");
sGen.initSign(PGPSignature.BINARY_DOCUMENT, secRing.getSecretKey().extractPrivateKey("test".toCharArray(), "BC"));
BCPGOutputStream bcOut = new BCPGOutputStream(bOut);
sGen.generateOnePassVersion(false).encode(bcOut);
PGPLiteralDataGenerator lGen = new PGPLiteralDataGenerator();
Date testDate = new Date((System.currentTimeMillis() / 1000) * 1000);
OutputStream lOut = lGen.open(
new UncloseableOutputStream(bcOut),
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(bcOut);
PGPObjectFactory pgpFact = new PGPObjectFactory(bOut.toByteArray());
PGPOnePassSignatureList p1 = (PGPOnePassSignatureList)pgpFact.nextObject();
PGPOnePassSignature ops = p1.get(0);
assertEquals(digest, ops.getHashAlgorithm());
assertEquals(PublicKeyAlgorithmTags.DSA, ops.getKeyAlgorithm());
PGPLiteralData p2 = (PGPLiteralData)pgpFact.nextObject();
if (!p2.getModificationTime().equals(testDate))
{
fail("Modification time not preserved");
}
InputStream dIn = p2.getInputStream();
ops.initVerify(pubRing.getPublicKey(), "BC");
while ((ch = dIn.read()) >= 0)
{
ops.update((byte)ch);
}
PGPSignatureList p3 = (PGPSignatureList)pgpFact.nextObject();
PGPSignature sig = p3.get(0);
assertEquals(digest, sig.getHashAlgorithm());
assertEquals(PublicKeyAlgorithmTags.DSA, sig.getKeyAlgorithm());
assertTrue(ops.verify(sig));
}
示例5: 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);
}