Current Location: Home> Latest Articles> crypt() performance comparison: Blowfish vs SHA-512

crypt() performance comparison: Blowfish vs SHA-512

M66 2025-06-04

In PHP, the crypt() function is an important tool for hash encryption, which supports a variety of encryption algorithms, including Blowfish (usually expressed as $2y$ ) and SHA-512 (used as $6$ ). Both algorithms are widely used for password hashing and verification, but they differ significantly in performance and applicable scenarios. This article will discuss the performance comparison of Blowfish and SHA-512 in the PHP crypt() function in detail, and analyze its advantages and disadvantages based on examples.

1. Blowfish algorithm ($2y$)

Blowfish is a symmetric key-based encryption algorithm, used in PHP as one of the implementations of bcrypt, to generate strong hash values. bcrypt is designed for password hashing, with a default cost factor of 10, which means that the calculation process will be deliberately delayed to prevent brute-force cracking.

advantage:

  • Designed for passwords : bcrypt's design goal is password hashing, which can resist rainbow tables and brute force cracking.

  • Adjustable cost factor : By increasing cost parameters, the hashing difficulty can be enhanced.

  • Stable performance : In most cases, execution time is relatively stable, which helps prevent time attacks.

Sample code:

 $password = 'mypassword';
$hash = crypt($password, '$2y$10$usesomesillystringforsalt$');
echo $hash;

2. SHA-512 algorithm ($6$)

SHA-512 is a member of the SHA-2 family and is a more general hash function, mainly used for data integrity verification, rather than specifically designed for password hashing. It is also supported in PHP's crypt() , but its security depends on the salt value and application context.

advantage:

  • Fast speed : SHA-512 is faster to calculate than Blowfish, and is suitable for fast hashing of non-password data.

  • Algorithm standardization : It is a standard hashing algorithm recommended by NIST and is widely used in file checksum signatures.

shortcoming:

  • Lack of cost factor control : Unlike Blowfish, SHA-512 cannot easily adjust the calculated cost.

  • More susceptible to brute-force attacks : Because of the fast computing speed, it becomes a potential security risk in password hashing.

Sample code:

 $password = 'mypassword';
$hash = crypt($password, '$6$rounds=5000$usesomesillystringforsalt$');
echo $hash;

3. Performance comparison test

To evaluate the performance differences between Blowfish and SHA-512, we can perform the following tests in the same server environment:

 $start = microtime(true);
crypt('mypassword', '$2y$10$usesomesillystringforsalt$');
$blowfish_time = microtime(true) - $start;

$start = microtime(true);
crypt('mypassword', '$6$rounds=5000$usesomesillystringforsalt$');
$sha512_time = microtime(true) - $start;

echo "Blowfishtime:{$blowfish_time} Second\n";
echo "SHA-512time:{$sha512_time} Second\n";

In general PHP operation environment, the execution time of Blowfish is usually several times that of SHA-512. While this "slowness" is designed (for enhanced security), it can have a performance impact when handling a large number of requests.

4. How to choose?

  • If you are developing a user login system, it is recommended to use Blowfish (bcrypt) because it is designed to be more secure and has brute-force resistance.

  • If you only need a fast hash, such as unique identification generation for non-sensitive data, you can use SHA-512.

It is worth noting that starting from PHP 5.5, it is recommended to use password_hash() and password_verify() instead of crypt() , so that password hash logic can be managed more easily and securely.

 $hash = password_hash('mypassword', PASSWORD_BCRYPT);
if (password_verify('mypassword', $hash)) {
    echo 'Correct password';
}

5. Summary

Although the crypt() function is still supported and used for password hashing, the choice between Blowfish and SHA-512 should be based on the specific application scenario. Blowfish provides stronger password security, while SHA-512 is the best in terms of speed. In scenarios where password security is needed, Blowfish should be preferred and used in combination with modern PHP password processing functions to ensure long-term security and maintenance of the system.

For developers who use password hashing in web authentication systems, please refer to the following URL for more best practices:

 https://m66.net/security/password-hashing