Advertisement
Guest User

Untitled

a guest
Nov 24th, 2016
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.23 KB | None | 0 0
  1. public class Security {
  2.  
  3. public static void main(String[] args) throws UserNotExistingException {
  4. Security s=new Security();
  5. s.signUp("John.Smith", "John Smith", "text@lau.edu", "test");
  6. System.out.println(s.Authenticate("John.Smith" , "test"));
  7. }
  8.  
  9. public boolean Authenticate(String username, String password) throws UserNotExistingException {
  10. String dbpass = null;
  11. byte[] salt = null;
  12. try {
  13. // Load driver for connecting to db
  14. Class.forName("com.mysql.jdbc.Driver");
  15. // Establishing connection to db
  16. Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/vote sys", "root", "");
  17. // Creating statement object to be executed on dbms
  18. Statement stmt = con.createStatement();
  19. ResultSet rs = stmt.executeQuery("select pass, salt from user_acc where username = '" + username + "';");
  20.  
  21. if (rs.next()) {
  22. dbpass = rs.getString(2);
  23. String temp = rs.getString(2);
  24. System.out.println(temp);
  25. salt = temp.getBytes();
  26. }
  27. for (byte i : salt)
  28. System.out.print(i);
  29. System.out.println();
  30. // Terminating connection to db
  31. con.close();
  32. } catch (Exception e) {
  33. System.out.println(e);
  34. }
  35. if (dbpass == null || salt == null)
  36. throw new UserNotExistingException("User " + username + " doesn't exist");
  37.  
  38. try { //this is where im facing the problem, the condition is always returning true when its not
  39. String hashed=generateHash(password, salt);
  40. System.out.println(hashed);
  41. if (hashed.compareTo(dbpass)!=0)
  42. return false;
  43.  
  44.  
  45. } catch (NoSuchAlgorithmException | InvalidKeySpecException e) {
  46. e.printStackTrace();
  47. }
  48.  
  49. return true;
  50. }
  51.  
  52. private static String generateHash(String password, byte[] salt)
  53. throws NoSuchAlgorithmException, InvalidKeySpecException {
  54. int iterations = 1000;
  55. char[] chars = password.toCharArray();
  56.  
  57. PBEKeySpec spec = new PBEKeySpec(chars, salt, iterations, 64 * 8);
  58. SecretKeyFactory skf = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
  59. byte[] hash = skf.generateSecret(spec).getEncoded();
  60. return iterations + ":" + toHex(salt) + ":" + toHex(hash);
  61. }
  62.  
  63. private static byte[] getSalt() throws NoSuchAlgorithmException {
  64. SecureRandom sr = SecureRandom.getInstance("SHA1PRNG");
  65. byte[] salt = new byte[16];
  66. sr.nextBytes(salt);
  67. return salt;
  68. }
  69.  
  70. private static String toHex(byte[] array) throws NoSuchAlgorithmException {
  71. BigInteger bi = new BigInteger(1, array);
  72. String hex = bi.toString(16);
  73. int paddingLength = (array.length * 2) - hex.length();
  74. if (paddingLength > 0) {
  75. return String.format("%0" + paddingLength + "d", 0) + hex;
  76. } else {
  77. return hex;
  78. }
  79. }
  80.  
  81. public void signUp(String username, String name, String email, String password) {
  82. String dbuser = "", dbemail = "";
  83. try {
  84. // Load driver for connecting to db
  85. Class.forName("com.mysql.jdbc.Driver");
  86. // Establishing connection to db
  87. Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/vote sys", "root", "");
  88. // Creating statement object to be executed on dbms
  89. Statement stmt = con.createStatement();
  90. ResultSet rs = stmt.executeQuery("select username, email from user_acc where username = '" + username
  91. + "' or email = '" + email + "';");
  92.  
  93. if (rs.next()) {
  94. dbuser = rs.getString(2);
  95. dbemail = rs.getString(2);
  96. }
  97. if (!dbuser.equals("") || !dbemail.equals(""))
  98. throw new UserNotExistingException("Username or email already exists");
  99.  
  100. byte[] salt = getSalt();
  101. for (int i = 0; i < salt.length; i++) {
  102. System.out.print(salt[i]);
  103. }
  104. System.out.println();
  105. String temp= new String(salt);
  106. System.out.println(temp);
  107. String hashedPass = generateHash(password, salt);
  108. System.out.println(hashedPass);
  109. stmt.executeUpdate("INSERT INTO `user_acc`(`username`, `name`, `email`, `pass`, `salt`) VALUES ('"
  110. + username + "','" + name + "','" + email + "','" + hashedPass + "','" + temp + "');");
  111.  
  112. } catch (Exception e) {
  113. System.out.println(e);
  114. }
  115. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement