Home PHP String Functions crypt() PHP crypt() Function Definition In PHP, crypt() creates a one-way hash of a string using a salt. The salt also selects the algorithm.
The string is required. Salt is optional but strongly recommended. Prefer password_hash() for new password code.
Syntax Call crypt with a salt. Start with this line.
$hash = crypt($password, $salt); Call What it does crypt($password, $salt)Hashes using the salt/algorithm. hash_equals($hash, crypt($password, $hash))Constant-time verify pattern.
Prefer password_hash() and password_verify() for modern password storage.
Library declarations crypt(string $string, string $salt = ?): string Parameters Parameter Description string Required. The password or data to hash. salt Optional. Salt string that also selects the hashing algorithm.
Return Value Return Value Description string The hashed string, including salt/algorithm info.
PHP Examples 1. Hash with crypt 2. Verify login 3. password_hash Example 1 — Create a Bcrypt Hash with crypt() Build a $2y$ salt, then hash. Store the entire return value.
<?php
$password = "secret";
$salt = '$2y$10$' . substr(
strtr(base64_encode(random_bytes(16)), '+', '.'),
0,
22
);
$hash = crypt($password, $salt);
if (strlen($hash) < 13) {
fwrite(STDERR, "crypt() failed\n");
exit(1);
}
echo $hash, PHP_EOL; $2y$10$......................xxxxxxxxxxxxxxxxxxxxxx
(exact string changes every run — salt is random) How It Works Make a bcrypt salt. Prefix $2y$10$ plus 22 salt characters.Call crypt. Hashes the password with that salt/algorithm.Check length. A very short result means failure on this platform.Store the full hash. It already contains the salt needed for verify.Example 2 — Verify a Password Re-hash the guess using the stored hash as the salt, then compare safely.
<?php
$stored = crypt("secret", '$2y$10$' . substr(
strtr(base64_encode(random_bytes(16)), '+', '.'),
0,
22
));
$guess = "secret";
$again = crypt($guess, $stored);
if (hash_equals($stored, $again)) {
echo "Password OK\n";
} else {
echo "Password rejected\n";
} How It Works Create a stored hash. Same idea as Example 1 (random salt each run).Hash the guess. Pass $stored as the salt so the algorithm and salt match.Compare with hash_equals. Avoids timing leaks from === on secrets.Wrong guess fails. Change $guess to see Password rejected.Example 3 — Prefer password_hash() for New Code Same security goal, safer defaults. This is what you should ship.
<?php
$hash = password_hash("secret", PASSWORD_DEFAULT);
if (password_verify("secret", $hash)) {
echo "Password OK\n";
} else {
echo "Password rejected\n";
}
echo substr($hash, 0, 7), "…\n"; // usually $2y$… How It Works password_hash. Creates a salted bcrypt/argon2 hash for you.password_verify. Checks a guess against the stored hash.No manual salt. Safer than building $2y$ strings by hand.Still related to crypt. Under the hood many builds use crypt-compatible formats.Frequently Asked Questions What does PHP crypt() do? crypt() performs one-way hashing of a string using a salt that selects the algorithm (for example bcrypt). The returned hash embeds the salt and algorithm id—there is no decrypt step.
What is the syntax for crypt()? string crypt(string $string, string $salt). Both arguments are required since PHP 8.0. The salt string chooses the algorithm (for example $2y$10$… for bcrypt) and must be generated securely.
How do I verify a password with crypt()? Pass the stored hash as the salt: crypt($userInput, $storedHash). If the result equals $storedHash, the password matches. Compare with hash_equals() to avoid timing attacks.
Should I use crypt() or password_hash() for new projects? Use password_hash() and password_verify() for new apps. They wrap crypt() with secure defaults, automatic salts, and safer APIs.
Can crypt() work without a salt in PHP 8? No. The salt parameter is required since PHP 8.0. Omitting it was already insecure in older versions.
CODETOFUN NEWS
Stay Ahead in Tech Get the latest IT news, programming updates, and technology trends directly on your phone.
About the author Developer, cloud engineer, and technical writer
I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.
8 people found this page helpful
Helpful Share Copy link Suggestion