-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHash.java
More file actions
35 lines (28 loc) · 1.14 KB
/
Hash.java
File metadata and controls
35 lines (28 loc) · 1.14 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
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Scanner;
public class Hash {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// Get user input
System.out.println("Enter text to hash:");
String input = scanner.nextLine();
try {
// Get SHA-256 MessageDigest instance
MessageDigest messageDigest = MessageDigest.getInstance("SHA-256");
// Convert input string to byte array
byte[] inputBytes = input.getBytes();
// Compute the hash
byte[] hashBytes = messageDigest.digest(inputBytes);
// Convert hash bytes to hexadecimal format
StringBuilder hashHex = new StringBuilder();
for (byte b : hashBytes) {
hashHex.append(String.format("%02x", b));
}
// Print the hash
System.out.println("Hashed value (SHA-256): " + hashHex.toString());
} catch (NoSuchAlgorithmException e) {
System.err.println("Error: SHA-256 algorithm not found.");
}
}
}