【问题标题】:Why is crypt() returning different hashes with the same salt?为什么 crypt() 返回具有相同盐的不同哈希?
【发布时间】:2012-08-13 23:59:27
【问题描述】:
public static function blowfish($password, $storedpass = false) {
    //if encrypted data is passed, check it against input ($info) 
      if ($storedpass) { 
            if (substr($storedpass, 0, 60) == crypt($password, "$2a$08$".substr($storedpass, 60))) { 
                return true; 
            }  else { 
                return false; 
            } 
      }  else { 
            //make a salt and hash it with input, and add salt to end 
            $salt = "143cd669b02e155c3cca6e";//substr(bin2hex(openssl_random_pseudo_bytes(22)), 0, 22);
            //for ($i = 0; $i < 22; $i++) { 
                //$salt .= substr("./ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789", mt_rand(0, 63), 1); 
            //} 

            //return 82 char string (60 char hash & 22 char salt) 
            return crypt($password, "$2a$08$".$salt).$salt; 
     }
}

print(substr($storedpass, 0, 60)."<br />");
print(crypt($password, "$2a$08$".substr($storedpass, 60))."<br />");
print(substr($storedpass, 60));

产生结果:

$2a$08$143cd669b02e155c3cca6eM3k8s9BdE4jErJXJ8wSxshJDPcJQVPW
$2a$08$143cd669b02e155c3cca6eEiYm6ilW1ZC1PBS07LOh2XSq1NODSKK
143cd669b02e155c3cca6e

你可以看到我之前生成了一个 22 个字符的随机盐,我知道 PHPASS,mt_rand() 不是 CSPRNG 等等。让我感到困惑/担心的只是为什么 crypt()(给定 $ password = 'admin') 即使使用静态盐也会生成不同的哈希值。你可以看到我打印了 substr($storedpass, 60) 生成正确的盐,但是然后运行 ​​crypt() 函数(使用相同的参数来创建初始 $storedpass)它会生成不同的结果,破坏身份验证我的一个(相对较小且不是关键任务)应用程序......

【问题讨论】:

  • 建议将整个哈希作为盐传入:PHP 会为您处理子字符串等。
  • 值 60 与盐有何关系?为什么不使用常量?
  • @DarkXphenomenon 那个帖子有很多好处,但仍然没有回答问题。

标签: php encryption hash salt crypt


【解决方案1】:

您似乎将 $password 参数作为(未定义)发送给函数。

会生成这个哈希:

$2a$08$143cd669b02e155c3cca6eM3k8s9BdE4jErJXJ8wSxshJDPcJQVPW

但是(例如)如果你运行这个:

$password = 'admin';
echo $storedpass = blowfish($password)."<br />";

print(substr($storedpass, 0, 60)."<br />");
print(crypt('admin', '$2a$08$'.substr($storedpass, 60))."<br />");
print(substr($storedpass, 60));

if (blowfish($password, $storedpass) == true) {
    echo 1;
}

会输出:

$2a$08$143cd669b02e155c3cca6eEiYm6ilW1ZC1PBS07LOh2XSq1NODSKK143cd669b02e155c3cca6e
$2a$08$143cd669b02e155c3cca6eEiYm6ilW1ZC1PBS07LOh2XSq1NODSKK
$2a$08$143cd669b02e155c3cca6eEiYm6ilW1ZC1PBS07LOh2XSq1NODSKK
143cd669b02e155c3cca6e
1

【讨论】:

  • 感谢您的回答,但我已经验证 $password 正在打印“admin”,无论是在我调用我构建的 blowfish() 函数的控制器中,还是在该函数中。即使设置了,我也会看到不同的哈希值......
  • 但您必须找到或帮助我们帮助您找出密码发送为未定义的原因。你能发布你是如何调用那个函数的吗?
猜你喜欢
  • 1970-01-01
  • 2011-01-14
  • 2015-11-14
  • 2017-03-16
  • 1970-01-01
  • 2011-06-16
  • 2018-03-23
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多