Password Hashing Algorithms

Overview

FusionAuth provides several options for password hashing schemes.

An ideal password hashing algorithm should be slow. When hashing is fast and password entropy is low, brute force attacks become a high risk. Even with enforced password policies, low password entropy can be difficult to mitigate. Choosing a slow algorithm is actually preferred for password hashing. Of the hashing schemes provided, only PBKDF2 and Bcrypt are designed to be slow which makes them the best choice for password hashing, MD5 and SHA-256 were designed to be fast and as such this makes them a less than ideal choice.

When selecting a load factor for the hashing scheme, the minimum values are provided for a starting guideline only. The factor should be set as high as possible such that the login times are still acceptable to your end users. Be cautious with setting the factor too high, especially with Bcrypt, setting the factor too high may cause login requests to take seconds or minutes to complete.

phpass MD5

Available Since Version 1.45.0

This is the phpass (pronounced “pH pass”) MD5 algorithm commonly used by PHP applications. This is weak hash that has been shown to be compromised and is vulnerable to brute force attacks. This hash is provided for migration purposes and it should be upgraded to a stronger hash as soon as possible.

The following is the string value used by the FusionAuth API to request this algorithm.

  • phpass-md5

The following pseudocode is provided to help you identify if this algorithm is likely to match your current hashing scheme such that it can be used during import.

// Given a plain text password, a base64 encoded salt and a factor
hash(password, salt, factor) {
  result = md5(join(bytes(salt), bytes(password)))

  count = 0
  while count < factor {
    count = count + 1
    temp = join(result, bytes(password))
    result = md5(temp)
  }

  // encode and return the first 16 bytes of the result
  result = result[:16]
  return String(base64Encode(result))
}

phpass SHA-512

Available Since Version 1.45.0

This is the phpass (pronounced “pH pass”) SHA-512 algorithm commonly used by PHP applications. SHA-512 is part of the SHA-2 set of cryptographic standards. SHA-512 is a general purpose hash and similar to MD5 it was designed to be fast which makes it a less than ideal choice for a password hashing. This hash is mainly provided for migration purposes or where login performance is very critical. If login performance has not become an issue a stronger scheme should be utilized.

The following is the string value used by the FusionAuth API to request this algorithm.

  • phppass-sha512

The following pseudocode is provided to help you identify if this algorithm is likely to match your current hashing scheme such that it can be used during import.

// Given a plain text password, a base64 encoded salt and a factor
hash(password, salt, factor) {
  result = sha512(join(bytes(salt), bytes(password)))

  count = 0
  while count < factor {
    count = count + 1
    temp = join(result, bytes(password))
    result = sha512(temp)
  }

  // Encode the result, and return the first 43 characters
  return String(base64Encode(result).sub(0, 43))
}

Salted MD5

MD5 is a general purpose hashing algorithm producing a 128-bit hash. This is weak hash that has been shown to be compromised and is vulnerable to brute force attacks. This hash is provided for migration purposes and it should be upgraded to a stronger hash as soon as possible. A recommended minimum factor for this hashing algorithm is 20,000.

The following is the string value used by the FusionAuth API to request this algorithm.

  • salted-md5

The following pseudocode is provided to help you identify if this algorithm is likely to match your current hashing scheme such that it can be used during import.

