added a method for getting a password with a null checking

This commit is contained in:
IrynaMatveieva 2024-03-06 17:56:51 +02:00
parent 6b8af63c1e
commit 0177b5fdc7
2 changed files with 7 additions and 3 deletions

View File

@ -73,7 +73,7 @@ public class SslUtil {
@SneakyThrows @SneakyThrows
public static PrivateKey readPrivateKey(String fileContent, String passStr) { public static PrivateKey readPrivateKey(String fileContent, String passStr) {
char[] password = StringUtils.isEmpty(passStr) ? EMPTY_PASS : passStr.toCharArray(); char[] password = getPassword(passStr);
PrivateKey privateKey = null; PrivateKey privateKey = null;
JcaPEMKeyConverter keyConverter = new JcaPEMKeyConverter(); JcaPEMKeyConverter keyConverter = new JcaPEMKeyConverter();
@ -102,4 +102,8 @@ public class SslUtil {
return privateKey; return privateKey;
} }
public static char[] getPassword(String passStr) {
return StringUtils.isEmpty(passStr) ? EMPTY_PASS : passStr.toCharArray();
}
} }

View File

@ -87,7 +87,7 @@ public class CertPemCredentials implements ClientCredentials {
private KeyManagerFactory createAndInitKeyManagerFactory() throws Exception { private KeyManagerFactory createAndInitKeyManagerFactory() throws Exception {
KeyManagerFactory kmf = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm()); KeyManagerFactory kmf = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
kmf.init(loadKeyStore(), password.toCharArray()); kmf.init(loadKeyStore(), SslUtil.getPassword(password));
return kmf; return kmf;
} }
@ -107,7 +107,7 @@ public class CertPemCredentials implements ClientCredentials {
CertPath certPath = factory.generateCertPath(certificates); CertPath certPath = factory.generateCertPath(certificates);
List<? extends Certificate> path = certPath.getCertificates(); List<? extends Certificate> path = certPath.getCertificates();
Certificate[] x509Certificates = path.toArray(new Certificate[0]); Certificate[] x509Certificates = path.toArray(new Certificate[0]);
keyStore.setKeyEntry(PRIVATE_KEY_ALIAS, privateKey, password.toCharArray(), x509Certificates); keyStore.setKeyEntry(PRIVATE_KEY_ALIAS, privateKey, SslUtil.getPassword(password), x509Certificates);
} }
return keyStore; return keyStore;
} }