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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
| import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.lang.reflect.Constructor;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.Provider;
import java.security.Security;
import java.util.Set;
/**
* 哈希工具类
*
* @author chenchuancheng
* @since 2023/08/28 21:01
*/
public class HashUtils {
private static final String MD5 = "MD5";
private static final String SHA_1 = "SHA-1";
private static final String SHA_224 = "SHA-224";
private static final String SHA_256 = "SHA-256";
private static final String SHA_384 = "SHA-384";
private static final String SHA_512 = "SHA-512";
private static final String SHA3_224 = "SHA3-224";
private static final String SHA3_256 = "SHA3-256";
private static final String SHA3_384 = "SHA3-384";
private static final String SHA3_512 = "SHA3-512";
private static final String SHA3_512_224 = "SHA3-512/224";
private static final String SHA3_512_256 = "SHA3-512/256";
/**
* 中国国密算法。需要自己注册对应的Provider
*/
private static final String SM3 = "SM3";
static {
try {
// 若支持,则注册中国国密算法
Security.addProvider((Provider) Class.forName("org.bouncycastle.jce.provider.BouncyCastleProvider")
.getConstructor()
.newInstance());
} catch (Exception e) {
}
}
/**
* 列举出jdk可用的hash算法
*/
private static void listJDKAvailableAlgorithms() {
// 获取所有已注册的安全提供程序
Provider[] providers = Security.getProviders();
for (Provider provider : providers) {
System.out.println("Provider: " + provider.getName());
Set<Object> keys = provider.keySet();
for (Object key : keys) {
System.out.println(" Algorithm: " + provider.get(key));
}
}
}
/**
* 转换对象到字节数组
*
* @param o 对象
* @return {@link byte[]}
*/
public static byte[] convertObjectToBytes(Object o) {
try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
ObjectOutputStream oos = new ObjectOutputStream(baos);
oos.writeObject(o);
return baos.toByteArray();
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
/**
* 摘要十六进制字符串
*
* @param algorithm 算法
* @param dataToHashBytes 需要散列的数据
* @param saltBytes 盐, 相当于在dataToHashBytes后面直接追加了saltBytes
* @return {@link String}
* @throws NoSuchAlgorithmException 没有这样算法异常
*/
/**
* 摘要十六进制字符串
*
* @param algorithm 算法
* @param dataToHashBytes 需要散列的数据
* @param saltBytes 盐, 相当于在dataToHashBytes后面直接追加了saltBytes
* @return {@link String}
* @throws NoSuchAlgorithmException 没有这样算法异常
*/
public static String digestHexString(String algorithm, byte[] dataToHashBytes, byte[] saltBytes) throws NoSuchAlgorithmException {
byte[] digest = digestBytes(algorithm, dataToHashBytes, saltBytes);
return bytes2HexString(digest);
}
/**
* 将偶数位16进制转换为字节数组
*/
public static byte[] hexString2Bytes(String hexString) {
// 检查输入字符串的有效性
if (hexString == null || hexString.length() % 2 != 0) {
throw new IllegalArgumentException("Invalid hex string");
}
// 将十六进制字符串转换为字节数组
int len = hexString.length();
byte[] byteArray = new byte[len / 2];
for (int i = 0; i < len; i += 2) {
byteArray[i / 2] = (byte) ((Character.digit(hexString.charAt(i), 16) << 4)
+ Character.digit(hexString.charAt(i + 1), 16));
}
return byteArray;
}
/**
* 将字节数组转换为偶数位16进制
*/
public static String bytes2HexString(byte[] digest) {
// 将哈希值转换为十六进制字符串
StringBuilder hexStringBuilder = new StringBuilder();
for (byte b : digest) {
// String hexString = Integer.toHexString(b & 0xff);
// hexStringBuilder.append(hexString);//这个做法转换出来的,比如0f,只会显示为f,并不会补0
/**
* 将byte转换为十六进制字符串
* 0-结果不足指定宽度时,用0填充
* 2-宽度为2
* @see https://stackoverflow.com/questions/2817752/java-code-to-convert-byte-to-hexadecimal
*/
String hexString = String.format("%02x", b);
hexStringBuilder.append(hexString);
}
return hexStringBuilder.toString();
}
/**
* 字节
*
* @param algorithm 算法
* @param dataToHashBytes 数据
* @param saltBytes 盐
* @return {@link byte[]}
* @throws NoSuchAlgorithmException 没有这样算法异常
*/
public static byte[] digestBytes(String algorithm, byte[] dataToHashBytes, byte[] saltBytes) throws NoSuchAlgorithmException {
MessageDigest messageDigest = MessageDigest.getInstance(algorithm);
messageDigest.update(dataToHashBytes);
if (!(saltBytes == null || saltBytes.length <= 0)) {
//相当于在原内容dataToHashBytes上,追加了saltBytes
messageDigest.update(saltBytes);
}
return messageDigest.digest();
}
public static String digestHexStringByMD5(byte[] dataToHash, byte[] salt) throws NoSuchAlgorithmException {
return digestHexString(MD5, dataToHash, salt);
}
public static String digestHexStringByMD5(byte[] dataToHash) throws NoSuchAlgorithmException {
return digestHexString(MD5, dataToHash, null);
}
public static String digestHexStringBySHA1(byte[] dataToHash, byte[] salt) throws NoSuchAlgorithmException {
return digestHexString(SHA_1, dataToHash, salt);
}
public static String digestHexStringBySHA1(byte[] dataToHash) throws NoSuchAlgorithmException {
return digestHexString(SHA_1, dataToHash, null);
}
public static String digestHexStringBySHA224(byte[] dataToHash, byte[] salt) throws NoSuchAlgorithmException {
return digestHexString(SHA_224, dataToHash, salt);
}
public static String digestHexStringBySHA224(byte[] dataToHash) throws NoSuchAlgorithmException {
return digestHexString(SHA_224, dataToHash, null);
}
public static String digestHexStringBySHA256(byte[] dataToHash, byte[] salt) throws NoSuchAlgorithmException {
return digestHexString(SHA_256, dataToHash, salt);
}
public static String digestHexStringBySHA256(byte[] dataToHash) throws NoSuchAlgorithmException {
return digestHexString(SHA_256, dataToHash, null);
}
public static String digestHexStringBySHA384(byte[] dataToHash, byte[] salt) throws NoSuchAlgorithmException {
return digestHexString(SHA_384, dataToHash, salt);
}
public static String digestHexStringBySHA384(byte[] dataToHash) throws NoSuchAlgorithmException {
return digestHexString(SHA_384, dataToHash, null);
}
public static String digestHexStringBySHA512(byte[] dataToHash, byte[] salt) throws NoSuchAlgorithmException {
return digestHexString(SHA_512, dataToHash, salt);
}
public static String digestHexStringBySHA512(byte[] dataToHash) throws NoSuchAlgorithmException {
return digestHexString(SHA_512, dataToHash, null);
}
public static String digestHexStringBySHA3_224(byte[] dataToHash, byte[] salt) throws NoSuchAlgorithmException {
return digestHexString(SHA3_224, dataToHash, salt);
}
public static String digestHexStringBySHA3_224(byte[] dataToHash) throws NoSuchAlgorithmException {
return digestHexString(SHA3_224, dataToHash, null);
}
public static String digestHexStringBySHA3_256(byte[] dataToHash, byte[] salt) throws NoSuchAlgorithmException {
return digestHexString(SHA3_256, dataToHash, salt);
}
public static String digestHexStringBySHA3_256(byte[] dataToHash) throws NoSuchAlgorithmException {
return digestHexString(SHA3_256, dataToHash, null);
}
public static String digestHexStringBySHA3_384(byte[] dataToHash, byte[] salt) throws NoSuchAlgorithmException {
return digestHexString(SHA3_384, dataToHash, salt);
}
public static String digestHexStringBySHA3_384(byte[] dataToHash) throws NoSuchAlgorithmException {
return digestHexString(SHA3_384, dataToHash, null);
}
public static String digestHexStringBySHA3_512(byte[] dataToHash, byte[] salt) throws NoSuchAlgorithmException {
return digestHexString(SHA3_512, dataToHash, salt);
}
public static String digestHexStringBySHA3_512(byte[] dataToHash) throws NoSuchAlgorithmException {
return digestHexString(SHA3_512, dataToHash, null);
}
public static String digestHexStringBySHA3_512_224(byte[] dataToHash, byte[] salt) throws NoSuchAlgorithmException {
return digestHexString(SHA3_512_224, dataToHash, salt);
}
public static String digestHexStringBySHA3_512_224(byte[] dataToHash) throws NoSuchAlgorithmException {
return digestHexString(SHA3_512_224, dataToHash, null);
}
public static String digestHexStringBySHA3_512_256(byte[] dataToHash, byte[] salt) throws NoSuchAlgorithmException {
return digestHexString(SHA3_512_256, dataToHash, salt);
}
public static String digestHexStringBySHA3_512_256(byte[] dataToHash) throws NoSuchAlgorithmException {
return digestHexString(SHA3_512_256, dataToHash, null);
}
public static String digestHexStringBySM3(byte[] dataToHash) throws NoSuchAlgorithmException {
return digestHexString(SM3, dataToHash, null);
}
public static String digestHexStringBySM3(byte[] dataToHash, byte[] salt) throws NoSuchAlgorithmException {
return digestHexString(SM3, dataToHash, salt);
}
public static byte[] digestBytesByMD5(byte[] dataToHash, byte[] salt) throws NoSuchAlgorithmException {
return digestBytes(MD5, dataToHash, salt);
}
public static byte[] digestBytesByMD5(byte[] dataToHash) throws NoSuchAlgorithmException {
return digestBytes(MD5, dataToHash, null);
}
public static byte[] digestBytesBySHA1(byte[] dataToHash, byte[] salt) throws NoSuchAlgorithmException {
return digestBytes(SHA_1, dataToHash, salt);
}
public static byte[] digestBytesBySHA1(byte[] dataToHash) throws NoSuchAlgorithmException {
return digestBytes(SHA_1, dataToHash, null);
}
public static byte[] digestBytesBySHA224(byte[] dataToHash, byte[] salt) throws NoSuchAlgorithmException {
return digestBytes(SHA_224, dataToHash, salt);
}
public static byte[] digestBytesBySHA224(byte[] dataToHash) throws NoSuchAlgorithmException {
return digestBytes(SHA_224, dataToHash, null);
}
public static byte[] digestBytesBySHA256(byte[] dataToHash, byte[] salt) throws NoSuchAlgorithmException {
return digestBytes(SHA_256, dataToHash, salt);
}
public static byte[] digestBytesBySHA256(byte[] dataToHash) throws NoSuchAlgorithmException {
return digestBytes(SHA_256, dataToHash, null);
}
public static byte[] digestBytesBySHA384(byte[] dataToHash, byte[] salt) throws NoSuchAlgorithmException {
return digestBytes(SHA_384, dataToHash, salt);
}
public static byte[] digestBytesBySHA384(byte[] dataToHash) throws NoSuchAlgorithmException {
return digestBytes(SHA_384, dataToHash, null);
}
public static byte[] digestBytesBySHA512(byte[] dataToHash, byte[] salt) throws NoSuchAlgorithmException {
return digestBytes(SHA_512, dataToHash, salt);
}
public static byte[] digestBytesBySHA512(byte[] dataToHash) throws NoSuchAlgorithmException {
return digestBytes(SHA_512, dataToHash, null);
}
public static byte[] digestBytesBySHA3_224(byte[] dataToHash, byte[] salt) throws NoSuchAlgorithmException {
return digestBytes(SHA3_224, dataToHash, salt);
}
public static byte[] digestBytesBySHA3_224(byte[] dataToHash) throws NoSuchAlgorithmException {
return digestBytes(SHA3_224, dataToHash, null);
}
public static byte[] digestBytesBySHA3_256(byte[] dataToHash, byte[] salt) throws NoSuchAlgorithmException {
return digestBytes(SHA3_256, dataToHash, salt);
}
public static byte[] digestBytesBySHA3_256(byte[] dataToHash) throws NoSuchAlgorithmException {
return digestBytes(SHA3_256, dataToHash, null);
}
public static byte[] digestBytesBySHA3_384(byte[] dataToHash, byte[] salt) throws NoSuchAlgorithmException {
return digestBytes(SHA3_384, dataToHash, salt);
}
public static byte[] digestBytesBySHA3_384(byte[] dataToHash) throws NoSuchAlgorithmException {
return digestBytes(SHA3_384, dataToHash, null);
}
public static byte[] digestBytesBySHA3_512(byte[] dataToHash, byte[] salt) throws NoSuchAlgorithmException {
return digestBytes(SHA3_512, dataToHash, salt);
}
public static byte[] digestBytesBySHA3_512(byte[] dataToHash) throws NoSuchAlgorithmException {
return digestBytes(SHA3_512, dataToHash, null);
}
public static byte[] digestBytesBySHA3_512_224(byte[] dataToHash, byte[] salt) throws NoSuchAlgorithmException {
return digestBytes(SHA3_512_224, dataToHash, salt);
}
public static byte[] digestBytesBySHA3_512_224(byte[] dataToHash) throws NoSuchAlgorithmException {
return digestBytes(SHA3_512_224, dataToHash, null);
}
public static byte[] digestBytesBySHA3_512_256(byte[] dataToHash, byte[] salt) throws NoSuchAlgorithmException {
return digestBytes(SHA3_512_256, dataToHash, salt);
}
public static byte[] digestBytesBySHA3_512_256(byte[] dataToHash) throws NoSuchAlgorithmException {
return digestBytes(SHA3_512_256, dataToHash, null);
}
public static byte[] digestBytesBySM3(byte[] dataToHash) throws NoSuchAlgorithmException {
return digestBytes(SM3, dataToHash, null);
}
public static byte[] digestBytesBySM3(byte[] dataToHash, byte[] salt) throws NoSuchAlgorithmException {
return digestBytes(SM3, dataToHash, salt);
}
}
|