时时商务社区

标题: 详谈PHP中的密码安全性Password Hashing [打印本页]

作者: yj1281    时间: 2018-2-14 05:28

            如果你还在用md5加密,建议看看下方密码加密和验证方式。
先看一个简单的Password Hashing例子:

下方代码提供了一个完整的模拟的 User 类,在这个类中,通过使用Password Hashing,既能安全地处理用户的密码,又能支持未来不断变化的安全需求。
passwordHash and $data->username
    $this->data = new stdClass();
    $this->data->passwordHash = 'dbd014125a4bad51db85f27279f1040a';
  }
  // Mock save functionality
  public function save() {
    // Store the data from $data back into the database
  }
  // Allow for changing a new password:
  public function setPassword($password) {
    $this->data->passwordHash = password_hash($password, self::HASH, ['cost' => self::COST]);
  }
  // Logic for logging a user in:
  public function login($password) {
    // First see if they gave the right password:
    echo "Login: ", $this->data->passwordHash, "\n";
    if (password_verify($password, $this->data->passwordHash)) {
      // Success - Now see if their password needs rehashed
      if (password_needs_rehash($this->data->passwordHash, self::HASH, ['cost' => self::COST])) {
        // We need to rehash the password, and save it. Just call setPassword
        $this->setPassword($password);
        $this->save();
      }
      return true; // Or do what you need to mark the user as logged in.
    }
    return false;
  }
}
以上这篇详谈PHP中的密码安全性Password Hashing就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持脚本之家。
            
            
您可能感兴趣的文章:
  • 理解php Hash函数,增强密码安全
  • php password密码验证正则表达式(8位长度限制)
  • PHP更安全的密码加密机制Bcrypt详解
  • PHP实现根据密码长度显示安全条
            




    欢迎光临 时时商务社区 (http://bbs.4435.cn/) Powered by Discuz! X3.2