优化 非对称加密 60%(未完)

v1.4.1
hiparker 4 years ago
parent a8177cf1dd
commit 7b4a28ff81

@ -22,6 +22,9 @@ import cn.hutool.crypto.SmUtil;
import cn.hutool.crypto.asymmetric.*; import cn.hutool.crypto.asymmetric.*;
import cn.hutool.json.JSONException; import cn.hutool.json.JSONException;
import cn.hutool.json.JSONUtil; import cn.hutool.json.JSONUtil;
import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;
import com.google.common.collect.Maps;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import opsli.plugins.crypto.enums.CryptoAsymmetricType; import opsli.plugins.crypto.enums.CryptoAsymmetricType;
import opsli.plugins.crypto.exception.CryptoException; import opsli.plugins.crypto.exception.CryptoException;
@ -30,16 +33,47 @@ import opsli.plugins.crypto.msg.CryptoMsg;
import opsli.plugins.crypto.strategy.CryptoAsymmetricService; import opsli.plugins.crypto.strategy.CryptoAsymmetricService;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import java.util.Map;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicReference;
/** /**
* @BelongsProject: opsli-boot *
* @BelongsPackage: org.opsli.core.utils *
* @Author: Parker * @author Parker
* @CreateTime: 2020-09-19 20:03 * @date 202151810:53:27
* @Description:
*/ */
@Slf4j @Slf4j
public class CryptoAsymmetricAsymmetricImpl implements CryptoAsymmetricService { public class CryptoAsymmetricAsymmetricImpl implements CryptoAsymmetricService {
/** 默认缓存个数 超出后流量自动清理 */
private static final int DEFAULT_CACHE_COUNT = 1000;
/** 默认缓存时效 超出后自动清理 */
private static final int DEFAULT_CACHE_TIME = 20;
/** 加解密执行器缓存 防止多次创建 */
private static final Map<CryptoAsymmetricType, Cache<String, AbstractAsymmetricCrypto<?>>> LFU_CACHE_MAP;
static{
// 初始化缓存类对象
LFU_CACHE_MAP = Maps.newConcurrentMap();
LFU_CACHE_MAP.put(CryptoAsymmetricType.RSA,
CacheBuilder
.newBuilder().maximumSize(DEFAULT_CACHE_COUNT)
.expireAfterWrite(DEFAULT_CACHE_TIME, TimeUnit.MINUTES).build()
);
LFU_CACHE_MAP.put(CryptoAsymmetricType.SM2,
CacheBuilder
.newBuilder().maximumSize(DEFAULT_CACHE_COUNT)
.expireAfterWrite(DEFAULT_CACHE_TIME, TimeUnit.MINUTES).build()
);
LFU_CACHE_MAP.put(CryptoAsymmetricType.ECIES,
CacheBuilder
.newBuilder().maximumSize(DEFAULT_CACHE_COUNT)
.expireAfterWrite(DEFAULT_CACHE_TIME, TimeUnit.MINUTES).build()
);
}
/** /**
* *
* @return Model * @return Model
@ -56,26 +90,14 @@ public class CryptoAsymmetricAsymmetricImpl implements CryptoAsymmetricService {
*/ */
@Override @Override
public CryptoAsymmetric createKeyModel(final CryptoAsymmetricType cryptoAsymmetricType){ public CryptoAsymmetric createKeyModel(final CryptoAsymmetricType cryptoAsymmetricType){
AbstractAsymmetricCrypto<?> cryptoHandler =
this.createCryptoHandler(cryptoAsymmetricType);
CryptoAsymmetric model = this.createNilModel(); CryptoAsymmetric model = this.createNilModel();
model.setCryptoType(cryptoAsymmetricType);
switch (cryptoAsymmetricType){ if(cryptoHandler != null){
case RSA: model.setCryptoType(cryptoAsymmetricType);
RSA rsa = SecureUtil.rsa(); model.setPublicKey(cryptoHandler.getPublicKeyBase64());
model.setPublicKey(rsa.getPublicKeyBase64()); model.setPrivateKey(cryptoHandler.getPrivateKeyBase64());
model.setPrivateKey(rsa.getPrivateKeyBase64());
break;
case SM2:
SM2 sm2 = SmUtil.sm2();
model.setPublicKey(sm2.getPublicKeyBase64());
model.setPrivateKey(sm2.getPrivateKeyBase64());
break;
case ECIES:
ECIES ecies = new ECIES();
model.setPublicKey(ecies.getPublicKeyBase64());
model.setPrivateKey(ecies.getPrivateKeyBase64());
break;
default:
break;
} }
return model; return model;
} }
@ -90,14 +112,7 @@ public class CryptoAsymmetricAsymmetricImpl implements CryptoAsymmetricService {
@Override @Override
public String encrypt(final CryptoAsymmetric model, final Object data){ public String encrypt(final CryptoAsymmetric model, final Object data){
// 非法验证 // 非法验证
if(model == null || this.verify(model);
model.getCryptoType() == null ||
StringUtils.isEmpty(model.getPrivateKey()) ||
StringUtils.isEmpty(model.getPublicKey())
){
// 配置信息未初始化
throw new CryptoException(CryptoMsg.CRYPTO_EXCEPTION_MODEL_NULL);
}
// 原始/加密 数据 // 原始/加密 数据
String encryptedStr; String encryptedStr;
@ -105,7 +120,8 @@ public class CryptoAsymmetricAsymmetricImpl implements CryptoAsymmetricService {
encryptedStr = JSONUtil.toJsonStr(data); encryptedStr = JSONUtil.toJsonStr(data);
// 创建执行器 // 创建执行器
AbstractAsymmetricCrypto<?> cryptoHandler = this.createCryptoHandler(model); AbstractAsymmetricCrypto<?> cryptoHandler =
this.createCryptoHandler(model);
if(cryptoHandler == null){ if(cryptoHandler == null){
// 无法获得加解密执行器 // 无法获得加解密执行器
throw new CryptoException(CryptoMsg.CRYPTO_EXCEPTION_HANDLER_NULL); throw new CryptoException(CryptoMsg.CRYPTO_EXCEPTION_HANDLER_NULL);
@ -138,7 +154,9 @@ public class CryptoAsymmetricAsymmetricImpl implements CryptoAsymmetricService {
@Override @Override
public Object decryptToObj(final CryptoAsymmetric model, final String data){ public Object decryptToObj(final CryptoAsymmetric model, final String data){
Object obj; Object obj;
// 解密数据
String decryptedData = decrypt(model, data); String decryptedData = decrypt(model, data);
// 反射对象
try{ try{
obj = JSONUtil.parse(decryptedData); obj = JSONUtil.parse(decryptedData);
}catch (Exception e){ }catch (Exception e){
@ -157,14 +175,7 @@ public class CryptoAsymmetricAsymmetricImpl implements CryptoAsymmetricService {
@Override @Override
public String decrypt(final CryptoAsymmetric model, final String data){ public String decrypt(final CryptoAsymmetric model, final String data){
// 非法验证 // 非法验证
if(model == null || this.verify(model);
model.getCryptoType() == null ||
StringUtils.isEmpty(model.getPrivateKey()) ||
StringUtils.isEmpty(model.getPublicKey())
){
// 配置信息未初始化
throw new CryptoException(CryptoMsg.CRYPTO_EXCEPTION_MODEL_NULL);
}
// 如果解密内容为空 则返回原内容 // 如果解密内容为空 则返回原内容
if(StringUtils.isEmpty(data)){ if(StringUtils.isEmpty(data)){
@ -174,7 +185,8 @@ public class CryptoAsymmetricAsymmetricImpl implements CryptoAsymmetricService {
String decryptStr; String decryptStr;
try { try {
// 创建执行器 // 创建执行器
AbstractAsymmetricCrypto<?> cryptoHandler = this.createCryptoHandler(model); AbstractAsymmetricCrypto<?> cryptoHandler =
this.createCryptoHandler(model);
if(cryptoHandler == null){ if(cryptoHandler == null){
// 无法获得加解密执行器 // 无法获得加解密执行器
throw new CryptoException(CryptoMsg.CRYPTO_EXCEPTION_HANDLER_NULL); throw new CryptoException(CryptoMsg.CRYPTO_EXCEPTION_HANDLER_NULL);
@ -195,31 +207,100 @@ public class CryptoAsymmetricAsymmetricImpl implements CryptoAsymmetricService {
return decryptStr; return decryptStr;
} }
/**
*
* @param model
*/
private void verify(CryptoAsymmetric model){
// 非法验证
if(model == null ||
model.getCryptoType() == null ||
StringUtils.isEmpty(model.getPrivateKey()) ||
StringUtils.isEmpty(model.getPublicKey())
){
// 配置信息未初始化
throw new CryptoException(CryptoMsg.CRYPTO_EXCEPTION_MODEL_NULL);
}
}
/** /**
* *
* 使
*
* @param model * @param model
* @return * @return
*/ */
private AbstractAsymmetricCrypto<?> createCryptoHandler(final CryptoAsymmetric model){ private AbstractAsymmetricCrypto<?> createCryptoHandler(final CryptoAsymmetric model){
AbstractAsymmetricCrypto<?> encryptor = null; // 非法验证
switch (model.getCryptoType()){ if(model == null ||
// 注意 这里 switch 使用的是代码块 方法执行完毕后 直接回收对象 model.getCryptoType() == null ||
StringUtils.isEmpty(model.getPrivateKey()) ||
StringUtils.isEmpty(model.getPublicKey())
){
return null;
}
Cache<String, AbstractAsymmetricCrypto<?>> asymmetricCryptoCache =
LFU_CACHE_MAP.get(model.getCryptoType());
AbstractAsymmetricCrypto<?> cryptoHandler = null;
try {
// 查询并设置缓存
cryptoHandler = asymmetricCryptoCache.get(model.getPublicKey(), () -> {
AbstractAsymmetricCrypto<?> tmp = null;
switch (model.getCryptoType()) {
case RSA: {
tmp = SecureUtil.rsa(model.getPrivateKey(), model.getPublicKey());
break;
}
case SM2: {
tmp = SmUtil.sm2(model.getPrivateKey(), model.getPublicKey());
break;
}
case ECIES: {
tmp = new ECIES(model.getPrivateKey(), model.getPublicKey());
break;
}
default:
break;
}
return tmp;
});
}catch (ExecutionException e){
log.error(e.getMessage(), e);
}
return cryptoHandler;
}
/**
*
* @param cryptoAsymmetricType
* @return Model
*/
private AbstractAsymmetricCrypto<?> createCryptoHandler(final CryptoAsymmetricType cryptoAsymmetricType){
AbstractAsymmetricCrypto<?> cryptoHandler = null;
switch (cryptoAsymmetricType){
case RSA:{ case RSA:{
encryptor = SecureUtil.rsa(model.getPrivateKey(), model.getPublicKey()); cryptoHandler = SecureUtil.rsa();
break; break;
} }
case SM2:{ case SM2:{
encryptor = SmUtil.sm2(model.getPrivateKey(), model.getPublicKey()); cryptoHandler = SmUtil.sm2();
break; break;
} }
case ECIES:{ case ECIES:{
encryptor = new ECIES(model.getPrivateKey(), model.getPublicKey()); cryptoHandler = new ECIES();
break; break;
} }
default: default:
break; break;
} }
return encryptor;
return cryptoHandler;
} }
} }

Loading…
Cancel
Save