Yii2重置密码
//Yii框架下hash密码正确的比较方法
$result = Yii::$app->security->validatePassword($password, $hash);
echo $result; //生成密码
public function generatePasswordHash($password, $cost = null)
{
if ($cost === null) {
$cost = $this->passwordHashCost;
} if (function_exists('password_hash')) {
/** @noinspection PhpUndefinedConstantInspection */
return password_hash($password, PASSWORD_DEFAULT, ['cost' => $cost]);
} $salt = $this->generateSalt($cost);
$hash = crypt($password, $salt);
// strlen() is safe since crypt() returns only ascii
if (!is_string($hash) || strlen($hash) !== 60) {
throw new Exception('Unknown error occurred while generating hash.');
} return $hash;
}
//生成盐
protected function generateSalt($cost = 13)
{
$cost = (int) $cost;
if ($cost < 4 || $cost > 31) {
throw new InvalidParamException('Cost must be between 4 and 31.');
} // Get a 20-byte random string
$rand = $this->generateRandomKey(20);
// Form the prefix that specifies Blowfish (bcrypt) algorithm and cost parameter.
$salt = sprintf("$2y$%02d$", $cost);
// Append the random salt data in the required base64 format.
$salt .= str_replace('+', '.', substr(base64_encode($rand), 0, 22)); return $salt;
}
//验证密码
public function validatePassword($password, $hash)
{
if (!is_string($password) || $password === '') {
throw new InvalidParamException('Password must be a string and cannot be empty.');
} if (!preg_match('/^\$2[axy]\$(\d\d)\$[\.\/0-9A-Za-z]{22}/', $hash, $matches)
|| $matches[1] < 4
|| $matches[1] > 30
) {
throw new InvalidParamException('Hash is invalid.');
} if (function_exists('password_verify')) {
return password_verify($password, $hash);
} $test = crypt($password, $hash);
$n = strlen($test);
if ($n !== 60) {
return false;
} return $this->compareString($test, $hash);
}
Yii2重置密码的更多相关文章
- MVC5 网站开发之六 管理员 2、添加、删除、重置密码、修改密码、列表浏览
目录 奔跑吧,代码小哥! MVC5网站开发之一 总体概述 MVC5 网站开发之二 创建项目 MVC5 网站开发之三 数据存储层功能实现 MVC5 网站开发之四 业务逻辑层的架构和基本功能 MVC5 网 ...
- ubuntu 重置密码
背景:在登陆ubuntu之后,按Ctr+Alt+F1进入控制台时,需要登陆,一时忘了密码... 参考:http://www.cnblogs.com/relaxgirl/p/3179507.html ...
- SharePoint重置密码功能Demo
博客地址 http://blog.csdn.net/foxdave 本文将说明一个简单的重置SharePoint用户密码(NTLM Windows认证)的功能如何实现 重置密码功能,实际上就是重置域用 ...
- Centos学习手册——装逼宝典之强制重置密码
---恢复内容开始--- Centos学习手册by RuffianFish; 痞子鱼 近日闲的无聊,而最近又在搞Centos决定写个Centos详细的学习手册,以便自己在长时间没摸Centos的情况下 ...
- MAC Mysql 重置密码
使用mac电脑,当mysql登录密码忘记时,需要重置密码.步骤如下: 1. 关闭当前正在运行的mysql进程. A.进入"偏好设置",选择mysql, 再选"stop m ...
- nessus重置密码
许久不用的nessus密码居然忘记了,查了下: cmd下进入到nessus的安装目录 提升为管理员,登录系统 如果想用之前的账号,可以直接在系统内重置密码.
- mysql 忘记root密码,重置密码,及重置密码后权限问题不能登录的解决方案
由于一段时间没有使用MySQL,忘记了root登录密码. 决定重置下密码,搜索帮助. 参考文档: http://blog.csdn.net/odailidong/article/details/507 ...
- mysql 服务【安装】【启动】【停止】【卸载】【重置密码】
windows安装:一.一机安装多个服务时,重复步骤,修改服务名即可: 1.mysqld install mysql_12336 --defaults-file="MYSQL_HOME\my ...
- mysql 重置密码
mysql 重置密码,跳过登录修改密码: # centos 上mysql 已经改名了,启动服务的时候注意是mariadb 了!!!!! # systemctl stop mariadb # syste ...
随机推荐
- c语言统计一个文件中的单词,字符和行数
body, table{font-family: 微软雅黑; font-size: 10pt} table{border-collapse: collapse; border: solid gray; ...
- scss 初学笔记 二 混合宏
混合宏 格式 @mixin 定义混合宏 (相当于变量声明 var $ ?) //不带参数混合宏 @mixin borderRadius{ -webkit-border-radius: 5px; b ...
- Servlet--HttpServlet类
HttpServlet类 定义 public class HttpServlet extends GenericServlet implements Serializable 这是一个抽象类,用来简化 ...
- HashiCorp Vault介绍
HashiCorp Vault是一款企业级私密信息管理工具.说起Vault,不得不提它的创造者HashiCorp公司.HashiCorp是一家专注于DevOps工具链的公司,其旗下明星级产品包括Vag ...
- 【Java SE】如何用Java实现反转排序
摘要:反转排序是将原先已经排序好了的重新排序,是原来的数组元素的顺序反转过来.假设原来的数组顺序是{6,5,4,3,2,1},反转之后的顺序就是{1,2,3,4,5,6}.这个排序的算法不是很难,代码 ...
- CSS样式中常用的字体名称
css中引入字体: @font-face { font-family: "AncientWar"; src: url('style/css/fonts/AncientWar.ttf ...
- php与HTML交互问题
1.将表单中的action属性值设为PHP路径,则网页会跳转到这个网址 <html> <body> <form action="welcome.php" ...
- GO开发:用go写个日志监控系统
日志收集系统架构 1.项目背景 a. 每个系统都有日志,当系统出现问题时,需要通过日志解决问题 b. 当系统机器比较少时,登陆到服务器上查看即可满足 c. 当系统机器规模巨大,登陆到机器上查看几乎不现 ...
- ssh免秘钥登录
简介 SSH 为 Secure Shell 的缩写,由 IETF 的网络小组(Network Working Group)所制定:SSH 为建立在应用层基础上的安全协议.SSH 是目前较可靠,专为远程 ...
- 开发快速定位需求(Coding之前的工作)
自我总结,求高人指点,欢迎拍砖! 目的:快速定位feature需求,避免浪费不必要的时间 需求目的:它要用来解决什么问题?(客户需求,bug fixed,学习新技术) 需求对象:它针对的对象是谁?(明 ...