本文整理汇总了Java中org.bouncycastle.openpgp.PGPSignatureList类的典型用法代码示例。如果您正苦于以下问题:Java PGPSignatureList类的具体用法?Java PGPSignatureList怎么用?Java PGPSignatureList使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
PGPSignatureList类属于org.bouncycastle.openpgp包,在下文中一共展示了PGPSignatureList类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: verify
import org.bouncycastle.openpgp.PGPSignatureList; //导入依赖的package包/类
/**
* Verify a signature. Only return true if signature matches calculated signature of signedData
* and if it was signed by the publicKey
*
* @param signedData
* @param signature
* @return
*/
public boolean verify(InputStream signedData, InputStream signature) {
try {
signature = PGPUtil.getDecoderStream(signature);
JcaPGPObjectFactory pgpFact = new JcaPGPObjectFactory(signature);
PGPSignature sig = ((PGPSignatureList) pgpFact.nextObject()).get(0);
PGPPublicKey key = pgpPubRingCollection.getPublicKey(sig.getKeyID());
sig.init(new JcaPGPContentVerifierBuilderProvider().setProvider("BC"), key);
byte[] buff = new byte[1024];
int read = 0;
while ((read = signedData.read(buff)) != -1) {
sig.update(buff, 0, read);
}
signedData.close();
return sig.verify();
}
catch (Exception ex) {
// can we put a logger here please?
return false;
}
}
示例2: verifySignature
import org.bouncycastle.openpgp.PGPSignatureList; //导入依赖的package包/类
/**
* Verify a PGP signature.
*
* @param file the file
* @param signature the signature
* @param key the public key
* @return true if the signature is verified
* @throws Exception anything preventing the verification to happen
*/
public static boolean verifySignature(
InputStream file,
InputStream signature,
PGPPublicKey key)
throws Exception {
InputStream sigInputStream = PGPUtil.getDecoderStream(signature);
PGPObjectFactory pgpObjectFactory = new PGPObjectFactory(sigInputStream, new BcKeyFingerprintCalculator());
PGPSignatureList sigList = (PGPSignatureList) pgpObjectFactory.nextObject();
PGPSignature pgpSignature = sigList.get(0);
pgpSignature.init(new BcPGPContentVerifierBuilderProvider(), key);
try (InputStream inArtifact = new BufferedInputStream(file)) {
int t;
while ((t = inArtifact.read()) >= 0) {
pgpSignature.update((byte) t);
}
}
return pgpSignature.verify();
}
示例3: readSignatureFile
import org.bouncycastle.openpgp.PGPSignatureList; //导入依赖的package包/类
private PGPSignatureList readSignatureFile(final File signatureFile) throws PGPVerifyException {
AssertUtil.assertNotNull(signatureFile, "signatureFile");
if (!signatureFile.isFile() || !signatureFile.canRead())
throw new PGPVerifyException("The signature-file does not exist or is not readable: " + signatureFile.getAbsolutePath());
try {
final InputStream in = new BufferedInputStream(castStream(signatureFile.createInputStream()));
try {
final PGPObjectFactory objectFactory = new PGPObjectFactory(
PGPUtil.getDecoderStream(in), new BcKeyFingerprintCalculator());
final PGPSignatureList sl = (PGPSignatureList) objectFactory.nextObject();
return sl;
} finally {
in.close();
}
} catch (final Exception e) {
throw new PGPVerifyException(signatureFile.getAbsolutePath() + ": " + e, e);
}
}
示例4: verifyGoodSignature
import org.bouncycastle.openpgp.PGPSignatureList; //导入依赖的package包/类
@Test
public void verifyGoodSignature() throws Exception {
final PGPPublicKeyRingCollection publicKeyRing = getPublicKeyRingWithTrustedKeys();
final PGPSignatureList sl = readSignatureFile("/content1.sig");
assertThat(sl.isEmpty()).isFalse();
assertThat(sl.size()).isEqualTo(1);
PGPSignature signature = sl.get(0);
signature.init(new BcPGPContentVerifierBuilderProvider(), publicKeyRing.getPublicKey(signature.getKeyID()));
InputStream contentIn = PGPTest.class.getResourceAsStream("/content1");
byte[] buf = new byte[4096];
int len;
while (0 <= (len = contentIn.read(buf))) {
signature.update(buf, 0, len);
}
contentIn.close();
assertThat(signature.verify()).isTrue();
}
示例5: verifyBadSignature
import org.bouncycastle.openpgp.PGPSignatureList; //导入依赖的package包/类
@Test
public void verifyBadSignature() throws Exception {
final PGPPublicKeyRingCollection publicKeyRing = getPublicKeyRingWithTrustedKeys();
final PGPSignatureList sl = readSignatureFile("/content1.sig");
assertThat(sl.isEmpty()).isFalse();
assertThat(sl.size()).isEqualTo(1);
PGPSignature signature = sl.get(0);
signature.init(new BcPGPContentVerifierBuilderProvider(), publicKeyRing.getPublicKey(signature.getKeyID()));
InputStream contentIn = PGPTest.class.getResourceAsStream("/content1");
byte[] buf = new byte[4096];
int len;
while (0 <= (len = contentIn.read(buf))) {
buf[0] = 0;
signature.update(buf, 0, len);
}
contentIn.close();
assertThat(signature.verify()).isFalse();
}
示例6: ContentAndSignatures
import org.bouncycastle.openpgp.PGPSignatureList; //导入依赖的package包/类
public ContentAndSignatures( final byte[] decryptedContent, final PGPOnePassSignatureList onePassSignatureList,
final PGPSignatureList signatureList )
{
this.decryptedContent = decryptedContent;
this.onePassSignatureList = onePassSignatureList;
this.signatureList = signatureList;
}
示例7: doVerify
import org.bouncycastle.openpgp.PGPSignatureList; //导入依赖的package包/类
private static void doVerify( JcaPGPObjectFactory objectFactory, PGPOnePassSignature onePassSignature )
throws IOException, PGPException
{
PGPSignatureList signatures = ( PGPSignatureList ) objectFactory.nextObject();
if ( !onePassSignature.verify( signatures.get( 0 ) ) )
{
throw new PGPDataValidationException( "Signature verification failed" );
}
}
示例8: pgpExtractSignature
import org.bouncycastle.openpgp.PGPSignatureList; //导入依赖的package包/类
/**
* Extracts a {@link PGPSignature} object from a blob of {@code .sig} data.
*
* @throws SignatureException if a signature object couldn't be extracted for any reason.
*/
private static PGPSignature pgpExtractSignature(@Tainted byte[] signature)
throws SignatureException {
try {
ByteArrayInputStream input = new ByteArrayInputStream(signature);
PGPObjectFactory decoder = new BcPGPObjectFactory(PGPUtil.getDecoderStream(input));
Object object = decoder.nextObject();
if (object == null) {
throw new SignatureException(String.format(
"No OpenPGP packets found in signature.\n%s",
dumpHex(signature)));
}
if (!(object instanceof PGPSignatureList)) {
throw new SignatureException(String.format(
"Expected PGPSignatureList packet but got %s\n%s",
object.getClass().getSimpleName(),
dumpHex(signature)));
}
PGPSignatureList sigs = (PGPSignatureList) object;
if (sigs.isEmpty()) {
throw new SignatureException(String.format(
"PGPSignatureList doesn't have a PGPSignature.\n%s",
dumpHex(signature)));
}
return sigs.get(0);
} catch (IOException e) {
throw new SignatureException(String.format(
"Failed to extract PGPSignature object from .sig blob.\n%s",
dumpHex(signature)), e);
}
}
示例9: testSignVerify_Detached
import org.bouncycastle.openpgp.PGPSignatureList; //导入依赖的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();
}
示例10: verifySignature
import org.bouncycastle.openpgp.PGPSignatureList; //导入依赖的package包/类
private void verifySignature(PGPObjectFactory pgpFactory, PGPOnePassSignature signature) throws IOException, PGPException, SignatureException {
if (signature != null) {
PGPSignatureList sigList = (PGPSignatureList) pgpFactory.nextObject();
if (!signature.verify(getSignatureWithKeyId(signature.getKeyID(), sigList))) {
throw new SignatureException("Verification of the PGP signature with the key ID " + signature.getKeyID() + " failed. The PGP message may have been tampered.");
}
}
}
示例11: getSignatureWithKeyId
import org.bouncycastle.openpgp.PGPSignatureList; //导入依赖的package包/类
protected PGPSignature getSignatureWithKeyId(long keyID, PGPSignatureList sigList) {
for (int i = 0; i < sigList.size(); i++) {
PGPSignature signature = sigList.get(i);
if (keyID == signature.getKeyID()) {
return signature;
}
}
throw new IllegalStateException("PGP signature is inconsistent");
}
示例12: readSignature
import org.bouncycastle.openpgp.PGPSignatureList; //导入依赖的package包/类
private PGPSignature readSignature(PushCertificate cert) throws IOException {
ArmoredInputStream in =
new ArmoredInputStream(new ByteArrayInputStream(Constants.encode(cert.getSignature())));
PGPObjectFactory factory = new BcPGPObjectFactory(in);
Object obj;
while ((obj = factory.nextObject()) != null) {
if (obj instanceof PGPSignatureList) {
PGPSignatureList sigs = (PGPSignatureList) obj;
if (!sigs.isEmpty()) {
return sigs.get(0);
}
}
}
return null;
}
示例13: revocationTest
import org.bouncycastle.openpgp.PGPSignatureList; //导入依赖的package包/类
public void revocationTest()
throws Exception
{
PGPPublicKeyRing pgpPub = new PGPPublicKeyRing(pub7, new BcKeyFingerprintCalculator());
Iterator it = pgpPub.getPublicKeys();
PGPPublicKey masterKey = null;
while (it.hasNext())
{
PGPPublicKey k = (PGPPublicKey)it.next();
if (k.isMasterKey())
{
masterKey = k;
continue;
}
}
PGPSignature sig =((PGPSignatureList)new PGPObjectFactory(pub7revoke).nextObject()).get(0);
sig.init(new BcPGPContentVerifierBuilderProvider(), masterKey);
if (!sig.verifyCertification(masterKey))
{
fail("failed to verify revocation certification");
}
}
示例14: verifySignature
import org.bouncycastle.openpgp.PGPSignatureList; //导入依赖的package包/类
private static DecryptionResult verifySignature(DecryptionResult result,
PGPObjectFactory pgpFact, PGPOnePassSignature ops) throws PGPException, IOException {
Object object = pgpFact.nextObject(); // nullable
if (!(object instanceof PGPSignatureList)) {
LOGGER.warning("invalid signature packet");
result.errors.add(Coder.Error.INVALID_SIGNATURE_DATA);
return result;
}
PGPSignatureList signatureList = (PGPSignatureList) object;
if (signatureList.isEmpty()) {
LOGGER.warning("no signature in signature list");
result.errors.add(Coder.Error.INVALID_SIGNATURE_DATA);
return result;
}
PGPSignature signature = signatureList.get(0);
// TODO signature.getCreationTime()
if (ops.verify(signature)) {
// signature verification successful!
result.signing = Coder.Signing.VERIFIED;
} else {
LOGGER.warning("signature verification failed");
result.errors.add(Coder.Error.INVALID_SIGNATURE);
}
return result;
}
示例15: verify
import org.bouncycastle.openpgp.PGPSignatureList; //导入依赖的package包/类
/**
* Verify the specified {@code file}.
* @param file the file to be verified. Must not be <code>null</code>. There must be a second file
* with the same name and the additional suffix ".sig" next to this file (in the same directory).
* This secondary file is a so-called detached signature.
* @throws PGPVerifyException if the given {@code file} could not be verified successfully. Either
* there is no detached-signature-file, or its signature is broken or its signature does not match
* any of the {@linkplain #getPublicKeyRingWithTrustedKeys() trusted keys}.
*/
public void verify(final File file, final File signatureFile) throws PGPVerifyException {
AssertUtil.assertNotNull(file, "file");
AssertUtil.assertNotNull(signatureFile, "signatureFile");
final PGPSignatureList sl = readSignatureFile(signatureFile);
final PGPPublicKeyRingCollection publicKeyRing = getPublicKeyRingWithTrustedKeys();
for (int index = 0; index < sl.size(); ++index) {
try {
final PGPSignature signature = sl.get(index);
signature.init(new BcPGPContentVerifierBuilderProvider(), publicKeyRing.getPublicKey(signature.getKeyID()));
final InputStream contentIn = castStream(file.createInputStream());
try {
final byte[] buf = new byte[16 * 1024];
int len;
while (0 <= (len = contentIn.read(buf))) {
if (len > 0)
signature.update(buf, 0, len);
}
} finally {
contentIn.close();
}
if (signature.verify())
return;
} catch (final Exception e) {
throw new PGPVerifyException(file.getAbsolutePath() + ": " + e, e);
}
}
throw new PGPVerifyException(file.getAbsolutePath());
}