php加密用户密码

1.md5,

2.Password Hashing api

	// 用户密码加密 Password Hashing 
	$options = [
		// 'salt' => custom_function_for_salt(), //自定义函数来获得盐值
		'salt' => 'aFXBxYmkClsw46y7b8C5qN56zs', //test
		'cost' => 12 //the default cost is 10
	];
	// 加密
	$hash = password_hash($upwd,PASSWORD_DEFAULT,$options); 
         //输出60个字符:    
	// $2y$12$aFXBxYmkClsw46y7b8C5q.YaLI9FDsEVhGKUlmZcQtvT.OYKsropu

     // $hash = password_hash($upwd,PASSWORD_DEFAULT); //未加盐

     //输出60个字符:$2y$10$lEVWiXI85lYcSk1IrzN.0us6K/eSh./QIEvgBnnft4Gh/b4BbdyZ.

验证:密码

//下面的代码是验证密码

if (password_verify($pwd,$hash)) {
    echo "密码正确";
} else {
    echo "密码错误";
}

更换更改加密方式

if (password_needs_rehash($hash, PASSWORD_DEFAULT, ['cost' => 12])) {
  // cost 变为 12
  $hash = password_hash($password, PASSWORD_DEFAULT, ['cost' => 12]);
 
  // 然后重新保存 hash值
}



猜你喜欢

转载自blog.csdn.net/milli236/article/details/81018129