1
0
Fork 0
mirror of https://github.com/pgpainless/pgpainless.git synced 2025-09-13 04:09:38 +02:00

issue #107 Add method KeyRingInfo.isFullyEncrypted()

This commit is contained in:
Ivan Pizhenko 2021-04-25 09:23:09 +03:00
parent 2c4a3fca6a
commit adfe4f33f5
2 changed files with 42 additions and 5 deletions

View file

@ -314,6 +314,25 @@ public class KeyRingInfo {
return true;
}
/**
* Returns true when every secret key on the key ring is encrypted.
* If there is at least one not encrypted secret key on the ring, return false.
* If the ring is a {@link PGPPublicKeyRing}, return false.
*
* @return true if all secret keys are encrypted.
*/
public boolean isFullyEncrypted() {
if (isSecretKey()) {
for (PGPSecretKey secretKey : getSecretKeys()) {
if (secretKey.getS2KUsage() == 0) {
return false;
}
}
return true;
}
return false;
}
public List<PGPSignature> getSelfSignaturesOnKey(long subkeyId) {
PGPPublicKey publicKey = KeyRingUtils.requirePublicKeyFrom(keys, subkeyId);
Iterator<PGPSignature> it = publicKey.getSignaturesForKeyID(keys.getPublicKey().getKeyID());

View file

@ -89,16 +89,34 @@ public class KeyRingInfoTest {
assertTrue(info.isFullyDecrypted());
secretKeys = PGPainless.modifyKeyRing(secretKeys)
.changePassphraseFromOldPassphrase(null)
.withSecureDefaultSettings()
.toNewPassphrase(Passphrase.fromPassword("sw0rdf1sh"))
.done();
secretKeys = encryptSecretKeys(secretKeys);
info = PGPainless.inspectKeyRing(secretKeys);
assertFalse(info.isFullyDecrypted());
}
@Test
public void testIsFullyEncrypted() throws IOException, PGPException {
PGPSecretKeyRing secretKeys = TestKeys.getEmilSecretKeyRing();
KeyRingInfo info = PGPainless.inspectKeyRing(secretKeys);
assertFalse(info.isFullyEncrypted());
secretKeys = encryptSecretKeys(secretKeys);
info = PGPainless.inspectKeyRing(secretKeys);
assertTrue(info.isFullyEncrypted());
}
private static PGPSecretKeyRing encryptSecretKeys(PGPSecretKeyRing secretKeys) throws PGPException {
return PGPainless.modifyKeyRing(secretKeys)
.changePassphraseFromOldPassphrase(null)
.withSecureDefaultSettings()
.toNewPassphrase(Passphrase.fromPassword("sw0rdf1sh"))
.done();
}
@Test
public void testGetSecretKey() throws IOException, PGPException {
PGPSecretKeyRing secretKeys = TestKeys.getCryptieSecretKeyRing();