Class PasswordHash


  • public class PasswordHash
    extends java.lang.Object
    PBKDF2 salted password hashing.
    Author:
    havoc AT defuse.ca, www: http://crackstation.net/hashing-security.htm
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static java.lang.String createHash​(byte[] password)
      Returns a salted PBKDF2 hash of the password.
      static java.lang.String createHash​(byte[] password, int saltSize, int iterations, int dkSize)
      Returns a salted PBKDF2 hash of the password.
      static java.lang.String createHash​(java.lang.String password)
      Returns a salted PBKDF2 hash of the password.
      static byte[] pbkdf2​(byte[] password, byte[] salt, int iterations, int bytes)
      Computes the PBKDF2 hash of a password.
      static boolean validatePassword​(byte[] password, java.lang.String correctHash)
      Validates a password using a hash.
      static boolean validatePassword​(java.lang.String password, java.lang.String correctHash)
      Validates a password using a hash.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Method Detail

      • createHash

        public static java.lang.String createHash​(java.lang.String password)
        Returns a salted PBKDF2 hash of the password.
        Parameters:
        password - - the password to hash
        Returns:
        a salted PBKDF2 hash of the password
      • createHash

        public static java.lang.String createHash​(byte[] password)
        Returns a salted PBKDF2 hash of the password.
        Parameters:
        password - - the password to hash
        Returns:
        a salted PBKDF2 hash of the password
      • createHash

        public static java.lang.String createHash​(byte[] password,
                                                  int saltSize,
                                                  int iterations,
                                                  int dkSize)
        Returns a salted PBKDF2 hash of the password.
        Parameters:
        password - - the password to hash
        saltSize - - the size of salt in bytes
        iterations - - the iteration count (slowness factor)
        dkSize - - the length of the derived key
        Returns:
        a salted PBKDF2 hash of the password
      • validatePassword

        public static boolean validatePassword​(java.lang.String password,
                                               java.lang.String correctHash)
        Validates a password using a hash.
        Parameters:
        password - - the password to check
        correctHash - - the hash of the valid password
        Returns:
        true if the password is correct, false if not
      • validatePassword

        public static boolean validatePassword​(byte[] password,
                                               java.lang.String correctHash)
        Validates a password using a hash.
        Parameters:
        password - - the password to check
        correctHash - - the hash of the valid password
        Returns:
        true if the password is correct, false if not
      • pbkdf2

        public static byte[] pbkdf2​(byte[] password,
                                    byte[] salt,
                                    int iterations,
                                    int bytes)
        Computes the PBKDF2 hash of a password.
        Parameters:
        password - - the password to hash.
        salt - - the salt
        iterations - - the iteration count (slowness factor)
        bytes - - the length of the hash to compute in bytes
        Returns:
        the PBDKF2 hash of the password