Ved ikke om du kan bruge det, men jeg sad og legede lidt med sådan noget en - to timers tid på et tidspunkt for sjov. Og blev ikke færdig med det.
Om du delvis kan bruge nogle af tingene skal jeg lade være op til dig.
- /*GUI*/
-
- private void jb_LoginActionPerformed(java.awt.event.ActionEvent evt) {
- String username_var = jtf_Username.getText();
- char password_array[] = jpf_Password.getPassword();
-
- /**
- * Create a empty string to compile the password from the
- * char array into
- */
- String compile_password = "";
-
- /**
- * Compile the password
- */
- StringBuilder sb = new StringBuilder();
- for(char b : password_array)
- {
- sb.append(b);
- }
-
- /**
- * Call the Password_diagest class to digest the password to hex
- */
- Password_diagest pd = new Password_diagest();
-
- /**
- * Get the returned hex hash
- */
- String password_hash = pd.digest_password(sb.toString());
-
- Dal1 d = new Dal1();
- try
- {
- /**
- * Create a sql connection
- */
- d.sqlConnection();
-
- /**
- * Check if input password hash and db password hash match.
- */
- boolean doesPasswordMatch = d.checkPassword(username_var, password_hash);
- if(doesPasswordMatch == true)
- {
- //JOptionPane.showMessageDialog(null, "Login accepted");
- GUI3 g3 = new GUI3();
- g3.show();
-
- this.hide();
- }
- else
- {
- JOptionPane.showMessageDialog(null, "Try again");
- }
- }
- catch(Exception e)
- {
- JOptionPane.showMessageDialog(null, e.fillInStackTrace());
- }
-
- }
- public boolean checkPassword(String username, String input_password) throws SQLException
- {
- ResultSet rs = null;
-
- CallableStatement proc = conn.prepareCall("{ call SP_GET_PASSWORD_HASH(?) }");
- proc.setString(1, username);
- rs = proc.executeQuery();
-
- String password_hash = "";
- while(rs.next())
- {
- password_hash = rs.getString(1);
- }
-
- if(input_password.equals(password_hash))
- {
- return true;
- }
-
- return false;
- }
- public class Password_diagest
- {
- public String digest_password(String password)
- {
- byte[] thedigest = new byte[0];
-
- try
- {
- byte[] bytesOfMessage = password.getBytes("UTF-8");
- MessageDigest password_md5 = MessageDigest.getInstance("MD5");
- thedigest = password_md5.digest(bytesOfMessage);
- }
-
- catch (NoSuchAlgorithmException ex)
- {
- Logger.getLogger(GUI2.class.getName()).log(Level.SEVERE, null, ex);
- }
- catch (UnsupportedEncodingException ex)
- {
- Logger.getLogger(GUI2.class.getName()).log(Level.SEVERE, null, ex);
- }
-
- /**
- * Compile hash into hex code
- */
- StringBuilder sb = new StringBuilder();
- for(byte b : thedigest)
- {
- sb.append(Integer.toHexString((int) (b & 0xff)));
- }
-
- String password_md5 = sb.toString();
-
- return password_md5;
- }
- }
MySql har også nogle indbyggede hash funktioner med sha1.
Lidt hygge læsning
http://www.palominodb.com/blog/2011/12/04/hashing-algorithm-mysql-passwordDog er MySql også den langsommeste (der er værd at snakke om)sequal database.
Dette ville jeg selv vælge at læse hvis jeg skulle arbejde videre på overstående.
https://crackstation.net/hashing-security.htmher tænker jeg information som "brugernavn" og "adgangskode"
Du burde også lave et unikt id til hver af dine brugere. F.eks. En int du automatisk tæller op for hvert oprettet en ny bruger.
Vh
Indlæg senest redigeret d. 30.09.2013 13:25 af Bruger #17719