Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.security.MessageDigest;
- import java.security.NoSuchAlgorithmException;
- import javax.swing.JOptionPane;
- public class hasher{
- public static String hashString(String password){
- MessageDigest md = null;
- try {
- md = MessageDigest.getInstance("SHA-256");
- } catch (NoSuchAlgorithmException e) {
- e.printStackTrace();
- }
- byte[] byteData = null;
- for(int i = 0; i <= 999; i++){
- md.update(password.getBytes());
- byteData = md.digest();
- }
- StringBuffer sb = new StringBuffer();
- for(int i = 0; i < byteData.length; i++){
- String hex = Integer.toHexString(0xff & byteData[i]);
- if(hex.length() == 1){
- sb.append('0');
- }
- sb.append(hex);
- }
- String finalHash = sb.toString();
- return finalHash;
- }
- public static void main(String args[]){
- fileReadWrite frw= new fileReadWrite();
- //Scanner scanner = new Scanner(System.in);
- int menuOption = 0;
- String tempUser;
- String tempPassword;
- String tempHash;
- boolean outcome;
- String localUsers[];
- String localHashes[];
- System.out.println("--SHA-256 Hasher---");
- System.out.println("===MAIN MENU===");
- System.out.println("1) Create new password hash");
- System.out.println("2) Compare existing password");
- System.out.println("3) Print hash database");
- System.out.println("4) Quit");
- System.out.println("Input: ");
- String SmenuOption = JOptionPane.showInputDialog(null, "Menu Choice..");
- menuOption = Integer.parseInt(SmenuOption);
- switch(menuOption){
- case 1:
- System.out.println("Enter a username for the password");
- tempUser = JOptionPane.showInputDialog(null, "Enter a username");
- System.out.println("Enter a password for the above username");
- tempPassword = JOptionPane.showInputDialog(null, "Enter a password");
- tempHash = hashString(tempPassword);
- outcome = frw.writeHash(tempUser, tempHash);
- if(outcome == true){
- System.out.println("Operation Successful");
- } else {
- System.out.println("Operation Failed!");
- }
- case 2:
- System.out.println("Enter a username..");
- tempUser = JOptionPane.showInputDialog(null, "Enter a username..");
- System.out.println("Enter the password for the username");
- tempPassword = JOptionPane.showInputDialog(null, "Enter a password");
- tempHash = hashString(tempPassword);
- frw.readHash();
- outcome = frw.compareHashes(tempUser, tempHash);
- if(outcome == true){
- System.out.println("Password for Username: " + tempUser + " matches!");
- } else {
- System.out.println("No match found! Username of password invalid");
- }
- break;
- case 3:
- //PRINT USERNAME AND HASH DATABASE
- localUsers = frw.getUsers();
- localHashes = frw.getHashes();
- System.out.println(localHashes[0]);
- for(int i = 0; i <= localUsers.length+1; i++){
- System.out.println("Username: " + localUsers[i] + "\nHash: " + localHashes[i] + "\n---------------------\n");
- }
- break;
- case 4:
- break;
- default:
- System.out.println("Invalid entry");
- break;
- }
- }
- }
Add Comment
Please, Sign In to add comment