I have read a lot of bad examples on PEMParser:
int myFunc(String privateKeyFileName, char [] password) {
File privateKeyFile = new File(privateKeyFileName); // private key file in PEM format
PEMParser pemParser = new PEMParser(new FileReader(privateKeyFile));
Object object = pemParser.readObject();
PEMDecryptorProvider decProv = new JcePEMDecryptorProviderBuilder().build(password);
JcaPEMKeyConverter converter = new JcaPEMKeyConverter().setProvider("BC");
KeyPair kp;
if (object instanceof PEMEncryptedKeyPair) {
System.out.println("Encrypted key - we will use provided password");
kp = converter.getKeyPair(((PEMEncryptedKeyPair) object).decryptKeyPair(decProv));
} else {
System.out.println("Unencrypted key - no password needed");
kp = converter.getKeyPair((PEMKeyPair) object);
}
}
Bouncy Castle : PEMReader => PEMParser
Also here: How to read .pem file to get private and public key https://www.codota.com/code/java/classes/org.bouncycastle.openssl.PEMParser https://www.programcreek.com/java-api-examples/?api=org.bouncycastle.openssl.PEMParser https://www.javatips.net/api/org.bouncycastle.util.io.pem.pemreader
Does BouncyCastle provide some factory method for this?
The only thing I found is: org.apache.sshd.common.keyprovider.FileKeyPairProvider (Apache Mina SSH Java client implementation). I think something like this could be a part of BouncyCastle itself or Apache Common libraries (fileutils / stringutils).
Is there any factory/builder method or some design pattern implementation or is everyone doing a production code like if (object instance of xxx)
and solving the problems on the go?
Aucun commentaire:
Enregistrer un commentaire