/*
 * Created on 06.04.2008
 */
package org.languagetool;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class PasswordTools {
  
  private static final String SALT_DELIMITER = "-";

  private PasswordTools() {
    // static tools, no public constructor
  }
  
  public static String hash(String password) {
    String randomSalt = Math.random() + "";
    randomSalt += "." + Math.random() + "";
    return hash(password, randomSalt);
  }
  
  public static String hash(String password, String salt) {
    if (password == null || password.equals("")) {
      throw new IllegalArgumentException("password may not be null or empty");
    }
    String hash = salt + SALT_DELIMITER + 
      hexMD5(salt + SALT_DELIMITER + password);
    return hash;
  }

  public static boolean checkPassword(String userPassword, String hashPassword) {
    String parts[] = hashPassword.split(SALT_DELIMITER);
    if (parts.length != 2) {
      throw new IllegalArgumentException("Invalid password format, missing salt delimiter: " + userPassword);
    }
    String salt = parts[0];
    String encPassword = hash(userPassword, salt);
    if (hashPassword.equals(encPassword)) {
      return true;
    }
    return false;
  }
  
  public static String hexMD5(String s) {
    try {
      MessageDigest md = MessageDigest.getInstance("MD5");
      return binaryToHex(md.digest(s.getBytes()));
    } catch (NoSuchAlgorithmException e) {
      throw new RuntimeException(e);
    }
  }

  public static String binaryToHex(byte[] array) {
    StringBuffer sb = new StringBuffer();
    for (int i = 0; i < array.length; ++i) {
        sb.append(Integer.toHexString((array[i] & 0xFF) | 0x100).substring(1,3));
    }
    return sb.toString();
  }  

}

@@ -140,217 +140,562 @@
 n;%0A%0A
-public class PasswordTools %7B%0A  %0A  private static final String SALT_DELIMITER = %22-%22;%0A%0A  private PasswordTools() %7B%0A    // static tools, no public constructor%0A  %7D%0A  %0A  public static String hash(String password) %7B
+/**%0A * Utility methods for hashing passwords.%0A * %0A * @author Daniel Naber%0A */%0Apublic class PasswordTools %7B%0A  %0A  /**%0A   * Delimiter char between plain text salt and hashed password.%0A   */%0A  private static final String SALT_DELIMITER = %22-%22;%0A%0A  private PasswordTools() %7B%0A    // static tools, no public constructor%0A  %7D%0A  %0A  /**%0A   * Calculate a hash of the given string and prepend a%0A   * random salt.%0A   */%0A  public static String hash(String password) %7B%0A    // the salt hampers the use of attacks that use %0A    // pre-computed hash values (%22rainbow table%22):
 %0A   
@@ -818,26 +818,101 @@
 mSalt);%0A  %7D%0A
+%0A
   
+/**%0A   * Calculate a hash of the given string, using the given salt.%0A   */
 %0A  public st
@@ -1205,24 +1205,354 @@
  hash;%0A  %7D%0A%0A
+  /**%0A   * Checks of a user-provided password is the same as the stored%0A   * password (actually checks if the hashes of both are the same).%0A   * %0A   * @param userPassword the plain test password%0A   * @param hashPassword the hashed password with a prepended salt%0A   * @return true if the password matches the hashed password%0A   */%0A
   public sta
@@ -1996,16 +1996,85 @@
 %0A  %7D%0A  %0A
+  /**%0A   * Returns a hex-encoded MD5 sum of the givens string.%0A   */%0A
   public
@@ -2320,22 +2320,96 @@
 %0A  %7D%0A%0A  
-public
+/**%0A   * Returns a hex representation of the given binary input.%0A   */%0A  private
  static 

add javadoc