-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHashUtil.java
More file actions
119 lines (97 loc) · 3.68 KB
/
Copy pathHashUtil.java
File metadata and controls
119 lines (97 loc) · 3.68 KB
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
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;
import java.util.Base64;
import java.util.stream.Collectors;
/**
* @author Jason/GeW
*/
public class HashUtil {
enum Algorithm {
MD5("MD5"),
SHA1("SHA1"),
SHA256("SHA256"),
SHA384("SHA384"),
BCrypt("Bcrypt");
private final String name;
Algorithm(String a) {
name = a;
}
public String toString() {
return this.name;
}
}
private static final int BCRYPT_HASH_ROUND = 10;
private HashUtil() {
// Static Class
}
private static void checkContent(final String... content) {
if (content == null || content.length == 0) {
throw new IllegalArgumentException("Invalid Content");
}
}
private static Algorithm convert(final String algorithm) {
if (Algorithm.MD5.toString().equalsIgnoreCase(algorithm) || algorithm.toUpperCase().equals("MD-5")) {
return Algorithm.MD5;
} else if (Algorithm.SHA1.toString().equalsIgnoreCase(algorithm) || algorithm.toUpperCase().equals("SHA-1")) {
return Algorithm.SHA1;
} else if (Algorithm.SHA256.toString().equalsIgnoreCase(algorithm) || algorithm.toUpperCase().equals("SHA-256")) {
return Algorithm.SHA256;
} else if (Algorithm.SHA384.toString().equalsIgnoreCase(algorithm) || algorithm.toUpperCase().equals("SHA-384")) {
return Algorithm.SHA384;
} else if (Algorithm.BCrypt.toString().equalsIgnoreCase(algorithm)) {
return Algorithm.BCrypt;
} else {
throw new IllegalArgumentException("Algorithm Not In Provide");
}
}
public static String secureHash(final String algorithm, String... data) {
switch (convert(algorithm)) {
case MD5:
return md5(data);
case SHA1:
return sha1(data);
case SHA256:
return sha256(data);
case SHA384:
return sha384(data);
case BCrypt:
return bCrypt(data);
default:
return sha1(data);
}
}
public static byte[] digest(final String algorithm, final String... content) {
checkContent(content);
StringBuilder sb = new StringBuilder();
Arrays.stream(content).filter(s -> s != null && s.length() > 0).forEach(sb::append);
try {
MessageDigest md = MessageDigest.getInstance(algorithm);
md.update(sb.toString().getBytes(StandardCharsets.UTF_8));
return md.digest();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
return sb.toString().getBytes();
}
}
public static String md5(final String... content) {
return Base64.getEncoder().encodeToString(digest("MD5", content));
}
public static String sha1(final String... content) {
return Base64.getEncoder().encodeToString(digest("SHA-1", content));
}
public static String sha256(final String... content) {
return Base64.getEncoder().encodeToString(digest("SHA-256", content));
}
public static String sha384(final String... content) {
return Base64.getEncoder().encodeToString(digest("SHA-384", content));
}
public static String bCrypt(final String... content) {
checkContent(content);
String raw = Arrays.stream(content)
.filter(s -> s != null && s.length() > 0)
.collect(Collectors.joining());
return BCrypt.hashpw(raw, BCrypt.gensalt(BCRYPT_HASH_ROUND));
}
}