Guest User

Untitled

a guest
Sep 11th, 2018
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.09 KB | None | 0 0
  1. import java.security.MessageDigest;
  2. import java.security.NoSuchAlgorithmException;
  3.  
  4. import javax.swing.JOptionPane;
  5.  
  6. public class hasher{
  7.    
  8.     public static String hashString(String password){
  9.        
  10.         MessageDigest md = null;
  11.        
  12.         try {
  13.             md = MessageDigest.getInstance("SHA-256");
  14.         } catch (NoSuchAlgorithmException e) {
  15.             e.printStackTrace();
  16.         }
  17.        
  18.         byte[] byteData = null;
  19.        
  20.         for(int i = 0; i <= 999; i++){
  21.             md.update(password.getBytes());
  22.             byteData = md.digest();
  23.         }
  24.  
  25.         StringBuffer sb = new StringBuffer();
  26.  
  27.         for(int i = 0; i < byteData.length; i++){
  28.             String hex = Integer.toHexString(0xff & byteData[i]);
  29.             if(hex.length() == 1){
  30.                 sb.append('0');
  31.             }
  32.        
  33.             sb.append(hex);
  34.         }
  35.        
  36.         String finalHash = sb.toString();
  37.  
  38.         return finalHash;      
  39.     }
  40.    
  41.     public static void main(String args[]){
  42.  
  43.         fileReadWrite frw= new fileReadWrite();
  44.         //Scanner scanner = new Scanner(System.in);
  45.  
  46.         int menuOption = 0;
  47.         String tempUser;
  48.         String tempPassword;
  49.         String tempHash;
  50.         boolean outcome;
  51.  
  52.         String localUsers[];
  53.         String localHashes[];
  54.  
  55.         System.out.println("--SHA-256 Hasher---");
  56.         System.out.println("===MAIN MENU===");
  57.        
  58.         System.out.println("1) Create new password hash");
  59.         System.out.println("2) Compare existing password");
  60.         System.out.println("3) Print hash database");
  61.         System.out.println("4) Quit");
  62.    
  63.         System.out.println("Input: ");
  64.         String SmenuOption = JOptionPane.showInputDialog(null, "Menu Choice..");
  65.         menuOption = Integer.parseInt(SmenuOption);
  66.    
  67.  
  68.     switch(menuOption){
  69.         case 1:
  70.                 System.out.println("Enter a username for the password");
  71.                 tempUser = JOptionPane.showInputDialog(null, "Enter a username");
  72.                
  73.                 System.out.println("Enter a password for the above username");
  74.                 tempPassword = JOptionPane.showInputDialog(null, "Enter a password");
  75.                 tempHash = hashString(tempPassword);
  76.                 outcome = frw.writeHash(tempUser, tempHash);
  77.                 if(outcome == true){
  78.                     System.out.println("Operation Successful");
  79.                 } else {
  80.                     System.out.println("Operation Failed!");
  81.                 }
  82.  
  83.         case 2:
  84.                 System.out.println("Enter a username..");
  85.                 tempUser = JOptionPane.showInputDialog(null, "Enter a username..");
  86.                
  87.                 System.out.println("Enter the password for the username");
  88.                 tempPassword = JOptionPane.showInputDialog(null, "Enter a password");
  89.                 tempHash = hashString(tempPassword);
  90.                 frw.readHash();
  91.                 outcome = frw.compareHashes(tempUser, tempHash);
  92.                 if(outcome == true){
  93.                     System.out.println("Password for Username: " + tempUser + " matches!");
  94.                 } else {
  95.                     System.out.println("No match found! Username of password invalid");
  96.                 }
  97.  
  98.                 break;
  99.  
  100.         case 3:
  101.                 //PRINT USERNAME AND HASH DATABASE
  102.                 localUsers = frw.getUsers();
  103.                 localHashes = frw.getHashes();
  104.                 System.out.println(localHashes[0]);
  105.                 for(int i = 0; i <= localUsers.length+1; i++){
  106.                     System.out.println("Username: " + localUsers[i] + "\nHash: " + localHashes[i] + "\n---------------------\n");
  107.                 }
  108.                 break;
  109.    
  110.         case 4:
  111.                 break;
  112.  
  113.         default:
  114.                 System.out.println("Invalid entry");
  115.                 break;
  116.     }
  117.     }
  118. }
Add Comment
Please, Sign In to add comment