如何根据此代码使用哈希更新密码?假设我希望我的新密码为“ test123”(不带引号)。我如何生成此哈希并将其放入此数组中以便成功登录?
<?php
class AuthenticationService
{
static $users = array( );
static function authenticate($user, $pwd)
{
$user = strtolower($user);
//var_dump(self::$users);
if (array_key_exists($user, self::$users))
{
//echo "user found </br>";
if (self::$users[$user][1] == sha1(self::$users[$user][0].$pwd))
return(true);
else
return(false);
}
return(false);
}
}
?>
答案 0 :(得分:1)
您的$users
数组是用户名和salt + hash之间的关联数组:
static $users = array(
"testing" => array("0a1e5dd121178b2f780622e5fbd926d5","d190c655a6f85d48ff6c1981d61e938fd147580b"),
^ ^ ^
username salt sha1 hash
);
代码通过获取两个传入的密码$pwd
的SHA1哈希值与与用户名相关联的salt串接来检查密码。
static function authenticate($user, $pwd)
{
...
...
// dot operator concatenates salt & password
V
if (self::$users[$user][1] == sha1(self::$users[$user][0].$pwd))
return(true);
else
return(false);
...
}
为了添加新帐户进行测试,您可以:
"apple"
,"fish123"
,"im a complicated hash"
)$users
数组中。例如:
// new username = "fish"
// new password = "ilikephp"
// new salt = "UwU"
// Then:
static $users = array(
"testing" => array("0a1e5dd121178b2f780622e5fbd926d5","d190c655a6f85d48ff6c1981d61e938fd147580b"),
"fish" => array("UwU", sha1("UwU"."ilikephp")),
^
order of concatenation matters: salt + pasword
);
那么什么是盐?由于我自己不是专家,因此我将只留下wikipedia link。请注意,在Wikipedia的示例中,哈希是以相反的顺序计算的,即密码+盐而不是salt + password。