【问题标题】:Why does this code hash null?为什么此代码散列为空?
【发布时间】:2013-02-02 01:49:50
【问题描述】:

以下代码通过用户名检查,但在密码检查失败。

如您所见,哈希值被回显,但出于某种原因,它们输出e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855,即/dev/nullsha256sum。由于密码似乎根本没有回显,我只能假设它无法获取 POST,但为什么呢?

登录

<form action="dologin" method="post">
Username: <input type="text" name="username">
Password: <input type="password" name="password">
<input type="submit">
</form>

dologin

    if ( $_POST[username] == $actualusername ) {
        // Hash the password
        $hashedpassword = hash('sha256', $_POST[password]);
        echo $_POST[password];
        echo $hashedpassword;
        if ( $hashedpassword == $actualpassword ) {
            echo '<h2>Logged in</h2>';
        } else {
        echo '<h2>Incorrect password</h2>';
        echo $hashedpassword;
        }
    } else {
        echo '<h2>Incorrect username</h2>';
    }

【问题讨论】:

  • 您不应该使用 SHA* 系列来散列密码。见this answerJeff's Post on Passwords
  • 也请使用$_POST['password'] 而不是[password]
  • @Gargron Why?
  • @LewisGoddard - 因为它可能会导致一个非常讨厌且难以找到的错误,所以其他开发人员或外部库是否应该在某处定义一个名为 password 的常量?

标签: php forms post passwords sha256


【解决方案1】:

关闭输入标签能解决你的问题吗? 此外,您可以使用

isset($_POST["blabla"])

测试该值是否设置在$_POST 中。

【讨论】:

  • 是的!我认为 HTML5 意味着您不需要关闭链接、元、img 和输入项之类的东西?多么奇特的怪癖。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-06-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多