mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-05-09 23:51:34 +08:00
fix code
This commit is contained in:
parent
31407e11a0
commit
f013446279
@ -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;
|
||||||
|
@ -127,7 +127,7 @@ public class KeyUtil {
|
|||||||
*
|
*
|
||||||
* @param algorithm 算法,支持PBE算法
|
* @param algorithm 算法,支持PBE算法
|
||||||
* @param keySize 密钥长度,<0表示不设定密钥长度,即使用默认长度
|
* @param keySize 密钥长度,<0表示不设定密钥长度,即使用默认长度
|
||||||
* @param random 随机数生成器,null表示默认
|
* @param random 随机数生成器,null表示默认
|
||||||
* @return {@link SecretKey}
|
* @return {@link SecretKey}
|
||||||
* @since 5.5.2
|
* @since 5.5.2
|
||||||
*/
|
*/
|
||||||
@ -140,7 +140,7 @@ public class KeyUtil {
|
|||||||
keySize = 128;
|
keySize = 128;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(keySize > 0){
|
if (keySize > 0) {
|
||||||
if (null == random) {
|
if (null == random) {
|
||||||
keyGenerator.init(keySize);
|
keyGenerator.init(keySize);
|
||||||
} else {
|
} else {
|
||||||
@ -365,7 +365,7 @@ public class KeyUtil {
|
|||||||
*/
|
*/
|
||||||
public static KeyPair generateKeyPair(final String algorithm) {
|
public static KeyPair generateKeyPair(final String algorithm) {
|
||||||
int keySize = DEFAULT_KEY_SIZE;
|
int keySize = DEFAULT_KEY_SIZE;
|
||||||
if("ECIES".equalsIgnoreCase(algorithm)){
|
if ("ECIES".equalsIgnoreCase(algorithm)) {
|
||||||
// ECIES算法对KEY的长度有要求,此处默认256
|
// ECIES算法对KEY的长度有要求,此处默认256
|
||||||
keySize = 256;
|
keySize = 256;
|
||||||
}
|
}
|
||||||
@ -643,7 +643,7 @@ public class KeyUtil {
|
|||||||
public static String getAlgorithmAfterWith(String algorithm) {
|
public static String getAlgorithmAfterWith(String algorithm) {
|
||||||
Assert.notNull(algorithm, "algorithm must be not null !");
|
Assert.notNull(algorithm, "algorithm must be not null !");
|
||||||
|
|
||||||
if(StrUtil.startWithIgnoreCase(algorithm, "ECIESWith")){
|
if (StrUtil.startWithIgnoreCase(algorithm, "ECIESWith")) {
|
||||||
return "EC";
|
return "EC";
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -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中获取私钥公钥
|
||||||
*
|
*
|
||||||
@ -953,9 +967,9 @@ public class KeyUtil {
|
|||||||
* @return RSA公钥,null表示私钥不被支持
|
* @return RSA公钥,null表示私钥不被支持
|
||||||
* @since 5.3.6
|
* @since 5.3.6
|
||||||
*/
|
*/
|
||||||
public static PublicKey getRSAPublicKey(final PrivateKey privateKey){
|
public static PublicKey getRSAPublicKey(final PrivateKey privateKey) {
|
||||||
if(privateKey instanceof RSAPrivateCrtKey){
|
if (privateKey instanceof RSAPrivateCrtKey) {
|
||||||
final RSAPrivateCrtKey privk = (RSAPrivateCrtKey)privateKey;
|
final RSAPrivateCrtKey privk = (RSAPrivateCrtKey) privateKey;
|
||||||
return getRSAPublicKey(privk.getModulus(), privk.getPublicExponent());
|
return getRSAPublicKey(privk.getModulus(), privk.getPublicExponent());
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
@ -964,12 +978,12 @@ public class KeyUtil {
|
|||||||
/**
|
/**
|
||||||
* 获得RSA公钥对象
|
* 获得RSA公钥对象
|
||||||
*
|
*
|
||||||
* @param modulus Modulus
|
* @param modulus Modulus
|
||||||
* @param publicExponent Public Exponent
|
* @param publicExponent Public Exponent
|
||||||
* @return 公钥
|
* @return 公钥
|
||||||
* @since 5.3.6
|
* @since 5.3.6
|
||||||
*/
|
*/
|
||||||
public static PublicKey getRSAPublicKey(final String modulus, final String publicExponent){
|
public static PublicKey getRSAPublicKey(final String modulus, final String publicExponent) {
|
||||||
return getRSAPublicKey(
|
return getRSAPublicKey(
|
||||||
new BigInteger(modulus, 16), new BigInteger(publicExponent, 16));
|
new BigInteger(modulus, 16), new BigInteger(publicExponent, 16));
|
||||||
}
|
}
|
||||||
@ -977,12 +991,12 @@ public class KeyUtil {
|
|||||||
/**
|
/**
|
||||||
* 获得RSA公钥对象
|
* 获得RSA公钥对象
|
||||||
*
|
*
|
||||||
* @param modulus Modulus
|
* @param modulus Modulus
|
||||||
* @param publicExponent Public Exponent
|
* @param publicExponent Public Exponent
|
||||||
* @return 公钥
|
* @return 公钥
|
||||||
* @since 5.3.6
|
* @since 5.3.6
|
||||||
*/
|
*/
|
||||||
public static PublicKey getRSAPublicKey(final BigInteger modulus, final BigInteger publicExponent){
|
public static PublicKey getRSAPublicKey(final BigInteger modulus, final BigInteger publicExponent) {
|
||||||
final RSAPublicKeySpec publicKeySpec = new RSAPublicKeySpec(modulus, publicExponent);
|
final RSAPublicKeySpec publicKeySpec = new RSAPublicKeySpec(modulus, publicExponent);
|
||||||
try {
|
try {
|
||||||
return getKeyFactory("RSA").generatePublic(publicKeySpec);
|
return getKeyFactory("RSA").generatePublic(publicKeySpec);
|
||||||
@ -993,11 +1007,12 @@ public class KeyUtil {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* 将密钥编码为Base64格式
|
* 将密钥编码为Base64格式
|
||||||
|
*
|
||||||
* @param key 密钥
|
* @param key 密钥
|
||||||
* @return Base64格式密钥
|
* @return Base64格式密钥
|
||||||
* @since 5.7.22
|
* @since 5.7.22
|
||||||
*/
|
*/
|
||||||
public static String toBase64(final Key key){
|
public static String toBase64(final Key key) {
|
||||||
return Base64.encode(key.getEncoded());
|
return Base64.encode(key.getEncoded());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user