PHP 加密 Password Hashing API基础知识点

2025-05-27 0 83

PHP 5.5 之后引入 Password hashing API 用于创建和校验哈希密码,它属于内核自带,无需进行任何扩展安装和配置。它主要提供了四个函数以供使用:

● password_hash():创建密码的哈希;

● password_verify():验证密码是否和哈希匹配;

● password_needs_rehash():检查给定的哈希是否匹配给定的选项;

● password_get_info():返回指定哈希的相关信息。

1、password_hash(string password, int algo [, array options])

使用足够强度的单向散列算法生成密码的哈希。此函数兼容 crypt(),即由 crypt() 生成的哈希值可以使用 Password hashing API 的相关函数进行校验。

● password:用户密码。

● algo:密码算法常量。取值包括:

● PASSWORD_DEFAULT:使用 bcrypt 算法。最终生成的结果可能超过 60 个字符;

● PASSWORD_BCRYPT:使用 CRYPT_BLOWFISH 算法创建哈希。最终结果是 60 个字符的字符串,或在失败时返回 FALSE。

● salt:手动提供哈希密码的盐值。省略此项时,函数会为每个密码哈希自动生成随机的盐值。PHP 7.0 已废弃该项;

● cost:代表算法使用的 cost。默认值是 10,可根据实际情况增加。

2、password_verify(string password, string hash)

● password:用户提供的密码。

● hash:由 password_hash() 创建的哈希散列值。 如果匹配则返回 TRUE,否则返回 FALSE。时序攻击对此函数不起作用。

3、password_needs_rehash(string hash, integer algo [, array opitons])

● hash:由 password_hash() 生成的哈希;

● algo:密码算法常量;

● options:包含有关选项的关联数组。

4、password_get_info(string hash) hash:由 password_hash() 生成的哈希。 返回一个包含三个元素的关联数组:

● algo:密码算法常量;

● algoName:算法名称;

● options:调用 password_hash() 时提供的选项。

示例

?

1

2

3

4

5

6

7

8
$str = 'chicken,run!';

$pwd1 = password_hash($str, PASSWORD_BCRYPT);

$pwd2 = crypt($str);

var_dump(password_verify('chicken,run!', $pwd1)); // 输出 true

var_dump(password_verify('chicken,ran!', $pwd1)); // 输出 false

var_dump(password_verify($str, $pwd2)); // 输出 true

var_dump(password_needs_rehash($pwd1, PASSWORD_BCRYPT, ['cost'=>10])); // 输出 false,因为 password_hash() 在加密时,出来默认 cost 为 10 外,还会指定随机的盐值

知识点补充:

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

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54
<?php

/**

*功能:对字符串进行加密处理

*参数一:需要加密的内容

*参数二:密钥

*/

function passport_encrypt($str,$key){ //加密函数

srand((double)microtime() * 1000000);

$encrypt_key=md5(rand(0, 32000));

$ctr=0;

$tmp='';

for($i=0;$i<strlen($str);$i++){

$ctr=$ctr==strlen($encrypt_key)?0:$ctr;

$tmp.=$encrypt_key[$ctr].($str[$i] ^ $encrypt_key[$ctr++]);

}

return base64_encode(passport_key($tmp,$key));

}

/**

*功能:对字符串进行解密处理

*参数一:需要解密的密文

*参数二:密钥

*/

function passport_decrypt($str,$key){ //解密函数

$str=passport_key(base64_decode($str),$key);

$tmp='';

for($i=0;$i<strlen($str);$i++){

$md5=$str[$i];

$tmp.=$str[++$i] ^ $md5;

}

return $tmp;

}

/**

*辅助函数

*/

function passport_key($str,$encrypt_key){

$encrypt_key=md5($encrypt_key);

$ctr=0;

$tmp='';

for($i=0;$i<strlen($str);$i++){

$ctr=$ctr==strlen($encrypt_key)?0:$ctr;

$tmp.=$str[$i] ^ $encrypt_key[$ctr++];

}

return $tmp;

}

$str='作者:WWW.JB51.NET;

$key='jb51net';

$encrypt=passport_encrypt($str,$key);

$decrypt=passport_decrypt($encrypt,$key);

echo '原文:',$str."<br><hr>";

echo '密文:',$encrypt."<br><hr>";

echo '译文:',$decrypt."<br><hr>";

?>

以上就是PHP 加密 Password Hashing API基础知识点的详细内容,更多关于PHP 加密:Password Hashing API的资料请关注快网idc其它相关文章!

原文链接:https://www.php.cn/php-weizijiaocheng-443832.html

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

快网idc优惠网 建站教程 PHP 加密 Password Hashing API基础知识点 https://www.kuaiidc.com/70503.html

相关文章

猜你喜欢
发表评论
暂无评论