mirror of
https://github.com/pgpainless/pgpainless.git
synced 2025-12-11 06:41:09 +01:00
KeyInfo metods + Take into account GNU S2K
This commit is contained in:
parent
adfe4f33f5
commit
8df9d12e7e
2 changed files with 39 additions and 2 deletions
|
|
@ -19,6 +19,7 @@ import org.bouncycastle.bcpg.ECDHPublicBCPGKey;
|
|||
import org.bouncycastle.bcpg.ECDSAPublicBCPGKey;
|
||||
import org.bouncycastle.bcpg.ECPublicBCPGKey;
|
||||
import org.bouncycastle.bcpg.EdDSAPublicBCPGKey;
|
||||
import org.bouncycastle.bcpg.S2K;
|
||||
import org.bouncycastle.jcajce.provider.asymmetric.util.ECUtil;
|
||||
import org.bouncycastle.openpgp.PGPPublicKey;
|
||||
import org.bouncycastle.openpgp.PGPSecretKey;
|
||||
|
|
@ -39,6 +40,22 @@ public class KeyInfo {
|
|||
this.secretKey = null;
|
||||
}
|
||||
|
||||
public String getCurveName() { return getCurveName(publicKey); }
|
||||
|
||||
/**
|
||||
* Returns indication that a contained secret key is encrypted.
|
||||
*
|
||||
* @return true if secret key is encrypted, false if secret key is not encrypted or there is public key only.
|
||||
*/
|
||||
public boolean isEncrypted() { return secretKey != null && isEncrypted(secretKey); }
|
||||
|
||||
/**
|
||||
* Returns indication that a contained secret key is not encrypted.
|
||||
*
|
||||
* @return true if secret key is not encrypted or there is public key only, false if secret key is encrypted.
|
||||
*/
|
||||
public boolean isDecrypted() { return secretKey == null || isDecrypted(secretKey); }
|
||||
|
||||
public static String getCurveName(PGPPublicKey publicKey) {
|
||||
PublicKeyAlgorithm algorithm = PublicKeyAlgorithm.fromId(publicKey.getAlgorithm());
|
||||
ECPublicBCPGKey key;
|
||||
|
|
@ -64,4 +81,24 @@ public class KeyInfo {
|
|||
public static String getCurveName(ECPublicBCPGKey key) {
|
||||
return ECUtil.getCurveName(key.getCurveOID());
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns indication that a secret key is encrypted.
|
||||
*
|
||||
* @param secretKey A secret key to examine.
|
||||
* @return true if secret key is encrypted, false otherwise.
|
||||
*/
|
||||
public static boolean isEncrypted(PGPSecretKey secretKey) {
|
||||
return secretKey.getS2KUsage() != 0 && secretKey.getS2K().getType() != S2K.GNU_DUMMY_S2K;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns indication that a secret key is not encrypted.
|
||||
*
|
||||
* @param secretKey A secret key to examine.
|
||||
* @return true if secret key is encrypted, false otherwise.
|
||||
*/
|
||||
public static boolean isDecrypted(PGPSecretKey secretKey) {
|
||||
return secretKey.getS2KUsage() == 0 || secretKey.getS2K().getType() == S2K.GNU_DUMMY_S2K;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -306,7 +306,7 @@ public class KeyRingInfo {
|
|||
public boolean isFullyDecrypted() {
|
||||
if (isSecretKey()) {
|
||||
for (PGPSecretKey secretKey : getSecretKeys()) {
|
||||
if (secretKey.getS2KUsage() != 0) {
|
||||
if (KeyInfo.isEncrypted(secretKey)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
|
@ -324,7 +324,7 @@ public class KeyRingInfo {
|
|||
public boolean isFullyEncrypted() {
|
||||
if (isSecretKey()) {
|
||||
for (PGPSecretKey secretKey : getSecretKeys()) {
|
||||
if (secretKey.getS2KUsage() == 0) {
|
||||
if (KeyInfo.isDecrypted(secretKey)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue