[李景山php]每天laravel-20160917|BcryptHasher
2016-07-19 09:02
471 查看
<?php namespace Illuminate\Hashing; use RuntimeException; use Illuminate\Contracts\Hashing\Hasher as HasherContract; // more namespace class BcryptHasher implements HasherContract {// this BcryptHasher implements HasherContract /** * Default crypt cost factor. * * @var int */ protected $rounds = 10;// Default crypt cost factor /** * Hash the given value. * * @param string $value * @param array $options * @return string * * @throws \RuntimeException */ public function make($value, array $options = [])// one is value ,other is config or options {// Hash the given value. means to crypt this value $cost = isset($options['rounds']) ? $options['rounds'] : $this->rounds;// get the rounds use like cost $hash = password_hash($value, PASSWORD_BCRYPT, ['cost' => $cost]);// use a wrap function to finish this action // with three parameter ,value flag and config about cost if ($hash === false) {// if wrong throw Exception throw new RuntimeException('Bcrypt hashing not supported.'); } // else return this value return $hash; } /** * Check the given plain value against a hash. * * @param string $value * @param string $hashedValue * @param array $options * @return bool */ public function check($value, $hashedValue, array $options = []) { if (strlen($hashedValue) === 0) { return false; }// check len about this str return password_verify($value, $hashedValue);// use a wrap function // two parameter ,one is value and other is hashedValue }//check the given value /** * Check if the given hash has been hashed using the given options. * * @param string $hashedValue * @param array $options * @return bool */ public function needsRehash($hashedValue, array $options = [])// determine thi value is been hashed { $cost = isset($options['rounds']) ? $options['rounds'] : $this->rounds; return password_needs_rehash($hashedValue, PASSWORD_BCRYPT, ['cost' => $cost]); }// a wrap function /** * Set the default password work factor. * * @param int $rounds * @return $this */ public function setRounds($rounds) { $this->rounds = (int) $rounds; return $this; }// set Rounds }
相关文章推荐
- [李景山php]每天laravel-20160916|FileSystemServiceProvider
- ThinkPHP 解决验证码不显示的方法
- php cgi fastcgi php-fpm区别
- Mac Pro 编译安装 PHP 5.6.21 及 问题汇总
- PHP中如何在数组中随机抽取n个数据的值 - array_rand()?
- PHP连接MySQL报错:SQLSTATE[HY000] [2002] Can't connect to local MySQL server through socket 'MySQL' (2)
- PHP实现HTML页面静态化的方法
- php判断是否为今天
- CCNA_ VTP、VLAN和单臂路由配置
- deepin安装vsftpd(仅安装篇)
- php验证码生成类
- php hash函数
- 深入理解PHP JSON数组与对象
- 在PHP语言中使用JSON和将json还原成数组的方法
- PHP中Socket连接及读写数据超时问题分析
- PHP实现数组array转换成xml的方法
- 分析PHP中单双引号的误区和双引号小隐患
- 提升PHP编程效率的20个要素
- php递归求无限分类子id;
- Yii2实用基础学习笔记(九):单文件上传