java_passwords/hash.java

38 lines
1.2 KiB
Java
Raw Normal View History

2023-10-23 18:01:19 +11:00
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
public class hash {
2023-10-25 13:48:43 +11:00
public static void main(String[] args) throws IOException, NoSuchAlgorithmException {
2023-10-23 18:01:19 +11:00
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter a string to hash: ");
String input = reader.readLine();
2023-10-25 13:48:43 +11:00
String stored = hashstring(input);
2023-10-23 18:01:19 +11:00
2023-10-25 13:48:43 +11:00
System.out.println("Stored as:");
System.out.println(stored);
System.out.println("Check password:");
String check = reader.readLine();
if (hashstring(check).equals(stored)) {
System.out.println("Match!");
} else {
System.out.println("No match!");
2023-10-23 18:01:19 +11:00
}
2023-10-25 13:48:43 +11:00
2023-10-23 18:01:19 +11:00
}
public static String hashstring(String input) throws NoSuchAlgorithmException
{
// Create MessageDigest instance for SHA-256
MessageDigest md = MessageDigest.getInstance("SHA-256");
// Hash password and convert to hex string
byte[] hash = md.digest(input.getBytes(StandardCharsets.UTF_8));
return bytetohex.toHexString(hash);
}
}