百科狗-知识改变命运!
--

hash_pbkdf2() - php 密码散列哈希函数

梵高1年前 (2023-11-21)阅读数 18#技术干货
文章标签密钥

hash_pbkdf2()

(PHP 5 >= 5.5.0, PHP 7)

生成所提供密码的 PBKDF2 密钥导出

说明

hash_pbkdf2(string $algo,string $password,string $salt,int $iterations[,int $length= 0[,bool $raw_output=FALSE]]): string

参数

$algo

哈希算法名称,例如md5sha256haval160,4等。受支持的算法清单请参见hash_algos()。

$password

要进行导出的密码。

$salt

进行导出时所使用的“盐”,这个值应该是随机生成的。

$iterations

进行导出时的迭代次数。

$length

密钥导出数据的长度。如果$raw_output为TRUE,此参数为密钥导出数据的字节长度。如果$raw_output为FALSE,此参数为密钥导出数据的字节长度的 2 倍,因为 1 个字节数据对应的 2 个 16 进制的字符。

如果传入0,则使用所选算法的完整输出大小。

$raw_output

hash_pbkdf2() - php 密码散列哈希函数

设置为TRUE输出原始二进制数据,设置为FALSE输出小写 16 进制字符串。

返回值

如果$raw_output设置为TRUE,则返回原始二进制数据表示的信息摘要,否则返回 16 进制小写字符串格式表示的信息摘要。

错误/异常

在以下情况下会产生E_WARNING:指定了未知的算法,$iterations小于等于0,$length小于等于0或者$salt过长(大于INT_MAX- 4)。

更新日志

版本说明
7.2.0不再支持非加密的哈希函数(adler32,crc32,crc32b,fnv132,fnv1a32,fnv164,fnv1a64,joaat)。

范例

Example #1hash_pbkdf2()例程,基础用法

以上例程的输出类似于:

120fb6cffcf8b32c43e7

注释

Caution

为了安全起见,可以使用 PBKDF2 方法对密码明文进行哈希运算后再存储。但是更好的方案是使用password_hash()函数或者使用CRYPT_BLOWFISH算法调用crypt()函数。

参见

  • crypt() 单向字符串散列
  • password_hash() 创建密码的散列(hash)
  • hash() 生成哈希值(消息摘要)
  • hash_algos() 返回已注册的哈希算法列表
  • hash_init() 初始化增量哈希运算上下文
  • hash_hmac() 使用 HMAC 方法生成带有密钥的哈希值
  • hash_hmac_file() 使用 HMAC 方法和给定文件的内容生成带密钥的哈希值
  • openssl_pbkdf2() 生成一个 PKCS5 v2 PBKDF2 字符串
Please pay great attention to the **$length** parameter! It is exactly the **return string length**, NOT the length of raw binary hash result.
I had a big problem about this -- 
I thought that `hash_pbkdf2(...false)` should equals to `bin2hex(hash_pbkdf2(...true))` just like `md5($x)` equals `bin2hex(md5($x, true))`. However I was wrong:
hash_pbkdf2('sha256', '123456', 'abc', 10000, 50, false); // returns string(50) "584bc5b41005169f1fa15177edb78d75f9846afc466a4bae05"
hash_pbkdf2('sha256', '123456', 'abc', 10000, 50, true); // returns string(50) "XKŴ\x10\5\x16\x1fQwujFjK\5\x11BFW\eYpG\x1f  mp.g2`;\x19N"
bin2hex(hash_pbkdf2('sha256', '123456', 'abc', 10000, 50, true)); // returns string(100) "584bc5b41005169f1fa15177edb78d75f9846afc466a4bae05119c82424657c81b5970471f098a6d702e6732b7603b194efe"
So I add such a note. Hope it will help someone else like me.
This is a light-weight drop-in replacement for PHP's hash_pbkdf2(); written for compatibility with older versions of PHP.
Written, formatted and tested by myself, but using code and ideas based on the following:
https://defuse.ca/php-pbkdf2.htm
https://github.com/rchouinard/hash_pbkdf2-compat/blob/master/src/hash_pbkdf2.php
https://gist.github.com/rsky/5104756
My main goals:
1) Maximum compatibility with PHP hash_pbkdf2(), ie. a drop-in replacement function
2) Minimum code size/bloat
3) Easy to copy/paste
4) No classes, and not encapsulated in a class! Why write a class when a simple function will do?
5) Eliminate calls to sprintf(). (used by other examples for the error reporting)
6) No other dependencies, ie. extra required functions

鹏仔微信 15129739599 鹏仔QQ344225443 鹏仔前端 pjxi.com 共享博客 sharedbk.com

免责声明:我们致力于保护作者版权,注重分享,当前被刊用文章因无法核实真实出处,未能及时与作者取得联系,或有版权异议的,请联系管理员,我们会立即处理! 部分文章是来自自研大数据AI进行生成,内容摘自(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供学习参考,不准确地方联系删除处理!邮箱:344225443@qq.com)

图片声明:本站部分配图来自网络。本站只作为美观性配图使用,无任何非法侵犯第三方意图,一切解释权归图片著作权方,本站不承担任何责任。如有恶意碰瓷者,必当奉陪到底严惩不贷!

内容声明:本文中引用的各种信息及资料(包括但不限于文字、数据、图表及超链接等)均来源于该信息及资料的相关主体(包括但不限于公司、媒体、协会等机构)的官方网站或公开发表的信息。部分内容参考包括:(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供参考使用,不准确地方联系删除处理!本站为非盈利性质站点,本着为中国教育事业出一份力,发布内容不收取任何费用也不接任何广告!)