Current Location: Home> Latest Articles> How to choose the appropriate algorithm and salt value format for crypt()

How to choose the appropriate algorithm and salt value format for crypt()

M66 2025-05-20

In PHP, the crypt() function is a traditional method for encrypting passwords. It supports multiple encryption algorithms and enables specific encryption mechanisms by passing in salt values ​​in different formats. Although more modern password hashing APIs (such as password_hash() ) have been recommended in recent years, crypt() is still widely used in some old systems. Therefore, understanding how to choose the appropriate algorithm and salt value format for the crypt() function is the key to ensuring security and system compatibility.

1. Supported encryption algorithms and salt value formats

The crypt() function uses the algorithm used to identify the salt value prefix. The following are mainstream algorithms and their corresponding salt value formats:

  • DES (traditional algorithm)

    • Salt value format: 2 characters (for example: "xy")

    • Safety: Very low, no longer recommended.

  • MD5

    • Salt value format: $1$ + up to 8 characters (for example: $1$abc12345 )

    • Security: Weak, easy to be brute-forced.

  • Blowfish

    • Salt value format: $2y$ + 2-bit cost factor + 22-bit base64 encoding (for example: $2y$10$abcdefghijklmnopqrstuu )

    • Safety: High, widely used.

    • Compatibility: It is recommended to use $2y$ instead of $2a$ because $2y$ fixes historical security issues.

  • SHA-256 and SHA-512 (glibc)

    • Salt value format:

      • SHA-256: $5$ + optional rounds parameter + salt (for example: $5$rounds=5000$mysaltvalue )

      • SHA-512: $6$ + optional rounds parameter + salt (for example: $6$mysaltvalue )

    • Safety: High

    • Compatibility: Available on platforms supported by glibc (such as most Linux systems), but not Windows.

2. How to choose the right algorithm

When choosing an encryption algorithm, the following points should be considered:

  • Security requirements : If the system needs to resist brute force cracking from modern hardware, it is recommended to use Blowfish ( $2y$ ) or SHA-512 ( $6$ ).

  • Cross-platform compatibility : If the system needs to be migrated between Linux and Windows, the Blowfish algorithm should be used first, because the SHA variant is incompatible on Windows.

  • Calculation cost control : Blowfish supports cost parameters (2–31) and can be adjusted according to server performance. For example, $2y$12$ means 2^12 iterations.

3. Best practices for salt value generation

The salt value is used to prevent the same password from producing the same hash, so the following characteristics must be:

  • Uniqueness : Each user's password should use a different salt.

  • Unpredictability : Salt should be generated using a safe pseudo-random source.

Sample code, using Blowfish to generate a hash with salt:

 function generateSalt($cost = 10) {
    $salt = substr(strtr(base64_encode(random_bytes(16)), '+', '.'), 0, 22);
    return sprintf('$2y$%02d$%s$', $cost, $salt);
}

$password = 'MySecurePassword123';
$salt = generateSalt(12);
$hash = crypt($password, $salt);

echo "The encrypted password is: " . $hash;

4. Methods to verify password

When using crypt() for password verification, the user input should be passed to crypt() along with the original hash, such as:

 function verifyPassword($password, $hash) {
    return crypt($password, $hash) === $hash;
}

This ensures that the verification process uses the same algorithm and salt values.

5. Precautions and recommendations

  • Do not manually specify strings with unknown salt values , use the safe salt values ​​automatically generated by the program.

  • Avoid using DES or MD5 even if they are still supported.

  • Preferential migration to password_hash() and password_verify() , these functions are maintained by PHP and automatically handle algorithms and salt values, which can better resist modern attacks.

6. Sample application scenarios

An old system saves password when a user registers:

 $password = $_POST['password'];
$salt = generateSalt(12);
$hashed = crypt($password, $salt);
// keep $hashed Go to the database

Verify password when logging in:

 $input = $_POST['password'];
$stored_hash = getPasswordFromDatabase(); // Assume that it has been retrieved from the database

if (verifyPassword($input, $stored_hash)) {
    echo "Login successfully";
} else {
    echo "Error password";
}

7. Summary

Although the crypt() function is no longer the preferred tool in modern PHP, it is still important to understand its algorithms and salt value mechanisms, especially when maintaining old systems. It is recommended to use the Blowfish ( $2y$ ) algorithm with safely generated salt values ​​to ensure a balance between compatibility and security. It is also recommended to transition the system to password_hash() in the long run, a more modern and secure solution.