crypt()
The crypt() function is used to one-way encrypt a string. The syntax of the crypt() function is as follows:
The PHP crypt() function performs one-way hashing of a string. "One-way" means you can turn a password into a hash, but you cannot turn the hash back into the original password — which is exactly what you want for storing passwords. This page covers the syntax, the salt formats that select each hashing algorithm, how to verify a stored hash, and why modern code should prefer password_hash() instead.
Syntax
crypt(string $string, string $salt = ""): stringThe function takes two parameters:
$string— the input text you want to hash.$salt— a string that selects the hashing algorithm and supplies random bytes that make identical passwords hash to different values. The salt is optional, but omitting it is unsafe (a warning is raised since PHP 8.0): always pass an explicit salt.
The return value is the hashed string. The first characters of the result encode the salt and algorithm, so the hash is self-describing — you can store it as-is and crypt() will know later how to reproduce it.
A basic example
Here we pass a string and a salt to crypt(), and it returns the hash. With the salt "ab", the output is the standard DES hash:
abJnggxhB/yWINotice the first two characters of the output (ab) are the salt itself — that is how crypt() remembers which algorithm and salt produced the hash.
Salt formats and algorithms
The shape of the salt tells crypt() which algorithm to use. This is the single most important thing to understand about the function:
| Salt prefix | Algorithm | Notes |
|---|---|---|
(2 chars, no $) | DES | Legacy, weak — only the first 8 characters of the password matter. Avoid. |
$1$ | MD5 | Old; do not use for new passwords. |
$5$ | SHA-256 | |
$6$ | SHA-512 | Strong; supports a rounds= cost. |
$2y$ | Blowfish (bcrypt) | The recommended algorithm for crypt(). Format: $2y$<cost>$<22-char salt>. |
A bcrypt example with an explicit cost of 10:
<?php
$hash = crypt("password123", '$2y$10$usesomesillystringforsalt$');
echo $hash;
?>The output begins with $2y$10$, recording the algorithm and cost so the hash can be verified later:
$2y$10$usesomesillystringforeSNzFqnuq1h/v0NITsGcb4b3qwzSfNIaVerifying a password
To check a password against a stored hash, hash the candidate using the stored hash as the salt. crypt() reads the algorithm and salt back out of that hash, so it reproduces the same parameters automatically:
Output:
Password is valid!Use hash_equals() rather than == for the comparison — it runs in constant time and avoids timing attacks that can leak information about the hash.
When to use it (and what to use instead)
crypt() is a low-level, legacy tool. It is easy to misuse: a too-short salt, the wrong prefix, or the obsolete DES format all produce weak hashes. For new code, prefer the higher-level helpers that wrap crypt() and handle salt generation and algorithm selection for you:
<?php
$hash = password_hash("password123", PASSWORD_DEFAULT); // generates a strong bcrypt hash + random salt
if (password_verify("password123", $hash)) {
echo "Password is valid!";
}
?>password_hash() and password_verify() are the recommended way to store and check passwords in modern PHP. Reach for crypt() only when you must interoperate with an existing system that already stores crypt()-format hashes.
For simple, non-password hashing (checksums, cache keys, ETags), see the md5() and sha1() functions — note that those are not suitable for passwords. For more string utilities, see the PHP String functions reference.