crypt in PHP

Syntax of crypt in PHP

string crypt(string string[, string salt])

Encrypts string using the DES encryption algorithm seeded with the two-character salt value salt. If salt is not supplied, a random salt value is generated the first time crypt() is called in a script; this value is used on subsequent calls to crypt(). Returns the encrypted string.

Example of crypt in PHP

<?php
 // let the salt be automatically generated; not recommended
$hashed_password = crypt('mypassword');

/* You should pass the entire results of crypt() as the salt for comparing a
   password, to avoid problems when different hashing algorithms are used. (As
   it says above, standard DES-based password hashing uses a 2-character salt,
   but MD5-based hashing uses 12.) */
if (hash_equals($hashed_password, crypt($user_input, $hashed_password))) {
   echo "Password verified!";
}
?>

Leave a Comment