cakephp - 使用 CakePHP 和河豚更改密码

标签 cakephp cakephp-2.0 bcrypt blowfish

我正在尝试设置一个表单以允许用户使用 CakePHP 2.3 更改他们的密码。使用的算法是河豚。我有以下三个字段:

<?php echo $this->Form->input('old_password', array('type' => 'password', 'autocomplete' => 'off')); ?>
<?php echo $this->Form->input('new_password', array('type' => 'password', 'autocomplete' => 'off')); ?>
<?php echo $this->Form->input('new_password_confirm', array('type' => 'password', 'autocomplete' => 'off', 'label' => 'Confirm Password')); ?>

这是我尝试验证他们正确输入旧密码的代码:
$hash = Security::hash($this->request->data['User']['old_password'], 'blowfish');
$correct = $this->User->find('first', array(
    'conditions' => array(
        'User.id' => AuthComponent::user('id'),
        'User.password' => $hash
    ),
    'fields' => array('id')
));

问题是即使我正确输入了旧密码,Cake 也永远找不到用户,因为它似乎没有计算正确的哈希值。每次我使用相同的旧密码提交表单时,Cake 每次都会生成不同的哈希值。这可能是由于我对河豚/bcrypt 算法的工作原理缺乏了解,但我似乎无法弄清楚。

我在这里缺少什么?

最佳答案

使用河豚哈希与使用其他哈希类型不同。来自 hash 的 API 文档方法:

Comparing Hashes: Simply pass the originally hashed password as the salt.



这意味着在您的情况下,您首先必须检索特定用户的散列密码,然后将其用作盐。就像是
$user = $this->User->find('first', array(
  'conditions' => array(
    'User.id' => AuthComponent::user('id')
  ),
  'fields' => array('password')
));
$storedHash = $user['User']['password'];
$newHash = Security::hash($this->request->data['User']['old_password'], 'blowfish', $storedHash);
$correct = $storedHash == $newHash;

关于cakephp - 使用 CakePHP 和河豚更改密码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17251814/

相关文章:

共享 Windows Plesk 服务器上的 CakePHP

php - 在插件元素 CakePHP 3 中获取当前插件名称

php - CakePHP 发生错误时发送电子邮件

php - CakePHP 如何处理带/不带 'id' 字段的 HABTM 表?

php - 在cakephp中的当前url中添加参数

php - MYSQL NEIGHBOR ROWS 和 CURRENT ROW

java - 如何从 Java 创建 Meteor 密码?

CakePHP 配置文件

java - Spring Boot 中自定义 LdapUserDetailsS​​ervice 始终无法匹配密码

java - 将 BCrypt 与 char[] 一起使用