yangys
2024-03-31 2969df3e404db3cd116f27db1495e523ce05bf86
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
package com.qianwen.core.tool.utils;
 
import java.security.InvalidKeyException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import javax.crypto.Mac;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import org.springframework.lang.Nullable;
import org.springframework.util.DigestUtils;
 
/* loaded from: blade-core-tool-9.3.0.0-SNAPSHOT.jar:org/springblade/core/tool/utils/DigestUtil.class */
public class DigestUtil extends DigestUtils {
    private static final char[] HEX_CODE = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
 
    public static String md5Hex(final String data) {
        return DigestUtils.md5DigestAsHex(data.getBytes(Charsets.UTF_8));
    }
 
    public static String md5Hex(final byte[] bytes) {
        return DigestUtils.md5DigestAsHex(bytes);
    }
 
    public static String sha1Hex(String data) {
        return sha1Hex(data.getBytes(Charsets.UTF_8));
    }
 
    public static String sha1Hex(final byte[] bytes) {
        return digestHex("SHA-1", bytes);
    }
 
    public static String sha224Hex(String data) {
        return sha224Hex(data.getBytes(Charsets.UTF_8));
    }
 
    public static String sha224Hex(final byte[] bytes) {
        return digestHex("SHA-224", bytes);
    }
 
    public static String sha256Hex(String data) {
        return sha256Hex(data.getBytes(Charsets.UTF_8));
    }
 
    public static String sha256Hex(final byte[] bytes) {
        return digestHex("SHA-256", bytes);
    }
 
    public static String sha384Hex(String data) {
        return sha384Hex(data.getBytes(Charsets.UTF_8));
    }
 
    public static String sha384Hex(final byte[] bytes) {
        return digestHex("SHA-384", bytes);
    }
 
    public static String sha512Hex(String data) {
        return sha512Hex(data.getBytes(Charsets.UTF_8));
    }
 
    public static String sha512Hex(final byte[] bytes) {
        return digestHex("SHA-512", bytes);
    }
 
    public static String digestHex(String algorithm, byte[] bytes) {
        try {
            MessageDigest md = MessageDigest.getInstance(algorithm);
            return encodeHex(md.digest(bytes));
        } catch (NoSuchAlgorithmException e) {
            throw Exceptions.unchecked(e);
        }
    }
 
    public static String hmacMd5Hex(String data, String key) {
        return hmacMd5Hex(data.getBytes(Charsets.UTF_8), key);
    }
 
    public static String hmacMd5Hex(final byte[] bytes, String key) {
        return digestHMacHex("HmacMD5", bytes, key);
    }
 
    public static String hmacSha1Hex(String data, String key) {
        return hmacSha1Hex(data.getBytes(Charsets.UTF_8), key);
    }
 
    public static String hmacSha1Hex(final byte[] bytes, String key) {
        return digestHMacHex("HmacSHA1", bytes, key);
    }
 
    public static String hmacSha224Hex(String data, String key) {
        return hmacSha224Hex(data.getBytes(Charsets.UTF_8), key);
    }
 
    public static String hmacSha224Hex(final byte[] bytes, String key) {
        return digestHMacHex("HmacSHA224", bytes, key);
    }
 
    public static byte[] hmacSha256(String data, String key) {
        return hmacSha256(data.getBytes(Charsets.UTF_8), key);
    }
 
    public static byte[] hmacSha256(final byte[] bytes, String key) {
        return digestHMac("HmacSHA256", bytes, key);
    }
 
    public static String hmacSha256Hex(String data, String key) {
        return hmacSha256Hex(data.getBytes(Charsets.UTF_8), key);
    }
 
    public static String hmacSha256Hex(final byte[] bytes, String key) {
        return digestHMacHex("HmacSHA256", bytes, key);
    }
 
    public static String hmacSha384Hex(String data, String key) {
        return hmacSha384Hex(data.getBytes(Charsets.UTF_8), key);
    }
 
    public static String hmacSha384Hex(final byte[] bytes, String key) {
        return digestHMacHex("HmacSHA384", bytes, key);
    }
 
    public static String hmacSha512Hex(String data, String key) {
        return hmacSha512Hex(data.getBytes(Charsets.UTF_8), key);
    }
 
    public static String hmacSha512Hex(final byte[] bytes, String key) {
        return digestHMacHex("HmacSHA512", bytes, key);
    }
 
    public static String digestHMacHex(String algorithm, final byte[] bytes, String key) {
        SecretKey secretKey = new SecretKeySpec(key.getBytes(Charsets.UTF_8), algorithm);
        try {
            Mac mac = Mac.getInstance(secretKey.getAlgorithm());
            mac.init(secretKey);
            return encodeHex(mac.doFinal(bytes));
        } catch (InvalidKeyException | NoSuchAlgorithmException e) {
            throw Exceptions.unchecked(e);
        }
    }
 
    public static byte[] digestHMac(String algorithm, final byte[] bytes, String key) {
        SecretKey secretKey = new SecretKeySpec(key.getBytes(Charsets.UTF_8), algorithm);
        try {
            Mac mac = Mac.getInstance(secretKey.getAlgorithm());
            mac.init(secretKey);
            return mac.doFinal(bytes);
        } catch (InvalidKeyException | NoSuchAlgorithmException e) {
            throw Exceptions.unchecked(e);
        }
    }
 
    public static String encodeHex(byte[] bytes) {
        StringBuilder r = new StringBuilder(bytes.length * 2);
        for (byte b : bytes) {
            r.append(HEX_CODE[(b >> 4) & 15]);
            r.append(HEX_CODE[b & 15]);
        }
        return r.toString();
    }
 
    public static byte[] decodeHex(final String hexStr) {
        return DatatypeConverterUtil.parseHexBinary(hexStr);
    }
 
    public static boolean slowEquals(@Nullable String a, @Nullable String b) {
        if (a == null || b == null) {
            return false;
        }
        return slowEquals(a.getBytes(Charsets.UTF_8), b.getBytes(Charsets.UTF_8));
    }
 
    public static boolean slowEquals(@Nullable byte[] a, @Nullable byte[] b) {
        if (a == null || b == null || a.length != b.length) {
            return false;
        }
        int diff = a.length ^ b.length;
        for (int i = 0; i < a.length; i++) {
            diff |= a[i] ^ b[i];
        }
        return diff == 0;
    }
 
    public static String hex(String data) {
        if (StringUtil.isBlank(data)) {
            return StringPool.EMPTY;
        }
        return sha1Hex(data);
    }
 
    public static String encrypt(String data) {
        if (StringUtil.isBlank(data)) {
            return StringPool.EMPTY;
        }
        return sha1Hex(md5Hex(data));
    }
}