-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDay10.java
More file actions
34 lines (28 loc) · 1.08 KB
/
Day10.java
File metadata and controls
34 lines (28 loc) · 1.08 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
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
public class Day10 {
public static String hashString(String input) {
try {
MessageDigest digest = MessageDigest.getInstance("SHA-256");
byte[] encodedHash = digest.digest(input.getBytes());
StringBuilder hexString = new StringBuilder();
for (byte b : encodedHash) {
String hex = Integer.toHexString(0xff & b);
if (hex.length() == 1) {
hexString.append('0');
}
hexString.append(hex);
}
return hexString.toString();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
return null;
}
}
public static void main(String[] args) {
String inputString = "Hello, Hashing!";
String hashedResult = hashString(inputString);
System.out.println("Original String: " + inputString);
System.out.println("Hashed Result: " + hashedResult);
}
}