@SuppressWarnings("unchecked") public static boolean isCertificationKey(PGPPublicKey key) { if (key.getVersion() <= 3) { return true; } for (PGPSignature sig : new IterableIterator<PGPSignature>(key.getSignatures())) { if (key.isMasterKey() && sig.getKeyID() != key.getKeyID()) { continue; } PGPSignatureSubpacketVector hashed = sig.getHashedSubPackets(); if (hashed != null && (hashed.getKeyFlags() & KeyFlags.CERTIFY_OTHER) != 0) { return true; } PGPSignatureSubpacketVector unhashed = sig.getUnhashedSubPackets(); if (unhashed != null && (unhashed.getKeyFlags() & KeyFlags.CERTIFY_OTHER) != 0) { return true; } } return false; }
@SuppressWarnings("unchecked") public static boolean isSigningKey(PGPPublicKey key) { if (key.getVersion() <= 3) { return true; } // special case if (key.getAlgorithm() == PGPPublicKey.RSA_SIGN) { return true; } for (PGPSignature sig : new IterableIterator<PGPSignature>(key.getSignatures())) { if (key.isMasterKey() && sig.getKeyID() != key.getKeyID()) { continue; } PGPSignatureSubpacketVector hashed = sig.getHashedSubPackets(); if (hashed != null && (hashed.getKeyFlags() & KeyFlags.SIGN_DATA) != 0) { return true; } PGPSignatureSubpacketVector unhashed = sig.getUnhashedSubPackets(); if (unhashed != null && (unhashed.getKeyFlags() & KeyFlags.SIGN_DATA) != 0) { return true; } } return false; }
@SuppressWarnings("unchecked") public static PGPPublicKey getMasterKey(PGPPublicKeyRing keyRing) { if (keyRing == null) { return null; } for (PGPPublicKey key : new IterableIterator<PGPPublicKey>(keyRing.getPublicKeys())) { if (key.isMasterKey()) { return key; } } return null; }
public static Date getExpiryDate(PGPPublicKey key) { Date creationDate = getCreationDate(key); if (key.getValidDays() == 0) { // no expiry return null; } Calendar calendar = GregorianCalendar.getInstance(); calendar.setTime(creationDate); calendar.add(Calendar.DATE, key.getValidDays()); Date expiryDate = calendar.getTime(); return expiryDate; }
public static String getFingerPrint(Context context, long keyId) { PGPPublicKey key = ProviderHelper.getPGPPublicKeyByKeyId(context, keyId); // if it is no public key get it from your own keys... if (key == null) { PGPSecretKey secretKey = ProviderHelper.getPGPSecretKeyByKeyId(context, keyId); if (secretKey == null) { Log.e(Constants.TAG, "Key could not be found!"); return null; } key = secretKey.getPublicKey(); } return convertFingerprintToHex(key.getFingerprint(), true); }
@SuppressWarnings("unchecked") public static String getMainUserId(PGPPublicKey key) { for (String userId : new IterableIterator<String>(key.getUserIDs())) { return userId; } return null; }
public static Vector<PGPPublicKey> getUsableEncryptKeys(PGPPublicKeyRing keyRing) { Vector<PGPPublicKey> usableKeys = new Vector<PGPPublicKey>(); Vector<PGPPublicKey> encryptKeys = getEncryptKeys(keyRing); PGPPublicKey masterKey = null; for (int i = 0; i < encryptKeys.size(); ++i) { PGPPublicKey key = encryptKeys.get(i); if (!isExpired(key) && !key.isRevoked()) { if (key.isMasterKey()) { masterKey = key; } else { usableKeys.add(key); } } } if (masterKey != null) { usableKeys.add(masterKey); } return usableKeys; }
@SuppressWarnings("unchecked") public static boolean isEncryptionKey(PGPPublicKey key) { if (!key.isEncryptionKey()) { return false; } if (key.getVersion() <= 3) { // this must be true now return key.isEncryptionKey(); } // special cases if (key.getAlgorithm() == PGPPublicKey.ELGAMAL_ENCRYPT) { return true; } if (key.getAlgorithm() == PGPPublicKey.RSA_ENCRYPT) { return true; } for (PGPSignature sig : new IterableIterator<PGPSignature>(key.getSignatures())) { if (key.isMasterKey() && sig.getKeyID() != key.getKeyID()) { continue; } PGPSignatureSubpacketVector hashed = sig.getHashedSubPackets(); if (hashed != null && (hashed.getKeyFlags() & (KeyFlags.ENCRYPT_COMMS | KeyFlags.ENCRYPT_STORAGE)) != 0) { return true; } PGPSignatureSubpacketVector unhashed = sig.getUnhashedSubPackets(); if (unhashed != null && (unhashed.getKeyFlags() & (KeyFlags.ENCRYPT_COMMS | KeyFlags.ENCRYPT_STORAGE)) != 0) { return true; } } return false; }
public static Date getCreationDate(PGPPublicKey key) { return key.getCreationTime(); }
public static String getAlgorithmInfo(PGPPublicKey key) { return getAlgorithmInfo(key.getAlgorithm(), key.getBitStrength()); }