The crypt() function returns a hashed string using DES, Blowfish, or MD5 algorithms.
This function behaves different on different operating systems. PHP checks what algorithms are available and what algorithms to use when it is installed.
The salt parameter is optional. However, crypt() creates a weak password without the salt. Make sure to specify a strong enough salt for better security.
There are some constants that are used together with the crypt() function. The value of these constants are set by PHP when it is installed.
Constants:
On systems where this function supports multiple algorithms, the constants above are set to "1" if supported and "0" otherwise.
Note: There is no decrypt function. The crypt() function uses a one-way algorithm.
crypt(
str,salt)
Parameter | Description |
---|---|
str | Required. Specifies the string to be hashed |
salt | Optional. A salt string to base the hashing on |
Return Value: | Returns the encoded string or a string that is shorter than 13 characters and is guaranteed to differ from the salt on failure |
---|---|
PHP Version: | 4+ |
Changelog: | PHP 5.6.0 - Shows a E_NOTICE security warning if salt is omitted. PHP 5.3.7 - Added $2x$ and $2y$ Blowfish modes. PHP 5.3.2 - Added SHA-256 and SHA-512. Fixed Blowfish behavior on invalid rounds returns "failure" string ("*0" or "*1"), instead of falling back to DES. PHP 5.3.0 - PHP now contains its own implementation for MD5 crypt, Standard DES, Extended DES and the Blowfish algorithms and will use that if the system lacks of support for one or more of the algorithms. |
In this example we will test the different algorithms:
<?php
// 2 character salt
if (CRYPT_STD_DES == 1)
{
echo "Standard DES: ".crypt('something','st')."\n<br>";
}
else
{
echo "Standard DES not supported.\n<br>";
}
// 4 character salt
if (CRYPT_EXT_DES == 1)
{
echo "Extended DES: ".crypt('something','_S4..some')."\n<br>";
}
else
{
echo "Extended DES not supported.\n<br>";
}
// 12 character salt starting with $1$
if (CRYPT_MD5 == 1)
{
echo "MD5: ".crypt('something','$1$somethin$')."\n<br>";
}
else
{
echo "MD5 not supported.\n<br>";
}
// Salt starting with $2a$. The two digit cost parameter: 09. 22 characters
if (CRYPT_BLOWFISH == 1)
{
echo "Blowfish: ".crypt('something','$2a$09$anexamplestringforsalt$')."\n<br>";
}
else
{
echo "Blowfish DES not supported.\n<br>";
}
// 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<br>"; }
else
{
echo "SHA-256 not supported.\n<br>";
}
// 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.";
}
?>
The output of the code above could be (depending on the operating system):
Standard DES: stqAdD7zlbByI
Extended DES: _S4..someQXidlBpTUu6
MD5: $1$somethin$4NZKrUlY6r7K7.rdEOZ0w.
Blowfish: $2a$09$anexamplestringforsaleLouKejcjRlExmf1671qw3Khl49R3dfu
SHA-256: $5$rounds=5000$anexamplestringf$KIrctqsxo2wrPg5Ag/hs4jTi4PmoNKQUGWFXlVy9vu9
SHA-512: $6$rounds=5000$anexamplestringf$Oo0skOAdUFXkQxJpwzO05wgRHG0dhuaPBaOU/
oNbGpCEKlf/7oVM5wn6AN0w2vwUgA0O24oLzGQpp1XKI6LLQ0.
截取页面反馈部分,让我们更快修复内容!也可以直接跳过填写反馈内容!