PHP string crypt() Function
The crypt() is predefined PHP string function. It is used to returns a hashed string by using DES, Blowfish, or MD5 algorithms.
Following are some constants of crypt() function
- [CRYPT_STD_DES]
- [CRYPT_EXT_DES]
- [CRYPT_MD5]
- [CRYPT_BLOWFISH]
- [CRYPT_SHA_256]
- [CRYPT_SHA_512] etc
Syntax:
string crypt ( string $str [, string $salt ] );
Parameter | Description | Required/Optional |
---|---|---|
str | Specify the string to be hashed | Required |
salt | Specify a salt sting | optional |
Example 1
<?php
// 2 character salt
if (CRYPT_STD_DES == 1){
echo "Standard DES: ".crypt('shorat innovation','si')."n<br>";
}
else{
echo "Standard DES not supported.n<br>";
}
?>
Output
Standard DES: siXXXEq9GnSRg
Example 2
<?php
// 4 character salt
if (CRYPT_EXT_DES == 1){
echo "Extended DES: ".crypt('shorat innovation','_S4..shor')."n<br>";
}
else{
echo "Extended DES not supported.n<br>";
}
?>
Output
Extended DES: _S4..shorc7kJCJPxtp6
Example 3
<?php
// 4 character salt
if (CRYPT_MD5 == 1){
echo "MD5: ".crypt('shorat innovation','$1$shoratinn$')."n<br>";
}
else{
echo "MD5 not supported.n<br>";
}
?>
Output
MD5: $1$shoratinn$/QyBQ/V7dJjcGaOq83EhL0
Example 4
<?php
if (CRYPT_BLOWFISH == 1){
echo "Blowfish: ".crypt('shorat innovation','$2a$09$anexamplestringforsalt$')."n<br>";
}
else{
echo "Blowfish DES not supported.n<br>";
}
?>
Output
Blowfish: $2a$09$anexamplestringforsale8idRk7z/D1GAsmHMUTi1L/e7Cjcngiy
Example 5
<?php
// 16 character salt starting with $5$. The default number of rounds is 5000.
if (CRYPT_SHA256 == 1) {
echo "SHA-256: ".crypt('shorat innovation','$5$rounds=5000$anexamplestringforsalt$')."n<br>";
}
else{
echo "SHA-256 not supported.n<br>";
}
?>
Output
SHA-256: $5$rounds=5000$anexamplestringf$cdf4KmhoNRhj0riAq6kpiYdPHGWOBEnPxtPxje3Fjm2
Example 6
// 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 7
<?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