试图理解PHP password_hash

use*_*817 2 php security hash

我正在尝试更多地了解PHP安全性最佳实践,我遇到了Anthony_ Fersh 和Anthony Ferrara 的password_compat项目.我想我理解如何实现它,但在测试中,我遇到了一个奇怪的行为,这与我对密码散列的新手理解相矛盾.

如果我将password_hash函数的结果保存到MySQL数据库用户记录中,然后使用password_verify检索该哈希以进行验证,则它会按预期工作.但是,如果我做了完全相同的事情,而不是从数据库中提取,我只是通过数据库中的复制/粘贴硬编码密码哈希,password_verify函数失败.

代码如下:

// Get the Username and password hash from the MySQL database.  GetPassTestuser routine returns an array where
//   position[0][0] is the username and position[0][1] is the password hash.
$arrUser = GetPassTestuser("mike24");
echo("User Name: ".$arrUser[0][0]."<br/>");
echo("Password hash: ".$arrUser[0][1]."<br/>");

// Run password_verify with the password hash collected from the database.  Compare it with the string "mytest"
//  (This returns true in my tests).
if (password_verify("mytest",$arrUser[0][1])){
    echo("Password verified");
} else {
    echo("Password invalid");
}
echo("<hr>Now On to our second test...<br/>");
// Now run password_verify with a string representation directly copied/pasted from the database.  This is 
//   being compared with "mytest", which in my mind should return a true value.  But it doesn't and this test
//   fails.  Not sure why.
if (password_verify("mytest","$2y$10$S33h20qxHndErOoxJL.sceQtBQXtSWrHieBtFv59jwVwJuGeWwKgm")){  // String shown here is the same as value contained in $arrUser[0][1]
    echo("2nd Test Password verified");
} else {
    echo("2nd test Password invalid");
}
Run Code Online (Sandbox Code Playgroud)

虽然这不是我在实际代码中所做的事情,但我只想了解其中的区别.当我使用可能包含完全相同的哈希值的字符串变量时它为什么工作正常,但是当它被硬编码时不起作用?

谢谢!

Bab*_*aba 5

来自PHP DOC

要指定文字单引号,请使用反斜杠()对其进行转义.要指定文字反斜杠,请将其加倍(\).反斜杠的所有其他实例将被视为文字反斜杠:这意味着您可能习惯使用的其他转义序列(如\ r或\n)将按字母顺序输出,而不是具有任何特殊含义.

如果遇到美元符号($),解析器将贪婪地获取尽可能多的令牌以形成有效的变量名称.将变量名称括在花括号中以显式指定名称的结尾.

使用单引号

更换

"$2y$10$S33h20qxHndErOoxJL.sceQtBQXtSWrHieBtFv59jwVwJuGeWwKgm"
Run Code Online (Sandbox Code Playgroud)

'$2y$10$S33h20qxHndErOoxJL.sceQtBQXtSWrHieBtFv59jwVwJuGeWwKgm'
Run Code Online (Sandbox Code Playgroud)

要么

"\$2y\$10\$S33h20qxHndErOoxJL.sceQtBQXtSWrHieBtFv59jwVwJuGeWwKgm"
Run Code Online (Sandbox Code Playgroud)