本文整理汇总了Java中org.bouncycastle.openpgp.PGPPublicKeyRingCollection类的典型用法代码示例。如果您正苦于以下问题:Java PGPPublicKeyRingCollection类的具体用法?Java PGPPublicKeyRingCollection怎么用?Java PGPPublicKeyRingCollection使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
PGPPublicKeyRingCollection类属于org.bouncycastle.openpgp包,在下文中一共展示了PGPPublicKeyRingCollection类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: readPublicKey
import org.bouncycastle.openpgp.PGPPublicKeyRingCollection; //导入依赖的package包/类
public static PGPPublicKey readPublicKey(InputStream in)
throws IOException, PGPException {
in = PGPUtil.getDecoderStream(in);
PGPPublicKeyRingCollection pgpPub = new PGPPublicKeyRingCollection(in, new JcaKeyFingerprintCalculator() );
Iterator rIt = pgpPub.getKeyRings();
while (rIt.hasNext()) {
PGPPublicKeyRing kRing = (PGPPublicKeyRing) rIt.next();
Iterator kIt = kRing.getPublicKeys();
while (kIt.hasNext()) {
PGPPublicKey k = (PGPPublicKey) kIt.next();
if (k.isEncryptionKey()) {
return k;
}
}
}
throw new IllegalArgumentException(
"Can't find encryption key in key ring.");
}
示例2: readPublicKey
import org.bouncycastle.openpgp.PGPPublicKeyRingCollection; //导入依赖的package包/类
public static PGPPublicKey readPublicKey( InputStream is ) throws IOException, PGPException
{
PGPPublicKeyRingCollection pgpPub =
new PGPPublicKeyRingCollection( PGPUtil.getDecoderStream( is ), new JcaKeyFingerprintCalculator() );
Iterator keyRingIter = pgpPub.getKeyRings();
while ( keyRingIter.hasNext() )
{
PGPPublicKeyRing keyRing = ( PGPPublicKeyRing ) keyRingIter.next();
Iterator keyIter = keyRing.getPublicKeys();
while ( keyIter.hasNext() )
{
PGPPublicKey key = ( PGPPublicKey ) keyIter.next();
if ( key.isEncryptionKey() )
{
return key;
}
}
}
throw new IllegalArgumentException( "Can't find encryption key in key ring." );
}
示例3: getKey
import org.bouncycastle.openpgp.PGPPublicKeyRingCollection; //导入依赖的package包/类
public PGPPublicKey getKey(long keyID) throws IOException, PGPException {
File keyFile = null;
PGPPublicKey key = null;
try {
String path = String.format("%02X/%02X/%016X.asc",
(byte) (keyID >> 56), (byte) (keyID >> 48 & 0xff), keyID);
keyFile = new File(cachePath, path);
if (!keyFile.exists()) {
receiveKey(keyFile, keyID);
}
InputStream keyIn = PGPUtil.getDecoderStream(new FileInputStream(keyFile));
PGPPublicKeyRingCollection pgpRing = new PGPPublicKeyRingCollection(keyIn, new BcKeyFingerprintCalculator());
key = pgpRing.getPublicKey(keyID);
} finally {
if (key == null) {
deleteFile(keyFile);
}
}
return key;
}
示例4: lookupPublicKey
import org.bouncycastle.openpgp.PGPPublicKeyRingCollection; //导入依赖的package包/类
/**
* Search for public key on keyring based on a substring (like an email address).
*
* @throws VerifyException if the key couldn't be found.
* @see #lookupKeyPair
*/
public static PGPPublicKey lookupPublicKey(
PGPPublicKeyRingCollection keyring, String query, KeyRequirement want) {
try {
// Safe by specification.
@SuppressWarnings("unchecked")
Iterator<PGPPublicKeyRing> results =
keyring.getKeyRings(checkNotNull(query, "query"), true, true);
verify(results.hasNext(), "No public key found matching substring: %s", query);
while (results.hasNext()) {
Optional<PGPPublicKey> result = lookupPublicSubkey(results.next(), want);
if (result.isPresent()) {
return result.get();
}
}
throw new VerifyException(String.format(
"No public key (%s) found matching substring: %s", want, query));
} catch (PGPException e) {
throw new VerifyException(String.format(
"Public key lookup with query %s failed: %s", query, e.getMessage()));
}
}
示例5: lookupKeyPair
import org.bouncycastle.openpgp.PGPPublicKeyRingCollection; //导入依赖的package包/类
/**
* Same as {@link #lookupPublicKey} but also retrieves the associated private key.
*
* @throws VerifyException if either keys couldn't be found.
* @see #lookupPublicKey
*/
@SuppressWarnings("deprecation")
public static PGPKeyPair lookupKeyPair(
PGPPublicKeyRingCollection publics,
PGPSecretKeyRingCollection privates,
String query,
KeyRequirement want) {
PGPPublicKey publicKey = lookupPublicKey(publics, query, want);
PGPPrivateKey privateKey;
try {
PGPSecretKey secret = verifyNotNull(privates.getSecretKey(publicKey.getKeyID()),
"Keyring missing private key associated with public key id: %x (query '%s')",
publicKey.getKeyID(), query);
// We do not support putting a password on the private key so we're just going to
// put char[0] here.
privateKey = secret.extractPrivateKey(
new BcPBESecretKeyDecryptorBuilder(new BcPGPDigestCalculatorProvider())
.build(new char[0]));
} catch (PGPException e) {
throw new VerifyException(e.getMessage());
}
return new PGPKeyPair(publicKey, privateKey);
}
示例6: getPublicKeyWithKeyIdAndUserID
import org.bouncycastle.openpgp.PGPPublicKeyRingCollection; //导入依赖的package包/类
/**
* Determines a public key from the keyring collection which has a certain
* key ID and which has a User ID which contains at least one of the User ID
* parts.
*
* @param keyId
* key ID
* @param userIdParts
* user ID parts, can be empty, than no filter on the User ID is
* executed
* @param publicKeyringCollection
* keyring collection
* @return public key or <code>null</code> if no fitting key is found
* @throws PGPException
*/
@SuppressWarnings("unchecked")
public static PGPPublicKey getPublicKeyWithKeyIdAndUserID(long keyId, List<String> userIdParts, PGPPublicKeyRingCollection publicKeyringCollection)
throws PGPException {
PGPPublicKeyRing publicKeyring = publicKeyringCollection.getPublicKeyRing(keyId);
if (publicKeyring == null) {
LOG.debug("No public key found for key ID {}.", Long.toString(keyId));
return null;
}
// publicKey can be a subkey the user IDs must therefore be provided by the primary/master key
if (isAllowedKey(userIdParts, publicKeyring.getPublicKey().getUserIDs())) {
return publicKeyring.getPublicKey(keyId);
} else {
return null;
}
}
示例7: readPublicKey
import org.bouncycastle.openpgp.PGPPublicKeyRingCollection; //导入依赖的package包/类
static PGPPublicKey readPublicKey(String keyringPath) throws Exception {
InputStream input = new ByteArrayInputStream(getKeyRing(keyringPath));
PGPPublicKeyRingCollection pgpPub = new PGPPublicKeyRingCollection(PGPUtil.getDecoderStream(input),
new BcKeyFingerprintCalculator());
@SuppressWarnings("rawtypes")
Iterator keyRingIter = pgpPub.getKeyRings();
while (keyRingIter.hasNext()) {
PGPPublicKeyRing keyRing = (PGPPublicKeyRing) keyRingIter.next();
@SuppressWarnings("rawtypes")
Iterator keyIter = keyRing.getPublicKeys();
while (keyIter.hasNext()) {
PGPPublicKey key = (PGPPublicKey) keyIter.next();
if (key.isEncryptionKey()) {
return key;
}
}
}
throw new IllegalArgumentException("Can't find encryption key in key ring.");
}
示例8: createKeyFrom
import org.bouncycastle.openpgp.PGPPublicKeyRingCollection; //导入依赖的package包/类
public PGPPublicKey createKeyFrom(InputStream in) throws IOException, PGPException {
InputStream pgpData = PGPUtil.getDecoderStream(in);
PGPPublicKeyRingCollection pgpPub = new PGPPublicKeyRingCollection(
PGPUtil.getDecoderStream(pgpData), new JcaKeyFingerprintCalculator());
Iterator keyRingIter = pgpPub.getKeyRings();
while (keyRingIter.hasNext())
{
PGPPublicKeyRing keyRing = (PGPPublicKeyRing)keyRingIter.next();
Iterator keyIter = keyRing.getPublicKeys();
while (keyIter.hasNext())
{
PGPPublicKey key = (PGPPublicKey)keyIter.next();
if (key.isEncryptionKey())
{
return key;
}
}
}
throw new IllegalArgumentException("Can't find encryption key in key ring.");
}
示例9: saveToNotes
import org.bouncycastle.openpgp.PGPPublicKeyRingCollection; //导入依赖的package包/类
private void saveToNotes(ObjectInserter ins, PGPPublicKeyRing keyRing)
throws PGPException, IOException {
long keyId = keyRing.getPublicKey().getKeyID();
PGPPublicKeyRingCollection existing = get(keyId);
List<PGPPublicKeyRing> toWrite = new ArrayList<>(existing.size() + 1);
boolean replaced = false;
for (PGPPublicKeyRing kr : existing) {
if (sameKey(keyRing, kr)) {
toWrite.add(keyRing);
replaced = true;
} else {
toWrite.add(kr);
}
}
if (!replaced) {
toWrite.add(keyRing);
}
notes.set(keyObjectId(keyId), ins.insert(OBJ_BLOB, keysToArmored(toWrite)));
}
示例10: deleteFromNotes
import org.bouncycastle.openpgp.PGPPublicKeyRingCollection; //导入依赖的package包/类
private void deleteFromNotes(ObjectInserter ins, Fingerprint fp)
throws PGPException, IOException {
long keyId = fp.getId();
PGPPublicKeyRingCollection existing = get(keyId);
List<PGPPublicKeyRing> toWrite = new ArrayList<>(existing.size());
for (PGPPublicKeyRing kr : existing) {
if (!fp.equalsBytes(kr.getPublicKey().getFingerprint())) {
toWrite.add(kr);
}
}
if (toWrite.size() == existing.size()) {
return;
} else if (!toWrite.isEmpty()) {
notes.set(keyObjectId(keyId), ins.insert(OBJ_BLOB, keysToArmored(toWrite)));
} else {
notes.remove(keyObjectId(keyId));
}
}
示例11: checkSignature
import org.bouncycastle.openpgp.PGPPublicKeyRingCollection; //导入依赖的package包/类
private Result checkSignature(PGPSignature sig, PushCertificate cert, PublicKeyStore store)
throws PGPException, IOException {
PGPPublicKeyRingCollection keys = store.get(sig.getKeyID());
if (!keys.getKeyRings().hasNext()) {
return new Result(
null,
CheckResult.bad("No public keys found for key ID " + keyIdToString(sig.getKeyID())));
}
PGPPublicKey signer = PublicKeyStore.getSigner(keys, sig, Constants.encode(cert.toText()));
if (signer == null) {
return new Result(
null, CheckResult.bad("Signature by " + keyIdToString(sig.getKeyID()) + " is not valid"));
}
CheckResult result =
publicKeyChecker.setStore(store).setEffectiveTime(sig.getCreationTime()).check(signer);
if (!result.getProblems().isEmpty()) {
StringBuilder err =
new StringBuilder("Invalid public key ")
.append(keyToString(signer))
.append(":\n ")
.append(Joiner.on("\n ").join(result.getProblems()));
return new Result(signer, CheckResult.create(result.getStatus(), err.toString()));
}
return new Result(signer, result);
}
示例12: getKey
import org.bouncycastle.openpgp.PGPPublicKeyRingCollection; //导入依赖的package包/类
PGPPublicKey getKey(long keyID) throws IOException, PGPException {
File keyFile = null;
PGPPublicKey key = null;
try {
String path = String.format("%02X/%02X/%016X.asc",
(byte) (keyID >> 56), (byte) (keyID >> 48 & 0xff), keyID);
keyFile = new File(cachePath, path);
if (!keyFile.exists()) {
receiveKey(keyFile, keyID);
}
InputStream keyIn = PGPUtil.getDecoderStream(new FileInputStream(keyFile));
PGPPublicKeyRingCollection pgpRing = new PGPPublicKeyRingCollection(keyIn, new BcKeyFingerprintCalculator());
key = pgpRing.getPublicKey(keyID);
} finally {
if (key == null) {
deleteFile(keyFile);
}
}
return key;
}
示例13: if
import org.bouncycastle.openpgp.PGPPublicKeyRingCollection; //导入依赖的package包/类
@SuppressWarnings("unchecked")
private final static List<Result> validate
(CHkpSearch.Info candidate, String q, IGetter getter)
throws IOException
{
List<Result> ret = new ArrayList<Result>();
if (shouldSkip(candidate, q)) { return ret; }
// 1. pull down keys from the candidate.
PGPPublicKeyRingCollection pkrc = CHkpSearch.get
(candidate.getKeyId(), getter);
if ((pkrc == null) || (pkrc.size() <= 0)) {
return ret;
}
// 2. Validate each keyring.
Iterator<PGPPublicKeyRing> pkrit = pkrc.getKeyRings();
while (pkrit.hasNext()) {
Result r = validateKeyRing(pkrit.next(), q, getter);
if (r != null) { ret.add(r); }
}
return ret;
}
示例14: verifyGoodSignature
import org.bouncycastle.openpgp.PGPPublicKeyRingCollection; //导入依赖的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();
}
示例15: verifyBadSignature
import org.bouncycastle.openpgp.PGPPublicKeyRingCollection; //导入依赖的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();
}