Computer >> Computer tutorials >  >> Programming >> PHP

crypt() function in PHP


The crypt() function is used to hash the string using using algorithms like DES, Blowfish, or MD5.

Note − This function behaves different on different operating systems.

The following are some constants used together with the crypt() function.

  • [CRYPT_STD_DES] - Standard DES-based hash with two character salt from the alphabet "./0-9A-Za-z".

  • [CRYPT_EXT_DES] - Extended DES-based hash with a nine character salt consisting of an underscore followed by 4 bytes of iteration count and 4 bytes of salt.

  • [CRYPT_MD5] - MD5 hashing with a 12 character salt starting with $1$

  • [CRYPT_BLOWFISH] - Blowfish hashing with a salt starting with $2a$, $2x$, or $2y$, a two digit cost parameters "$", and 22 characters from the alphabet "./0-9A-Za-z".

  • [CRYPT_SHA_256] - SHA-256 hash with a 16 character salt starting with $5$.

  • [CRYPT_SHA_512] - SHA-512 hash with a 16 character salt starting with $6$.

Syntax

crypt(str, salt)

Parameters

  • str − The string to be hashed. Required.

  • salt − Salt string to base the hashing on. Optional.

Return

The crypt() function returns the encoded string or a string shorter than 13 characters and is guaranteed to differ from the salt on failure

The following is an example −

Example

<?php
if (CRYPT_STD_DES == 1) {
   echo "DES supported = ".crypt('demo','st')."\n";
} else {
   echo "DES not supported!";
}
?>

The following is the output −

Output

DES supported = st7zBedJadRn2