|
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+package com.lframework.xingyun.template.inner.sign.util;
|
|
|
2
|
+
|
|
|
3
|
+import com.aliyuncs.utils.IOUtils;
|
|
|
4
|
+import org.springframework.util.DigestUtils;
|
|
|
5
|
+
|
|
|
6
|
+import javax.crypto.Cipher;
|
|
|
7
|
+import javax.crypto.SecretKey;
|
|
|
8
|
+import javax.crypto.spec.SecretKeySpec;
|
|
|
9
|
+import java.io.ByteArrayInputStream;
|
|
|
10
|
+import java.io.IOException;
|
|
|
11
|
+import java.io.InputStream;
|
|
|
12
|
+import java.security.MessageDigest;
|
|
|
13
|
+import java.security.NoSuchAlgorithmException;
|
|
|
14
|
+import java.util.Base64;
|
|
|
15
|
+
|
|
|
16
|
+public class SecretUtil {
|
|
|
17
|
+ public static String MD5(String decrypt) {
|
|
|
18
|
+ return DigestUtils.md5DigestAsHex(decrypt.getBytes());
|
|
|
19
|
+ }
|
|
|
20
|
+
|
|
|
21
|
+ public static String SHA1(String decrypt) {
|
|
|
22
|
+ try {
|
|
|
23
|
+ MessageDigest digest = MessageDigest.getInstance("SHA-1");
|
|
|
24
|
+ digest.update(decrypt.getBytes());
|
|
|
25
|
+ byte messageDigest[] = digest.digest();
|
|
|
26
|
+ // Create Hex String
|
|
|
27
|
+ StringBuffer hexString = new StringBuffer();
|
|
|
28
|
+ // 字节数组转换为 十六进制 数
|
|
|
29
|
+ for (int i = 0; i < messageDigest.length; i++) {
|
|
|
30
|
+ String shaHex = Integer.toHexString(messageDigest[i] & 0xFF);
|
|
|
31
|
+ if (shaHex.length() < 2) {
|
|
|
32
|
+ hexString.append(0);
|
|
|
33
|
+ }
|
|
|
34
|
+ hexString.append(shaHex);
|
|
|
35
|
+ }
|
|
|
36
|
+ return hexString.toString();
|
|
|
37
|
+
|
|
|
38
|
+ } catch (NoSuchAlgorithmException e) {
|
|
|
39
|
+ e.printStackTrace();
|
|
|
40
|
+ }
|
|
|
41
|
+ return "";
|
|
|
42
|
+ }
|
|
|
43
|
+
|
|
|
44
|
+ public static String SHA256(String s) {
|
|
|
45
|
+ InputStream fis = new ByteArrayInputStream(s.getBytes());
|
|
|
46
|
+ return SHA256(fis);
|
|
|
47
|
+ }
|
|
|
48
|
+
|
|
|
49
|
+ public static String SHA256(InputStream inputStream) {
|
|
|
50
|
+ byte buffer[] = new byte[1024 * 10];
|
|
|
51
|
+ MessageDigest md5 = null;
|
|
|
52
|
+ try {
|
|
|
53
|
+ md5 = MessageDigest.getInstance("SHA-256");
|
|
|
54
|
+ for (int numRead = 0; (numRead = inputStream.read(buffer)) > 0; ) {
|
|
|
55
|
+ md5.update(buffer, 0, numRead);
|
|
|
56
|
+ }
|
|
|
57
|
+ byte[] digest = md5.digest();
|
|
|
58
|
+ StringBuffer strHexString = new StringBuffer();
|
|
|
59
|
+ for (int i = 0; i < digest.length; i++) {
|
|
|
60
|
+ String hex = Integer.toHexString(0xff & digest[i]);
|
|
|
61
|
+ if (hex.length() == 1) {
|
|
|
62
|
+ strHexString.append('0');
|
|
|
63
|
+ }
|
|
|
64
|
+ strHexString.append(hex);
|
|
|
65
|
+ }
|
|
|
66
|
+ return strHexString.toString();
|
|
|
67
|
+ } catch (NoSuchAlgorithmException | IOException e) {
|
|
|
68
|
+ e.printStackTrace();
|
|
|
69
|
+ } finally {
|
|
|
70
|
+ IOUtils.closeQuietly(inputStream);
|
|
|
71
|
+ }
|
|
|
72
|
+ return null;
|
|
|
73
|
+ }
|
|
|
74
|
+
|
|
|
75
|
+
|
|
|
76
|
+ private static String AES_KEY = "j8z&j*a$t{w#k}e@";
|
|
|
77
|
+ private static String AES_ECB = "AES/ECB/PKCS5Padding";
|
|
|
78
|
+
|
|
|
79
|
+ /**
|
|
|
80
|
+ * 使用AES加密原始字符串.
|
|
|
81
|
+ *
|
|
|
82
|
+ * @param input 原始输入字符串
|
|
|
83
|
+ */
|
|
|
84
|
+ public static String AesEncrypt(String input) {
|
|
|
85
|
+ try {
|
|
|
86
|
+ SecretKey secretKey = new SecretKeySpec(AES_KEY.getBytes(), "AES");
|
|
|
87
|
+ Cipher cipher = Cipher.getInstance(AES_ECB);
|
|
|
88
|
+ cipher.init(Cipher.ENCRYPT_MODE, secretKey);
|
|
|
89
|
+ byte[] result = cipher.doFinal(input.getBytes("utf-8"));
|
|
|
90
|
+ return Base64.getEncoder().encodeToString(result);
|
|
|
91
|
+ } catch (Exception e) {
|
|
|
92
|
+ e.printStackTrace();
|
|
|
93
|
+ }
|
|
|
94
|
+ return "";
|
|
|
95
|
+ }
|
|
|
96
|
+
|
|
|
97
|
+ /**
|
|
|
98
|
+ * 使用AES解密原始字符串.
|
|
|
99
|
+ *
|
|
|
100
|
+ * @param input 原始输入字符串
|
|
|
101
|
+ */
|
|
|
102
|
+ public static String AesDecrypt(String input) {
|
|
|
103
|
+ try {
|
|
|
104
|
+ SecretKey secretKey = new SecretKeySpec(AES_KEY.getBytes(), "AES");
|
|
|
105
|
+ Cipher cipher = Cipher.getInstance(AES_ECB);
|
|
|
106
|
+ cipher.init(Cipher.DECRYPT_MODE, secretKey);
|
|
|
107
|
+ byte[] result = cipher.doFinal(Base64.getDecoder().decode(input));
|
|
|
108
|
+ return new String(result, "utf-8");
|
|
|
109
|
+ } catch (Exception e) {
|
|
|
110
|
+ e.printStackTrace();
|
|
|
111
|
+ }
|
|
|
112
|
+ return "";
|
|
|
113
|
+ }
|
|
|
114
|
+
|
|
|
115
|
+
|
|
|
116
|
+}
|