inleft
2022-02-09 9bcb19959eeb9da9bde2561e7278f6d0a55eb151
commit | author | age
9bcb19 1 /*
I 2 Copyright [2020] [https://www.xiaonuo.vip]
3
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
7
8   http://www.apache.org/licenses/LICENSE-2.0
9
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
15
16 Snowy采用APACHE LICENSE 2.0开源协议,您在使用过程中,需要注意以下几点:
17
18 1.请不要删除和修改根目录下的LICENSE文件。
19 2.请不要删除和修改Snowy源码头部的版权声明。
20 3.请保留源码和相关描述文件的项目出处,作者声明等。
21 4.分发源码时候,请注明软件出处 https://gitee.com/xiaonuobase/snowy
22 5.在修改包名,模块名称,项目代码等时,请注明软件出处 https://gitee.com/xiaonuobase/snowy
23 6.若您的项目无法满足以上几点,可申请商业授权,获取Snowy商业授权许可,请在官网购买授权,地址为 https://www.xiaonuo.vip
24  */
25 package vip.xiaonuo.core.util;
26
27 import cn.hutool.log.Log;
28 import com.antherd.smcrypto.sm2.Sm2;
29 import com.antherd.smcrypto.sm3.Sm3;
30 import com.antherd.smcrypto.sm4.Sm4;
31 import com.antherd.smcrypto.sm4.Sm4Options;
32 import vip.xiaonuo.core.cryptogram.keypair;
33
34 /**
35  * 加密工具类,本框架目前使用 https://github.com/antherd/sm-crypto 项目中一些加解密方式
36  * 使用小伙伴需要过等保密评相关,请在此处更改为自己的加密方法,或加密机,使用加密机同时需要替换公钥,私钥在内部无法导出,提供加密的方法
37  *
38  * @author yubaoshan
39  */
40 public class CryptogramUtil {
41
42     private static final Log log = Log.get();
43
44     /**
45      * 加密方法(Sm2 的专门针对前后端分离,非对称秘钥对的方式,暴露出去的公钥,对传输过程中的密码加个密)
46      *
47      * @author yubaoshan
48      * @param str 待加密数据
49      * @return 加密后的密文
50      */
51     public static String doSm2Encrypt (String str) {
52         return Sm2.doEncrypt(str, keypair.PUBLIC_KEY);
53     }
54
55     /**
56      * 解密方法
57      * 如果采用加密机的方法,用try catch 捕捉异常,返回原文值即可
58      *
59      * @author yubaoshan
60      * @param str 密文
61      * @return 解密后的明文
62      */
63     public static String doSm2Decrypt (String str) {
64         // 解密
65         return Sm2.doDecrypt(str, keypair.PRIVATE_KEY);
66     }
67
68     /**
69      * 加密方法
70      *
71      * @author yubaoshan
72      * @param str 待加密数据
73      * @return 加密后的密文
74      */
75     public static String doEncrypt (String str) {
76         // SM4 加密  cbc模式
77         Sm4Options sm4Options4 = new Sm4Options();
78         sm4Options4.setMode("cbc");
79         sm4Options4.setIv("fedcba98765432100123456789abcdef");
80         return Sm4.encrypt(str, keypair.KEY, sm4Options4);
81     }
82
83     /**
84      * 解密方法
85      * 如果采用加密机的方法,用try catch 捕捉异常,返回原文值即可
86      *
87      * @author yubaoshan
88      * @param str 密文
89      * @return 解密后的明文
90      */
91     public static String doDecrypt (String str) {
92         // 解密,cbc 模式,输出 utf8 字符串
93         Sm4Options sm4Options8 = new Sm4Options();
94         sm4Options8.setMode("cbc");
95         sm4Options8.setIv("fedcba98765432100123456789abcdef");
96         String docString =  Sm4.decrypt(str, keypair.KEY, sm4Options8);
97         if (docString.equals("")) {
98             log.warn(">>> 字段解密失败,返回原文值:{}", str);
99             return str;
100         } else {
101             return docString;
102         }
103     }
104
105     /**
106      * 纯签名
107      *
108      * @author yubaoshan
109      * @param str 待签名数据
110      * @return 签名结果
111      */
112     public static String doSignature (String str) {
113         return Sm2.doSignature(str, keypair.PRIVATE_KEY);
114     }
115
116     /**
117      * 验证签名结果
118      *
119      * @author yubaoshan
120      * @param originalStr 签名原文数据
121      * @param str 签名结果
122      * @return 是否通过
123      */
124     public static boolean doVerifySignature (String originalStr, String str) {
125         return Sm2.doVerifySignature(originalStr, str, keypair.PUBLIC_KEY);
126     }
127
128     /**
129      * 通过杂凑算法取得hash值,用于做数据完整性保护
130      *
131      * @author yubaoshan
132      * @param str 字符串
133      * @return hash 值
134      */
135     public static String doHashValue (String str) {
136         return Sm3.sm3(str);
137     }
138
139 }