PHP crypt() Function


The crypt() function is used for one-way hashing of strings. It is used to store passwords securely.

Syntax
crypt(str,salt)
Parameters
  • string - (Required) Specify the string to hash.
  • salt - (Optional) Random string to be used as the base of the hashing.
Example
<?php
  $password = "Welcome123";
  
  $hashedPassword = crypt($password);
  echo "Original password: {$password}<br>";
  echo "Hashed password : {$hashedPassword}<br>";
  
  $hashedPasswordSalt = crypt($password,'$2a$09$kdjdyvujdherkkcvoijhdkemreogjjodf');
  echo "Hashed password with salt : {$hashedPasswordSalt}"; 
?>

Output

Original password: Welcome123
Hashed password : $1$96RY9TTs$KDSvQ574wKAO9ALBMfgzJ/
Hashed password with salt : $2a$09$kdjdyvujdherkkcvoijhdePinilgfR.LFZ.E.IucZrq8LDrq3rfxy

Prev Next