• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
车车大人
博客园    首页    新随笔    联系   管理     

密码加密

/*
     * 注册账号密码加密
     * */
    function custom_function_for_salt(){
        return $salt = '$2y$11$' . substr(md5(uniqid(rand(), true)), 0, 22);
    }

    public function generateHashWithSalt($password) {
        $options = [
            'salt' => self::custom_function_for_salt(), //write your own code to generate a suitable salt
            'cost' => 12 // the default cost is 10
        ];
        $str = password_hash($password, PASSWORD_DEFAULT, $options);
        $res = array();
        $res['password'] = $str;
        $res['salt'] = $options['salt'];
        return $res;
    }

 

这个时候返回的password和salt是要入库的

 

那么我们在登录的时候先获取到数据库的password和salt,把salt传入到登录的密码验证方法里面,最后判断是不是和数据库的密码一致,见下图

/*
     * 登录密码验证
     * */
    public function LogingenerateHashWithSalt($password,$salt) {
        $options = [
            'salt' => $salt,
            'cost' => 12 // the default cost is 10
        ];
        $str = password_hash($password, PASSWORD_DEFAULT, $options);
        return $str;
    }

 

通往牛逼的路上,在意的只有远方!
posted @ 2017-08-15 14:39  车车大人  阅读(249)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3