-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHasher.java
More file actions
28 lines (24 loc) · 848 Bytes
/
Hasher.java
File metadata and controls
28 lines (24 loc) · 848 Bytes
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
package httpserver;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
public class Hasher {
public boolean matches(byte[] input, String hash) {
return getHash(input).equals(hash);
}
public String getHash(byte[] input) {
try {
MessageDigest messageDigest = MessageDigest.getInstance("SHA1");
byte[] hash = messageDigest.digest(input);
return bytesToHex(hash);
} catch (NoSuchAlgorithmException e) {
throw new RuntimeException(e);
}
}
private String bytesToHex(byte[] hash) {
StringBuilder stringBuilder = new StringBuilder();
for (byte b : hash) {
stringBuilder.append(Integer.toString((b & 0xff) + 0x100, 16).substring(1));
}
return stringBuilder.toString();
}
}