hash_pbkdf2() - php 密码散列哈希函数
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哈希算法名称,例如md5,sha256,haval160,4等。受支持的算法清单请参见hash_algos()。
$password要进行导出的密码。
$salt进行导出时所使用的“盐”,这个值应该是随机生成的。
$iterations进行导出时的迭代次数。
$length密钥导出数据的长度。如果$raw_output为TRUE
,此参数为密钥导出数据的字节长度。如果$raw_output为FALSE
,此参数为密钥导出数据的字节长度的 2 倍,因为 1 个字节数据对应的 2 个 16 进制的字符。
如果传入0,则使用所选算法的完整输出大小。
$raw_output设置为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
图片声明:本站部分配图来自网络。本站只作为美观性配图使用,无任何非法侵犯第三方意图,一切解释权归图片著作权方,本站不承担任何责任。如有恶意碰瓷者,必当奉陪到底严惩不贷!