-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSecurityUtil.java
More file actions
58 lines (52 loc) · 1.89 KB
/
Copy pathSecurityUtil.java
File metadata and controls
58 lines (52 loc) · 1.89 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
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.PBEKeySpec;
import java.security.MessageDigest;
import java.security.SecureRandom;
public class SecurityUtil {
private static final int ITERATIONS = 10000;
private static final int KEY_LENGTH = 256;
public static String generateSalt() {
SecureRandom random = new SecureRandom();
byte[] salt = new byte[16];
random.nextBytes(salt);
return bytesToHex(salt);
}
public static String hashPassword(String password, String salt) {
try {
PBEKeySpec spec = new PBEKeySpec(
password.toCharArray(),
hexToBytes(salt),
ITERATIONS,
KEY_LENGTH
);
SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA256");
byte[] hash = factory.generateSecret(spec).getEncoded();
return bytesToHex(hash);
} catch (Exception e) {
throw new RuntimeException("Error hashing password", e);
}
}
public static boolean verifyPassword(String password, String salt, String expectedHash) {
String actualHash = hashPassword(password, salt);
return MessageDigest.isEqual(
actualHash.getBytes(),
expectedHash.getBytes()
);
}
private static String bytesToHex(byte[] bytes) {
StringBuilder sb = new StringBuilder();
for (byte b : bytes) {
sb.append(String.format("%02x", b));
}
return sb.toString();
}
private static byte[] hexToBytes(String hex) {
int len = hex.length();
byte[] data = new byte[len / 2];
for (int i = 0; i < len; i += 2) {
data[i / 2] = (byte) ((Character.digit(hex.charAt(i), 16) << 4)
+ Character.digit(hex.charAt(i+1), 16));
}
return data;
}
}