<?php
$password = crypt('mypassword'); // let the salt be automatically generated
/* You should pass the entire results of crypt() as the salt for comparing a
password, to avoid problems when different hashing algorithms are used. (As
it says above, standard DES-based password hashing uses a 2-character salt,
but MD5-based hashing uses 12.) */
if (crypt($user_input, $password) == $password) {
echo "Password verified!";
}
?>
Run Code Online (Sandbox Code Playgroud)
为什么这样做?我认为这'mypassword'是我希望实际管理员使用的密码.所以我首先加密,并将其设置为等于$password.显然,我必须将其存储在DB中.但在接下来的行中,它被用作盐和我正在比较的东西,我不明白怎么crypt($user_input, $password)可能等于$password,如果在后一种情况下我理想的是正确的密码,$user_input但是$password被比较盐腌到$password.如果最后一行是,那对我来说会更有意义
if (crypt($user_input) == $password) {
echo "Password verified!";
}
Run Code Online (Sandbox Code Playgroud)
我不明白的是什么?
crypt是一个单向函数,并返回已包含salt的字符串.输出类似于存储的内容/etc/shadow.
来自php.net的示例:
<?php
echo 'result: ' . crypt('somepassword');
echo 'result: ' . crypt('somepassword');
echo 'result: ' . crypt('somepassword');
?>
result: $1$K2D8DGwq$b05uO37aMwO4rnDlB9Rsi1
result: $1$aPBvu2y.$213YVEs8/5m.jMCXSScly/
result: $1$dW3Xu2p6$nuCtJe2zzlgBMLxN2oZCx/
Run Code Online (Sandbox Code Playgroud)
将用户输入与crypt结果进行比较时,该函数会自动从字符串中提取salt.
| 归档时间: |
|
| 查看次数: |
2374 次 |
| 最近记录: |