hash门面bcrypt()方法是一种强大的密码哈希方式。它可以防止恶意用户破解使用bcrypt()生成的密码。
the hashing details are available inside config/hashing.php. the default driver has bcrypt() as the hashing to be used.
hashing passwords要使用hash facade,您需要包含以下类:
illuminate\support\facades\hash
example要对密码进行哈希处理,您可以使用make()方法。以下是一个哈希密码的示例
<?phpnamespace app\http\controllers;use illuminate\http\request;use app\models\student;use illuminate\support\facades\hash;class studentcontroller extends controller { public function index() { echo $hashed = hash::make('password', [ 'rounds' => 15, ]); }}
outputthe output of the above code is
$2y$15$qkyqhdkcdssmmixzmwyf/.sihzqdhxtgf5wniy4fdocnm6livihzi
verifying if the password matches with a hashed password要验证明文文本即hash::make中使用的文本是否与哈希值匹配,可以使用check()方法。
如果纯文本与哈希密码匹配,check()方法返回true,否则返回false。
<?phpnamespace app\http\controllers;use illuminate\http\request;use app\models\student;use illuminate\support\facades\hash;class studentcontroller extends controller { public function index() { $hashed = hash::make('password', [ 'rounds' => 15, ]); if (hash::check('password', $hashed)) { echo password matching; } else { echo password is not matching; } }}
outputthe output of the above code is
password matching
使用check()方法让我们现在通过提供错误的纯文本来测试,并查看 check() 方法的响应。
<?phpnamespace app\http\controllers;use illuminate\http\request;use app\models\student;use illuminate\support\facades\hash;class studentcontroller extends controller { public function index() { $hashed = hash::make('password', [ 'rounds' => 15, ]); if (hash::check('password123', $hashed)) { echo password matching; } else { echo password is not matching; } }}
我们在哈希中使用的纯文本是“password”。在check方法中,我们使用了“password123”,因为文本与哈希文本不匹配,所以输出为“密码不匹配”。
output当您在浏览器中执行时,输出将是 -
password is not matching
对密码进行两次哈希let us now hash the same text twice and compare it in the check() method −
$testhash1 = hash::make('mypassword');$testhash2 = hash::make('mypassword'); if (hash::check('mypassword', $testhash1) && hash::check('mypassword', $testhash2)) { echo password matching;} else { echo password not matching;}
you can test the complete code in the browser as shown below −
<?phpnamespace app\http\controllers;use illuminate\http\request;use app\models\student;use illuminate\support\facades\hash;class studentcontroller extends controller { public function index() { $testhash1 = hash::make('mypassword'); $testhash2 = hash::make('mypassword'); if (hash::check('mypassword', $testhash1) && hash::check('mypassword', $testhash2)) { echo password matching; } else { echo password not matching; } }}
output上述代码的输出为 −
password matching
使用bcrypt()方法you can also try using the bcrypt() method and test the plain text with hashed one using hash::check().
<?phpnamespace app\http\controllers;use illuminate\http\request;use app\models\student;use illuminate\support\facades\hash;class studentcontroller extends controller { public function index() { $hashedtext = bcrypt('mypassword'); if (hash::check('mypassword', $hashedtext)) { echo 'password matches'; } else{ echo 'password not matching'; } }}
output上述代码的输出为 -
password matches
以上就是在laravel中如何比较两个加密(bcrypt)密码?的详细内容。
