This commit is contained in:
Looly 2022-12-27 16:44:14 +08:00
parent 31407e11a0
commit f013446279
2 changed files with 32 additions and 14 deletions

View File

@ -8,6 +8,9 @@ import java.security.Provider;
* @author looly * @author looly
*/ */
public enum GlobalBouncyCastleProvider { public enum GlobalBouncyCastleProvider {
/**
* 单例对象
*/
INSTANCE; INSTANCE;
private Provider provider; private Provider provider;

View File

@ -745,9 +745,8 @@ public class KeyUtil {
* @return {@link KeyStore} * @return {@link KeyStore}
*/ */
public static KeyStore readKeyStore(final String type, final InputStream in, final char[] password) { public static KeyStore readKeyStore(final String type, final InputStream in, final char[] password) {
final KeyStore keyStore; final KeyStore keyStore = getKeyStore(type);
try { try {
keyStore = KeyStore.getInstance(type);
keyStore.load(in, password); keyStore.load(in, password);
} catch (final Exception e) { } catch (final Exception e) {
throw new CryptoException(e); throw new CryptoException(e);
@ -755,6 +754,21 @@ public class KeyUtil {
return keyStore; return keyStore;
} }
/**
* 获取{@link KeyStore}对象
*
* @param type 类型
* @return {@link KeyStore}
*/
public static KeyStore getKeyStore(final String type) {
final Provider provider = GlobalBouncyCastleProvider.INSTANCE.getProvider();
try {
return null == provider ? KeyStore.getInstance(type) : KeyStore.getInstance(type, provider);
} catch (final KeyStoreException e) {
throw new CryptoException(e);
}
}
/** /**
* 从KeyStore中获取私钥公钥 * 从KeyStore中获取私钥公钥
* *
@ -993,6 +1007,7 @@ public class KeyUtil {
/** /**
* 将密钥编码为Base64格式 * 将密钥编码为Base64格式
*
* @param key 密钥 * @param key 密钥
* @return Base64格式密钥 * @return Base64格式密钥
* @since 5.7.22 * @since 5.7.22