Advertisement
Trentv4

Untitled

Jul 24th, 2014
198
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.54 KB | None | 0 0
  1. import java.security.MessageDigest;
  2. import java.util.ArrayList;
  3. import java.util.Random;
  4.  
  5.  
  6. public class ExperimentMain
  7. {
  8.     static ArrayList<Account> accountsList = new ArrayList<Account>();
  9.    
  10.     public static void main(String[] args)
  11.     {
  12.         try
  13.         {
  14.             createAccount("trentv4", "password");
  15.             checkAccount("trentv4", "password");
  16.         }
  17.         catch(Exception e)
  18.         {
  19.             e.printStackTrace();
  20.         }
  21.     }
  22.    
  23.     public static void checkAccount(String user, String password)
  24.     {
  25.         for(int i = 0; i < accountsList.size(); i++)
  26.         {
  27.             if(accountsList.get(i).username.equals(user))
  28.             {
  29.                 Account act = accountsList.get(i);
  30.                 if(act.hash.equals(sha1(act.salt+password)))
  31.                 {
  32.                     System.out.println("Test");
  33.                 }
  34.             }
  35.         }
  36.     }
  37.    
  38.     public static void createAccount(String user, String password)
  39.     {
  40.         Account account;
  41.         String salt = "";
  42.         Random random = new Random();
  43.         for(int i = 0; i < 10; i++)
  44.         {
  45.             salt += random.nextInt(10);
  46.         }
  47.         account = new Account(user, sha1(salt+password), salt);
  48.         accountsList.add(account);
  49.     }
  50.    
  51.     static String sha1(String input)
  52.     {
  53.         try
  54.         {
  55.             MessageDigest mDigest = MessageDigest.getInstance("SHA1");
  56.             byte[] result = mDigest.digest(input.getBytes());
  57.             StringBuffer sb = new StringBuffer();
  58.             for (int i = 0; i < result.length; i++)
  59.             {
  60.                 sb.append(Integer.toString((result[i] & 0xff) + 0x100, 16).substring(1));
  61.             }
  62.              
  63.             return sb.toString();
  64.         }
  65.         catch(Exception e)
  66.         {
  67.             e.printStackTrace();
  68.             return "";
  69.         }
  70.     }
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement