PHP crypt() Function
PHP crypt() Function
The crypt () function in PHP returns a hashed string using DES, Blowfish, or MD5 algorithms.
Some constants of crypt() function are as follows:
- [CRYPT_STD_DES]
- [CRYPT_EXT_DES]
- [CRYPT_MD5]
- [CRYPT_BLOWFISH]
- [CRYPT_SHA_256]
- [CRYPT_SHA_512] etc.
Syntax
crypt ( string $str [, string $salt ] )
Parameter
str(required)- This parameter represents the string to be hashed
salt(optional)- This parameter represents the salt string to base the hashing on.
Return
This function returns the hashed string or a string that is shorter than 13 characters.
Example 1
Output
Standard DES: stqAdD7zlbByI
Example 2
<?php
// 12 character salt starting with $1$
if (CRYPT_MD5 == 1)
{
echo "MD5: ".crypt('tutorialandexample','$1$tutorialandexample$')."\n";
}
else
{
echo "MD5 not supported.\n";
}
?>
Output
MD5: $1$tutorial$h33eIOE.o5xIc2MCML1M2/
Example 3
<?php
// 16 character salt starting with $5$. The default number of rounds is 5000.
if (CRYPT_SHA256 == 1) {
echo "SHA-256: ".crypt('tutorialandexample','$565$rounds=98$tutorialandexamplestringforsalt$')."\n"; }
else{
echo "SHA-256 not supported.\n";
}
?>
Output
SHA-256: $5c8GnoWJydmI
Example 4
<?php
// 16 character salt starting with $6$. The default number of rounds is 5000.
if (CRYPT_SHA512 == 1)
{
echo "SHA-512: ".crypt('something','$6$rounds=5000$anexamplestringforsalt$');
}
else
{
echo "SHA-512 not supported.";
}
?>
Output
SHA-512: $6$rounds=5000$anexamplestringf$Oo0skOAdUFXkQxJpwzO05wgRHG0dhuaPBaOU/oNbGpCEKlf/7oVM5wn6AN0w2vwUgA0O24oLzGQpp1XKI6LLQ0
Example 5
<?php
// 16 character salt starting with $5$. The default number of rounds is 5000.
if (CRYPT_SHA256 == 1)
{
echo "SHA-256: ".crypt('something','$5$rounds=5000$anexamplestringforsalt$')."\n"; }
else
{
echo "SHA-256 not supported.\n";
}
?>
Output
SHA-256: $5$rounds=5000$anexamplestringf$KIrctqsxo2wrPg5Ag/hs4jTi4PmoNKQUGWFXlVy9vu9
