From 74a1166b5f250c2ba8aab027d98bc11e59860c01 Mon Sep 17 00:00:00 2001 From: Charles7c Date: Fri, 9 Feb 2024 17:44:59 +0800 Subject: [PATCH] =?UTF-8?q?perf(security/crypto):=20=E8=8E=B7=E5=8F=96?= =?UTF-8?q?=E5=8A=A0=E5=AF=86=E7=AE=97=E6=B3=95=E5=A2=9E=E5=8A=A0=E7=BC=93?= =?UTF-8?q?=E5=AD=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../AbstractSymmetricCryptoEncryptor.java | 77 +++++++++++++++++++ .../crypto/encryptor/AesEncryptor.java | 18 +---- 2 files changed, 81 insertions(+), 14 deletions(-) create mode 100644 continew-starter-security/continew-starter-security-crypto/src/main/java/top/charles7c/continew/starter/security/crypto/encryptor/AbstractSymmetricCryptoEncryptor.java diff --git a/continew-starter-security/continew-starter-security-crypto/src/main/java/top/charles7c/continew/starter/security/crypto/encryptor/AbstractSymmetricCryptoEncryptor.java b/continew-starter-security/continew-starter-security-crypto/src/main/java/top/charles7c/continew/starter/security/crypto/encryptor/AbstractSymmetricCryptoEncryptor.java new file mode 100644 index 00000000..b3adea5c --- /dev/null +++ b/continew-starter-security/continew-starter-security-crypto/src/main/java/top/charles7c/continew/starter/security/crypto/encryptor/AbstractSymmetricCryptoEncryptor.java @@ -0,0 +1,77 @@ +/* + * Copyright (c) 2022-present Charles7c Authors. All Rights Reserved. + *

+ * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0; + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + *

+ * http://www.gnu.org/licenses/lgpl.html + *

+ * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package top.charles7c.continew.starter.security.crypto.encryptor; + +import cn.hutool.core.util.StrUtil; +import cn.hutool.crypto.symmetric.SymmetricAlgorithm; +import cn.hutool.crypto.symmetric.SymmetricCrypto; +import top.charles7c.continew.starter.core.constant.StringConstants; + +import java.nio.charset.StandardCharsets; +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; + +/** + * 对称加/解密处理器 + * + * @author Charles7c + * @since 1.4.0 + */ +public abstract class AbstractSymmetricCryptoEncryptor implements IEncryptor { + + private static final Map CACHE = new ConcurrentHashMap<>(); + + @Override + public String encrypt(String plaintext, String password, String publicKey) throws Exception { + if (StrUtil.isBlank(plaintext)) { + return plaintext; + } + return this.getCrypto(password).encryptHex(plaintext); + } + + @Override + public String decrypt(String ciphertext, String password, String privateKey) throws Exception { + if (StrUtil.isBlank(ciphertext)) { + return ciphertext; + } + return this.getCrypto(password).decryptStr(ciphertext); + } + + /** + * 获取对称加密算法 + * + * @param password 密钥 + * @return 对称加密算法 + */ + protected SymmetricCrypto getCrypto(String password) { + SymmetricAlgorithm algorithm = this.getAlgorithm(); + String key = algorithm + StringConstants.UNDERLINE + password; + if (CACHE.containsKey(key)) { + return CACHE.get(key); + } + SymmetricCrypto symmetricCrypto = new SymmetricCrypto(algorithm, password.getBytes(StandardCharsets.UTF_8)); + CACHE.put(key, symmetricCrypto); + return symmetricCrypto; + } + + /** + * 获取对称加密算法类型 + * + * @return 对称加密算法类型 + */ + protected abstract SymmetricAlgorithm getAlgorithm(); +} diff --git a/continew-starter-security/continew-starter-security-crypto/src/main/java/top/charles7c/continew/starter/security/crypto/encryptor/AesEncryptor.java b/continew-starter-security/continew-starter-security-crypto/src/main/java/top/charles7c/continew/starter/security/crypto/encryptor/AesEncryptor.java index f9a453ad..08bafe6a 100644 --- a/continew-starter-security/continew-starter-security-crypto/src/main/java/top/charles7c/continew/starter/security/crypto/encryptor/AesEncryptor.java +++ b/continew-starter-security/continew-starter-security-crypto/src/main/java/top/charles7c/continew/starter/security/crypto/encryptor/AesEncryptor.java @@ -16,10 +16,7 @@ package top.charles7c.continew.starter.security.crypto.encryptor; -import cn.hutool.crypto.SecureUtil; -import cn.hutool.crypto.symmetric.AES; - -import java.nio.charset.StandardCharsets; +import cn.hutool.crypto.symmetric.SymmetricAlgorithm; /** * AES(Advanced Encryption Standard) 加/解密处理器 @@ -30,17 +27,10 @@ import java.nio.charset.StandardCharsets; * @author Charles7c * @since 1.4.0 */ -public class AesEncryptor implements IEncryptor { +public class AesEncryptor extends AbstractSymmetricCryptoEncryptor { @Override - public String encrypt(String plaintext, String password, String publicKey) throws Exception { - AES aes = SecureUtil.aes(password.getBytes(StandardCharsets.UTF_8)); - return aes.encryptHex(plaintext); - } - - @Override - public String decrypt(String ciphertext, String password, String privateKey) throws Exception { - AES aes = SecureUtil.aes(password.getBytes(StandardCharsets.UTF_8)); - return aes.decryptStr(ciphertext); + protected SymmetricAlgorithm getAlgorithm() { + return SymmetricAlgorithm.AES; } }