Advertisement
Guest User

Untitled

a guest
Sep 23rd, 2014
182
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 11.85 KB | None | 0 0
  1.  private static String _password_itoa64() {
  2.         return "./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
  3.     }
  4.  
  5.     private static int password_get_count_log2(String setting) {
  6.         return _password_itoa64().indexOf(setting.charAt(3));
  7.     }
  8.  
  9.     private static byte[] sha512(String input) {
  10.         try {
  11.             return java.security.MessageDigest.getInstance("SHA-512").digest(input.getBytes("UTF-8"));
  12.         } catch (NoSuchAlgorithmException ex) {
  13.             ex.printStackTrace();
  14.         } catch (UnsupportedEncodingException e) {
  15.             e.printStackTrace();
  16.         }
  17.         return new byte[0];
  18.     }
  19.  
  20.     private static byte[] sha512(byte[] input) {
  21.         try {
  22.             return java.security.MessageDigest.getInstance("SHA-512").digest(input);
  23.         } catch (NoSuchAlgorithmException ex) {
  24.             ex.printStackTrace();
  25.         }
  26.         return new byte[0];
  27.     }
  28.  
  29.     /**
  30.      * Note: taken from the default Drupal 7 password algorithm
  31.      *
  32.      * @param candidate               the clear text password
  33.      * @param saltedEncryptedPassword the salted encrypted password string to check => NEEDS TO BE THE DEFAULT DRUPAL 7 PASSWORD HASH.
  34.      * @return true if the candidate matches, false otherwise.
  35.      */
  36.     public static boolean checkPassword(String candidate, String saltedEncryptedPassword) throws Exception {
  37.         if (candidate == null || saltedEncryptedPassword == null) {
  38.             return false;
  39.         }
  40.  
  41.         String hash = password_crypt(candidate, saltedEncryptedPassword);
  42.         System.out.println("Expected value = " + saltedEncryptedPassword);
  43.         System.out.println("Calced   value = " + hash);
  44.         System.out.println("Result Good?   = " + saltedEncryptedPassword.equalsIgnoreCase(hash));
  45.  
  46.  
  47.         return saltedEncryptedPassword.equalsIgnoreCase(hash);
  48.     }
  49.  
  50.     private static String password_crypt(String password, String passwordHash) throws Exception {
  51.         // The first 12 characters of an existing hash are its setting string.
  52.         passwordHash = passwordHash.substring(0, 12);
  53.         int count_log2 = password_get_count_log2(passwordHash);
  54.         String salt = passwordHash.substring(4, 12);
  55.         // Hashes must have an 8 character salt.
  56.         if (salt.length() != 8) {
  57.             return null;
  58.         }
  59.  
  60.         int count = 1 << count_log2;
  61.  
  62.  
  63.         byte[] hash;
  64.         try {
  65.             hash = sha512(salt.concat(password));
  66.  
  67.             do {
  68.                 hash = sha512(joinBytes(hash, password.getBytes("UTF-8")));
  69.             } while (--count > 0);
  70.         } catch (Exception e) {
  71.             System.out.println("error " + e.toString());
  72.             return null;
  73.         }
  74.  
  75.         String output = passwordHash + _password_base64_encode(hash, hash.length);
  76.         return (output.length() > 0) ? output.substring(0, DRUPAL_HASH_LENGTH) : null;
  77.     }
  78.  
  79.     private static byte[] joinBytes(byte[] a, byte[] b) {
  80.         byte[] combined = new byte[a.length + b.length];
  81.  
  82.         System.arraycopy(a, 0, combined, 0, a.length);
  83.         System.arraycopy(b, 0, combined, a.length, b.length);
  84.         return combined;
  85.     }
  86.  
  87.     private static String _password_base64_encode(byte[] input, int count) throws Exception {
  88.  
  89.         StringBuffer output = new StringBuffer();
  90.         int i = 0;
  91.         CharSequence itoa64 = _password_itoa64();
  92.         do {
  93.             long value = SignedByteToUnsignedLong(input[i++]);
  94.  
  95.             output.append(itoa64.charAt((int) value & 0x3f));
  96.             if (i < count) {
  97.                 value |= SignedByteToUnsignedLong(input[i]) << 8;
  98.             }
  99.             output.append(itoa64.charAt((int) (value >> 6) & 0x3f));
  100.             if (i++ >= count) {
  101.                 break;
  102.             }
  103.             if (i < count) {
  104.                 value |= SignedByteToUnsignedLong(input[i]) << 16;
  105.             }
  106.  
  107.             output.append(itoa64.charAt((int) (value >> 12) & 0x3f));
  108.             if (i++ >= count) {
  109.                 break;
  110.             }
  111.             output.append(itoa64.charAt((int) (value >> 18) & 0x3f));
  112.         } while (i < count);
  113.  
  114.         return output.toString();
  115.     }
  116.  
  117.     public static long SignedByteToUnsignedLong(byte b) {
  118.         return b & 0xFF;
  119.     }
  120.  
  121.     public String getUsername() {
  122.         return jTextField1.getText();
  123.     }
  124.     // End of variables declaration//GEN-END:variables
  125.  
  126.     private String encode64(byte[] src, int count) {
  127.         int i, value;
  128.         String output = "";
  129.         i = 0;
  130.  
  131.         if (src.length < count) {
  132.             byte[] t = new byte[count];
  133.             System.arraycopy(src, 0, t, 0, src.length);
  134.             Arrays.fill(t, src.length, count - 1, (byte) 0);
  135.         }
  136.  
  137.         do {
  138.             value = src[i] + (src[i] < 0 ? 256 : 0);
  139.             ++i;
  140.             output += itoa64.charAt(value & 63);
  141.             if (i < count) {
  142.                 value |= (src[i] + (src[i] < 0 ? 256 : 0)) << 8;
  143.             }
  144.             output += itoa64.charAt((value >> 6) & 63);
  145.             if (i++ >= count) {
  146.                 break;
  147.             }
  148.             if (i < count) {
  149.                 value |= (src[i] + (src[i] < 0 ? 256 : 0)) << 16;
  150.             }
  151.             output += itoa64.charAt((value >> 12) & 63);
  152.             if (i++ >= count) {
  153.                 break;
  154.             }
  155.             output += itoa64.charAt((value >> 18) & 63);
  156.         } while (i < count);
  157.         return output;
  158.     }
  159.  
  160.     private String cryptPrivate(String password, String setting) {
  161.         String output = "*0";
  162.         if (((setting.length() < 2) ? setting : setting.substring(0, 2)).equalsIgnoreCase(output)) {
  163.             output = "*1";
  164.         }
  165.         String id = (setting.length() < 3) ? setting : setting.substring(0, 3);
  166.         if (!(id.equals("$P$") || id.equals("$H$"))) {
  167.             return output;
  168.         }
  169.         int countLog2 = itoa64.indexOf(setting.charAt(3));
  170.         if (countLog2 < 7 || countLog2 > 30) {
  171.             return output;
  172.         }
  173.         int count = 1 << countLog2;
  174.         String salt = setting.substring(4, 4 + 8);
  175.         if (salt.length() != 8) {
  176.             return output;
  177.         }
  178.         MessageDigest md;
  179.         try {
  180.             md = MessageDigest.getInstance("MD5");
  181.         } catch (NoSuchAlgorithmException e) {
  182.             e.printStackTrace();
  183.             return output;
  184.         }
  185.         byte[] pass = stringToUtf8(password);
  186.         byte[] hash = md.digest(stringToUtf8(salt + password));
  187.         do {
  188.             byte[] t = new byte[hash.length + pass.length];
  189.             System.arraycopy(hash, 0, t, 0, hash.length);
  190.             System.arraycopy(pass, 0, t, hash.length, pass.length);
  191.             hash = md.digest(t);
  192.         } while (--count > 0);
  193.         output = setting.substring(0, 12);
  194.         output += encode64(hash, 16);
  195.         return output;
  196.     }
  197.  
  198.     private String gensaltPrivate(byte[] input) {
  199.         String output = "$P$";
  200.         output += itoa64.charAt(Math.min(this.iterationCountLog2 + 5, 30));
  201.         output += encode64(input, 6);
  202.         return output;
  203.     }
  204.  
  205.     private byte[] stringToUtf8(String string) {
  206.         try {
  207.             return string.getBytes("UTF-8");
  208.         } catch (UnsupportedEncodingException e) {
  209.             throw new UnsupportedOperationException("This system doesn't support UTF-8!", e);
  210.         }
  211.     }
  212.  
  213.     public String HashPassword(String password) {
  214.         byte random[] = new byte[6];
  215.         this.randomGen.nextBytes(random);
  216.         // Unportable hashes (Blowfish, EXT_DES) could be added here, but I won't do this.
  217.         String hash = cryptPrivate(password, gensaltPrivate(stringToUtf8(new String(random))));
  218.         if (hash.length() == 34) {
  219.             return hash;
  220.         }
  221.         return "*";
  222.     }
  223.  
  224.     public boolean CheckPassword(String password, String storedHash) {
  225.         String hash = cryptPrivate(password, storedHash);
  226.         MessageDigest md = null;
  227.         if (hash.startsWith("*")) {    // If not phpass, try some algorythms from unix crypt()
  228.             if (storedHash.startsWith("$S$D")) {
  229.                 try {
  230.                     md = MessageDigest.getInstance("SHA-512");
  231.                 } catch (NoSuchAlgorithmException e) {
  232.                     md = null;
  233.                 }
  234.             }
  235.             if (md == null && storedHash.startsWith("$5$")) {
  236.                 try {
  237.                     md = MessageDigest.getInstance("SHA-256");
  238.                 } catch (NoSuchAlgorithmException e) {
  239.                     md = null;
  240.                 }
  241.             }
  242.             if (md == null && storedHash.startsWith("$2")) {
  243.                // return BCrypt.checkpw(password, storedHash);
  244.             }
  245.             if (md == null && storedHash.startsWith("$1$")) {
  246.                 try {
  247.                     md = MessageDigest.getInstance("MD5");
  248.                 } catch (NoSuchAlgorithmException e) {
  249.                     md = null;
  250.                 }
  251.             }
  252.  
  253.  
  254.             // STD_DES and EXT_DES not supported yet.
  255.             if (md != null) {
  256.                 try {
  257.                     hash = new String(md.digest(password.getBytes("UTF-8")));
  258.                 } catch (UnsupportedEncodingException e) {
  259.                     e.printStackTrace();
  260.                 }
  261.             }
  262.         }
  263.  
  264.         return hash.equals(storedHash);
  265.     }
  266.  
  267.  
  268.  
  269.    public void doTheLogin(){
  270.         if (!getUsername().isEmpty()) {
  271.             if (accountExists(getUsername()) == true) {
  272.                 char[] passChars = jPasswordField1.getPassword();
  273.                 if (passChars != null) {
  274.                     userpasss = new String(passChars);
  275.  
  276.                     try {
  277.                         PreparedStatement statement = Server_db.getConn().prepareStatement("SELECT uPassword FROM Users WHERE uName = ?");
  278.                         statement.setString(1, getUsername());
  279.                         ResultSet rs = statement.executeQuery();
  280.                         while (rs.next()) {
  281.                             pass = rs.getString("uPassword");
  282.                             System.err.println("The password returned is: " + pass);
  283.                         }
  284.                     } catch (Exception userpass) {
  285.                         System.err.println("sucks");
  286.                     }
  287.                     System.err.println("userpasss: " + userpasss);
  288.                     System.err.println("pass: " + pass);
  289.                     try {
  290.                         System.err.println("Pass crypt: " + password_crypt(userpasss, pass));
  291.                     } catch (Exception e) {
  292.                         e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
  293.                     }
  294.  
  295.                     try {
  296.                         if (password_crypt(userpasss, pass).equals(pass)) {
  297.  
  298.                             finaluser = jTextField1.getText();
  299.  
  300.                             JFrame lobby = new NewLobby(jTextField1.getText(), password_crypt(userpasss, pass), LoginWindow.this);
  301.  
  302.                             centerTheGUI(lobby);
  303.  
  304.  
  305.                             lobby.setVisible(true);
  306.                             LoginWindow.this.setVisible(false);
  307.  
  308.                         } else {
  309.                             JOptionPane.showMessageDialog(null, "Wrong username/password or the user is not registered.");
  310.  
  311.                         }
  312.                     } catch (Exception sad) {
  313.                         sad.printStackTrace();
  314.                     }
  315.  
  316.  
  317.                 }
  318.             } else {
  319.                 JOptionPane.showMessageDialog(null, "Wrong username/password or the user is not registered. or banned");
  320.             }
  321.         } else {
  322.             JOptionPane.showMessageDialog(null, "Please type down the username.");
  323.         }
  324.     }
  325.  
  326.  
  327.  
  328.  
  329.  
  330.  
  331. The password returned is: 57bcc508a591081df4ea8c1f04fbb6d9
  332. Pass crypt: 57bcc508a5910Q9TElm285bfXWnBboXf8R0XnzRGRgreb7k.E86scRI
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement