找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 2029|回复: 0
打印 上一主题 下一主题

详谈PHP中的密码安全性Password Hashing

[复制链接]

2487

主题

2487

帖子

7391

积分

论坛元老

Rank: 8Rank: 8

积分
7391
跳转到指定楼层
楼主
发表于 2018-2-14 05:28:40 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式

            如果你还在用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实现根据密码长度显示安全条
            
  • 分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
    收藏收藏
    回复

    使用道具 举报

    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    用户反馈
    客户端