Home >>PHP String Functions >PHP crypt() Function
PHP crypt() function is an in-built function of PHP. Crypt() function converts any given input string in to hash string. For the conversion, this function uses different algorithms like MD5, SHA256, or Blowfish. On different operating systems this crypt() function behaves differently.
The crypt() function doesn’t have any counter function for decryption.
Syntax :
crypt($string, $salt)
The $string parameter is the given input string which is to be hashed. This parameter is mandatory.The salt parameter is not mandatory, it’s optional but without this $salt crypt() function creates a weak password. So we try to give a strong salt value for strong security.
There are also some constants that are used With the crypt() function we also use some constants whose values are defined at the time of installation of PHP.
Let's see an example for crypt() function:
<?php if (CRYPT_STD_DES == 1) { echo "Standard DES: ".crypt('PHPTPOINT','st')."\n<br><br>"; } else { echo "Standard DES not supported.\n<br>"; } if (CRYPT_EXT_DES == 1) { echo "Extended DES: ".crypt('PHPTPOINT','_S4..some')."\n<br><br>"; } else { echo "Extended DES not supported.\n<br>"; } if (CRYPT_MD5 == 1) { echo "MD5: ".crypt('PHPTPOINT','$1$somethin$')."\n<br><br>"; } else { echo "MD5 not supported.\n<br>"; } if (CRYPT_BLOWFISH == 1) { echo "Blowfish: ".crypt('PHPTPOINT','$2a$09$anexamplestringforsalt$')."\n<br><br>"; } else { echo "Blowfish DES not supported.\n<br>"; } if (CRYPT_SHA512 == 1) { echo "SHA-512: ".crypt('PHPTPOINT','$6$rounds=5000$anexamplestringforsalt$')."\n<br><br>"; } else { echo "SHA-512 not supported."; } if (CRYPT_SHA256 == 1) { echo "SHA-256: ".crypt('PHPTPOINT','$5$rounds=5000$anexamplestringforsalt$')."\n<br><br>"; } else { echo "SHA-256 not supported.\n<br>"; } ?>