¶Ô±ÈÐÂÎļþ |
| | |
| | | package org.springblade.core.api.crypto.util; |
| | | |
| | | import org.springblade.core.api.crypto.annotation.decrypt.ApiDecrypt; |
| | | import org.springblade.core.api.crypto.annotation.encrypt.ApiEncrypt; |
| | | import org.springblade.core.api.crypto.bean.CryptoInfoBean; |
| | | import org.springblade.core.api.crypto.config.ApiCryptoProperties; |
| | | import org.springblade.core.api.crypto.enums.CryptoType; |
| | | import org.springblade.core.api.crypto.exception.EncryptBodyFailException; |
| | | import org.springblade.core.api.crypto.exception.EncryptMethodNotFoundException; |
| | | import org.springblade.core.api.crypto.exception.KeyNotConfiguredException; |
| | | import org.springblade.core.tool.utils.*; |
| | | import org.springframework.core.MethodParameter; |
| | | import org.springframework.lang.Nullable; |
| | | |
| | | import java.util.Objects; |
| | | |
| | | /** |
| | | * <p>è¾
婿£æµå·¥å
·ç±»</p> |
| | | * |
| | | * @author licoy.cn, L.cm |
| | | */ |
| | | public class ApiCryptoUtil { |
| | | |
| | | /** |
| | | * è·åæ¹æ³æ§å¶å¨ä¸çå 坿³¨è§£ä¿¡æ¯ |
| | | * |
| | | * @param methodParameter æ§å¶å¨æ¹æ³ |
| | | * @return å 坿³¨è§£ä¿¡æ¯ |
| | | */ |
| | | @Nullable |
| | | public static CryptoInfoBean getEncryptInfo(MethodParameter methodParameter) { |
| | | ApiEncrypt encryptBody = ClassUtil.getAnnotation(methodParameter.getMethod(), ApiEncrypt.class); |
| | | if (encryptBody == null) { |
| | | return null; |
| | | } |
| | | return new CryptoInfoBean(encryptBody.value(), encryptBody.secretKey()); |
| | | } |
| | | |
| | | /** |
| | | * è·åæ¹æ³æ§å¶å¨ä¸çè§£å¯æ³¨è§£ä¿¡æ¯ |
| | | * |
| | | * @param methodParameter æ§å¶å¨æ¹æ³ |
| | | * @return å 坿³¨è§£ä¿¡æ¯ |
| | | */ |
| | | @Nullable |
| | | public static CryptoInfoBean getDecryptInfo(MethodParameter methodParameter) { |
| | | ApiDecrypt decryptBody = ClassUtil.getAnnotation(methodParameter.getMethod(), ApiDecrypt.class); |
| | | if (decryptBody == null) { |
| | | return null; |
| | | } |
| | | return new CryptoInfoBean(decryptBody.value(), decryptBody.secretKey()); |
| | | } |
| | | |
| | | /** |
| | | * éæ©å 坿¹å¼å¹¶è¿è¡å å¯ |
| | | * |
| | | * @param jsonData json æ°æ® |
| | | * @param infoBean å å¯ä¿¡æ¯ |
| | | * @return å å¯ç»æ |
| | | */ |
| | | public static String encryptData(ApiCryptoProperties properties, byte[] jsonData, CryptoInfoBean infoBean) { |
| | | CryptoType type = infoBean.getType(); |
| | | if (type == null) { |
| | | throw new EncryptMethodNotFoundException(); |
| | | } |
| | | String secretKey = infoBean.getSecretKey(); |
| | | if (type == CryptoType.DES) { |
| | | secretKey = ApiCryptoUtil.checkSecretKey(properties.getDesKey(), secretKey, "DES"); |
| | | return DesUtil.encryptToBase64(jsonData, secretKey); |
| | | } |
| | | if (type == CryptoType.AES) { |
| | | secretKey = ApiCryptoUtil.checkSecretKey(properties.getAesKey(), secretKey, "AES"); |
| | | return AesUtil.encryptToBase64(jsonData, secretKey); |
| | | } |
| | | if (type == CryptoType.RSA) { |
| | | String privateKey = Objects.requireNonNull(properties.getRsaPrivateKey()); |
| | | return RsaUtil.encryptByPrivateKeyToBase64(privateKey, jsonData); |
| | | } |
| | | throw new EncryptBodyFailException(); |
| | | } |
| | | |
| | | /** |
| | | * éæ©å 坿¹å¼å¹¶è¿è¡è§£å¯ |
| | | * |
| | | * @param bodyData byte array |
| | | * @param infoBean å å¯ä¿¡æ¯ |
| | | * @return è§£å¯ç»æ |
| | | */ |
| | | public static byte[] decryptData(ApiCryptoProperties properties, byte[] bodyData, CryptoInfoBean infoBean) { |
| | | CryptoType type = infoBean.getType(); |
| | | if (type == null) { |
| | | throw new EncryptMethodNotFoundException(); |
| | | } |
| | | String secretKey = infoBean.getSecretKey(); |
| | | if (type == CryptoType.AES) { |
| | | secretKey = ApiCryptoUtil.checkSecretKey(properties.getAesKey(), secretKey, "AES"); |
| | | return AesUtil.decryptFormBase64(bodyData, secretKey); |
| | | } |
| | | if (type == CryptoType.DES) { |
| | | secretKey = ApiCryptoUtil.checkSecretKey(properties.getDesKey(), secretKey, "DES"); |
| | | return DesUtil.decryptFormBase64(bodyData, secretKey); |
| | | } |
| | | if (type == CryptoType.RSA) { |
| | | String privateKey = Objects.requireNonNull(properties.getRsaPrivateKey()); |
| | | return RsaUtil.decryptFromBase64(privateKey, bodyData); |
| | | } |
| | | throw new EncryptMethodNotFoundException(); |
| | | } |
| | | |
| | | /** |
| | | * æ£éªç§é¥ |
| | | * |
| | | * @param k1 é
ç½®çç§é¥ |
| | | * @param k2 注解ä¸çç§é¥ |
| | | * @param keyName keyåç§° |
| | | * @return ç§é¥ |
| | | */ |
| | | private static String checkSecretKey(String k1, String k2, String keyName) { |
| | | if (StringUtil.isBlank(k1) && StringUtil.isBlank(k2)) { |
| | | throw new KeyNotConfiguredException(String.format("%s key is not configured (æªé
ç½®%s)", keyName, keyName)); |
| | | } |
| | | return StringUtil.isBlank(k2) ? k1 : k2; |
| | | } |
| | | } |