PHP string crypt() 函数
crypt() 是预定义的 PHP 字符串函数。它用于使用 DES、Blowfish 或 MD5 算法返回一个哈希字符串。
以下是 crypt() 函数的一些常量:
- [CRYPT_STD_DES]
- [CRYPT_EXT_DES]
- [CRYPT_MD5]
- [CRYPT_BLOWFISH]
- [CRYPT_SHA_256]
- [CRYPT_SHA_512] 等。
语法
string crypt ( string str [, stringsalt ] );
参数 | 描述 | 必填/可选 |
---|---|---|
str | 指定要进行哈希处理的字符串 | 必填 |
salt | 指定一个盐字符串 | 可选 |
注意:此crypt()函数适用于不同的操作系统。
示例1
<?php
// 2 character salt
if (CRYPT_STD_DES == 1){
echo "Standard DES: ".crypt('javatpoint','jt')."\n<br>";
}
else{
echo "Standard DES not supported.\n<br>";
}
?>
输出:
Standard DES: jtigeEQ4GnSRg \
示例2
<?php
// 4 character salt
if (CRYPT_EXT_DES == 1){
echo "Extended DES: ".crypt('javatpoint','_S4..java')."\n<br>";
}
else{
echo "Extended DES not supported.\n<br>";
}
?>
输出:
Extended DES: _S4..javac7kJCJPxtp6
示例3
<?php
// 4 character salt
if (CRYPT_MD5 == 1){
echo "MD5: ".crypt('javatpoint','1javatpoint$')."\n<br>";
}
else{
echo "MD5 not supported.\n<br>";
}
?>
输出:
MD5: 1javatpoi$/QyBQ/V7dJjcGaOq83EhL0
注意:使用16个字符以5开头的盐,并且默认的迭代轮数是5000。
示例4
<?php
if (CRYPT_BLOWFISH == 1){
echo "Blowfish: ".crypt('javatpoint','2a09anexamplestringforsalt')."\n<br>";
}
else{
echo "Blowfish DES not supported.\n<br>";
}
?>
输出:
Blowfish: 2a09$anexamplestringforsale8idRk7z/D1GAsmHMUTi1L/e7Cjcngiy
示例5
<?php
// 16 character salt starting with 5. The default number of rounds is 5000.
if (CRYPT_SHA256 == 1) {
echo "SHA-256: ".crypt('javatpoint','5rounds=5000anexamplestringforsalt')."\n<br>"; }
else{
echo "SHA-256 not supported.\n<br>";
}
?>
输出:
SHA-256: 5rounds=5000anexamplestringfcdf4KmhoNRhj0riAq6kpiYdPHGWOBEnPxtPxje3Fjm2
示例6
// 16 character salt starting with 6. The default number of rounds is 5000.
if (CRYPT_SHA512 == 1)
{
echo "SHA-512: ".crypt('something','6rounds=5000anexamplestringforsalt');
}
else
{
echo "SHA-512 not supported.";
}
输出:
SHA-512: 6rounds=5000anexamplestringfOo0skOAdUFXkQxJpwzO05wgRHG0dhuaPBaOU/oNbGpCEKlf/7oVM5wn6AN0w2vwUgA0O24oLzGQpp1XKI6LLQ0
示例7
<?php
// 16 character salt starting with 6. The default number of rounds is 5000.
if (CRYPT_SHA512 == 1) {
echo "SHA-512: ".crypt('something','6rounds=5000anexamplestringforsalt');
}
else{
echo "SHA-512 not supported.";
}
?>
输出:
SHA-512: 6rounds=5000anexamplestringfOo0skOAdUFXkQxJpwzO05wgRHG0dhuaPBaOU/oNbGpCEKlf/7oVM5wn6AN0w2vwUgA0O24oLzGQpp1XKI6LLQ0