// Given a plain text password, a base64 encoded salt and a factor
hash(password, salt, factor) {
  result = join(bytes(password), base64Decode(bytes(salt))

  count = 0
  while count < factor {
    count = count + 1
    result = md5(result)
  }

  return result
}

Salted SHA-256

SHA-256 is part of the SHA-2 set of cryptographic standards. SHA-256 is a general purpose hash and similar to MD5 it was designed to be fast which makes it a less than ideal choice for a password hashing. This hash is mainly provided for migration purposes or where login performance is very critical. If login performance has not become an issue a stronger scheme should be utilized. A recommended minimum factor for this hashing algorithm is 20,000.

The following is the string value used by the FusionAuth API to request this algorithm.

  • salted-sha256

The following pseudocode is provided to help you identify if this algorithm is likely to match your current hashing scheme such that it can be used during import.

// Given a plain text password, a base64 encoded salt and a factor
hash(password, salt, factor) {
  result = join(bytes(password), base64Decode(bytes(salt))

  count = 0
  while count < factor {
    count = count + 1
    result = sha256(result)
  }

  return result
}

Salted HMAC SHA-256

HMAC SHA-256 is a hash based message authentication code. This scheme is still vulnerable to brute force attacks and like MD5 and SHA-256 is mainly provided for migration purposes or where login performance is very critical. If login performance has not become an issue a stronger scheme should be utilized. This scheme does not utilize a factor.

The following is the string value used by the FusionAuth API to request this algorithm.

  • salted-hmac-sha256

The following pseudocode is provided to help you identify if this algorithm is likely to match your current hashing scheme such that it can be used during import.

// Given a plain text password, a base64 encoded salt and a factor
hash(password, salt, factor) {
  key = {
     salt: base64Decode(bytes(salt)
     algorithm: "HmacSHA256"
  }

  hmac = Mac("HmacSHA256")
  result = hmac(bytes(password))

  return String(base64Encode(result))
}

Salted PBKDF2 HMAC SHA-256

PBKDF2 (Password-Based Key Derivation Function2) applies a hash-based message authentication code (HMAC) to the salted input and iterates based upon a factor to produce the hashed output. A recommended factor for this hashing algorithm is between 5,000 and 100,000 depending on the CPU performance of your system.

FusionAuth provides two implementations of this algorithm, one with a 256 bit derived key, and another with 512 bit key.

The following are the two string values used by the FusionAuth API to request this algorithm with the 256 bit and the 512 bit key algorithm respectively.

  • salted-pbkdf2-hmac-sha256
  • salted-pbkdf2-hmac-sha256-512

The following pseudocode is provided to help you identify if this algorithm is likely to match your current hashing scheme such that it can be used during import. The following example code shows a 256 key length, the pseudocode is the same for the 512 bit key.

// Given a plain text password, a base64 encoded salt and a factor
hash(password, salt, factor) {
  key = {
     password: password
     salt: base64Decode(bytes(salt)
     factor: factor
     keyLength: 256
  }

  secret = pbkdf2Sha256(key)
  return String(base64Encode(secret))
}

Salted PBKDF2 HMAC SHA-512

Available Since Version 1.43.0

PBKDF2 (Password-Based Key Derivation Function2) applies a hash-based message authentication code (HMAC) to the salted input and iterates based upon a factor to produce the hashed output. A recommended factor for this hashing algorithm is between 5,000 and 100,000 depending on the CPU performance of your system.

The following is the string value used by the FusionAuth API to request this algorithm with the 512 bit key algorithm.

  • salted-pbkdf2-hmac-sha512-512

The following pseudocode is provided to help you identify if this algorithm is likely to match your current hashing scheme such that it can be used during import.

// Given a plain text password, a base64 encoded salt and a factor
hash(password, salt, factor) {
  key = {
     password: password
     salt: base64Decode(bytes(salt)
     factor: factor
     keyLength: 512
  }

  secret = pbkdf2Sha512(key)
  return String(base64Encode(secret))
}

Salted Bcrypt

Bcrypt is a password hashing function based on the Blowfish cipher. A recommended factor for this hashing algorithm is between 8 and 14. Unlike the other mentioned hashing functions the factor for Bcrypt is not simply an iteration count. Bcrypt uses the factor as a work factor, the work factor will be calculated using the provided factor as power of 2. This means that the difference between a factor of 12 and 13 is 2x. For example 2^12 = 4096 and 2^13 = 8192.

The following is the string value used by the FusionAuth API to request this algorithm.

  • bcrypt

The following pseudocode is provided to help you identify if this algorithm is likely to match your current hashing scheme such that it can be used during import.

// Given a plain text password, a base64 encoded salt and a factor
hash(password, salt, factor) {
  // Note that bcrypt uses a less common base64 character set for encoding and decoding.
  // - The character set is: [./A-Za-z0-9]

  passwordBytes = bytes(password)
  saltBytes = base64Decode(bytes(salt))
  result = bcrypt(passwordBytes, saltBytes, factor, bcryptIV)

  resultLength = length(bcryptIV) * 4 - 1
  result = sub(result, 0, resultLength)
  return base64Encode(result)
}

Additional Schemes

If you require a different hashing scheme, you can build a password hashing plugin.

You may also want to review the community provided plugins repository. These are provided without any warranty of suitability but may prove useful.