-
Notifications
You must be signed in to change notification settings - Fork 0
/
FileHashing.java
28 lines (25 loc) · 927 Bytes
/
FileHashing.java
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
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
public class FileHashing {
public static void main(String[] args)throws Exception {
System.out.println(hashString("WordFactorial.java","SHA-256"));
}
public static String hashString(String s,String hashType) throws NoSuchAlgorithmException {
byte[] hash = null;
try {
MessageDigest md = MessageDigest.getInstance(hashType);
hash = md.digest(s.getBytes());
} catch (NoSuchAlgorithmException e) { e.printStackTrace(); }
StringBuilder sb = new StringBuilder();
for (int i = 0; i < hash.length; ++i) {
String hex = Integer.toHexString(hash[i]);
if (hex.length() == 1) {
sb.append(0);
sb.append(hex.charAt(hex.length() - 1));
} else {
sb.append(hex.substring(hex.length() - 2));
}
}
return sb.toString();
}
}