-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStringHash.java
More file actions
30 lines (25 loc) · 1.11 KB
/
StringHash.java
File metadata and controls
30 lines (25 loc) · 1.11 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
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
public class StringHash {
public static String generateHash(String input) throws NoSuchAlgorithmException {
MessageDigest digest = MessageDigest.getInstance("SHA-256");
byte[] hash = digest.digest(input.getBytes(StandardCharsets.UTF_8));
// Convert the byte array to a 10-character string
StringBuilder hashString = new StringBuilder();
for (int i = 0; i < 10 && i < hash.length; i++) {
String hex = Integer.toHexString(0xff & hash[i]);
if (hex.length() == 1) {
hashString.append('0');
}
hashString.append(hex.charAt(0));
}
return hashString.toString();
}
public static void main(String[] args) throws NoSuchAlgorithmException {
String originalString = "1";
String hashedString = generateHash(originalString);
System.out.println("Original String: " + originalString);
System.out.println("Hashed String (10 characters): " + hashedString);
}
}