php:bcrypt加密和验证(php 8.1)

一,相关文档:

https://www.php.net/manual/zh/function.password-hash.php

二,php代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
/*
 *测试用bcrypt方式验证密码
 *用password_hash 和 password_verify一对函数实现
 * */
public function testPass() {
    //原始密码
    $origPass = "123456";
    echo "origPass:".$origPass.":<br/>";
 
    $bcryptPass = password_hash($origPass, PASSWORD_DEFAULT);
    echo "password:".$bcryptPass.":<br/>";
 
    //判断密码是否匹配
    if (password_verify($origPass,$bcryptPass)) {
        echo "密码正确";
    } else {
        echo "密码错误";
    }
    echo "<br/>";
    if (password_verify("123321",$bcryptPass)) {
        echo "密码正确";
    } else {
        echo "密码错误";
    }
}

说明:刘宏缔的架构森林—专注it技术的博客,
网站:https://blog.imgtouch.com
原文: https://blog.imgtouch.com/index.php/2023/11/01/php-bcrypt-jia-mi-he-yan-zheng/
代码: https://github.com/liuhongdi/ 或 https://gitee.com/liuhongdi
说明:作者:刘宏缔 邮箱: 371125307@qq.com

三,测试效果:

四,查看php的版本:

liuhongdi@lhdpc:/data/laravel/dignews$ php --version
PHP 8.1.1 (cli) (built: Dec 20 2021 16:12:16) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.1.1, Copyright (c) Zend Technologies
posted @ 2023-11-01 12:20  刘宏缔的架构森林  阅读(89)  评论(0编辑  收藏  举报