PHP crypt() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
String

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.

PHP
$hash = crypt($password, $salt);
CallWhat 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
PHP
crypt(string $string, string $salt = ?): string

Parameters

ParameterDescription
stringRequired. The password or data to hash.
saltOptional. Salt string that also selects the hashing algorithm.

Return Value

Return ValueDescription
stringThe hashed string, including salt/algorithm info.

PHP Examples

Example 1 — Create a Bcrypt Hash with crypt()

Build a $2y$ salt, then hash. Store the entire return value.

PHP
<?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;

How It Works

  1. Make a bcrypt salt. Prefix $2y$10$ plus 22 salt characters.
  2. Call crypt. Hashes the password with that salt/algorithm.
  3. Check length. A very short result means failure on this platform.
  4. 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
<?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

  1. Create a stored hash. Same idea as Example 1 (random salt each run).
  2. Hash the guess. Pass $stored as the salt so the algorithm and salt match.
  3. Compare with hash_equals. Avoids timing leaks from === on secrets.
  4. 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
<?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

  1. password_hash. Creates a salted bcrypt/argon2 hash for you.
  2. password_verify. Checks a guess against the stored hash.
  3. No manual salt. Safer than building $2y$ strings by hand.
  4. Still related to crypt. Under the hood many builds use crypt-compatible formats.

Frequently Asked Questions

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.
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.
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.
Use password_hash() and password_verify() for new apps. They wrap crypt() with secure defaults, automatic salts, and safer APIs.
No. The salt parameter is required since PHP 8.0. Omitting it was already insecure in older versions.

Next: explode()

Split a string into an array using a delimiter.

explode() tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

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