-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsha.java
More file actions
26 lines (24 loc) · 943 Bytes
/
sha.java
File metadata and controls
26 lines (24 loc) · 943 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
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Scanner;
public class sha {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a string to hash: ");
String input = scanner.nextLine();
try {
MessageDigest digest = MessageDigest.getInstance("SHA-256");
byte[] hashBytes = digest.digest(input.getBytes());
StringBuilder hexString = new StringBuilder();
for (byte b : hashBytes) {
hexString.append(String.format("%02x", b));
}
System.out.println("SHA-256 Hash: " + hexString.toString());
} catch (NoSuchAlgorithmException e) {
System.err.println("Error: SHA-256 algorithm not found.");
e.printStackTrace();
} finally {
scanner.close();
}
}
}