<?php
define('testtime', 50000);
$algos = hash_algos(); //返回一个数值索引的数组, 包含了受支持的哈希算法名称。
//遍历算法,每个算法计算50000次的时间
foreach ($algos as $algo) {
$st = microtime(true);
for ($i = 0; $i < testtime; $i++) {
hash($algo, microtime(true) . $i);
}
$et = microtime(true);
$time[$algo] = $et - $st;
}
asort($time, SORT_NUMERIC);
echo "";
print_r($time);
//CI框架建不用MD_5和SHA1加密密码,php本身由一个专门来加密密码的函数:
echo password_hash("rasmuslerdorf", PASSWORD_DEFAULT) . "\n";
//输出结果类似: $2y$10$duGmwKVqHf7sgulBOd1gb.cIikNHx3dtmlsRg9yx/YvOelJSkPmpC
//PASSWORD_DEFAULT - 使用 bcrypt 算法 (PHP 5.5.0 默认)。 注意,该常量会随着 PHP 加入更新更高强度的算法而改变。 所以,使用此常量生成结果的长度将在未来有变化。 因此,数据库里储存结果的列可超过60个字符(最好是255个字符)。
输出结果如下:
Array
(
[crc32b] => 0.031200170516968
[sha1] => 0.045201063156128
[adler32] => 0.046799898147583
[crc32] => 0.046799898147583
[fnv1a64] => 0.046800136566162
[fnv1a32] => 0.046801090240479
[md5] => 0.055202007293701
[md4] => 0.062399864196777
[fnv164] => 0.062399864196777
[fnv132] => 0.062399864196777
[joaat] => 0.062399864196777
[ripemd160] => 0.062399864196777
[sha256] => 0.062399864196777
[ripemd256] => 0.062400102615356
[tiger128,3] => 0.062400102615356
[tiger128,4] => 0.062400102615356
[tiger160,4] => 0.062400817871094
[tiger192,4] => 0.064400196075439
[tiger192,3] => 0.065399885177612
[tiger160,3] => 0.066400051116943
[ripemd128] => 0.068401098251343
[haval192,3] => 0.073400974273682
[sha224] => 0.078000068664551
[ripemd320] => 0.079999923706055
[haval256,3] => 0.085000991821289
[haval160,3] => 0.089000940322876
[haval128,4] => 0.089999914169312
[haval224,4] => 0.093599796295166
[haval192,5] => 0.093600034713745
[haval192,4] => 0.093600034713745
[haval256,4] => 0.094600200653076
[haval256,5] => 0.094600200653076
[haval160,4] => 0.095601081848145
[haval128,3] => 0.096001148223877
[haval224,3] => 0.1030011177063
[haval128,5] => 0.10920000076294
[whirlpool] => 0.10920095443726
[haval160,5] => 0.11120080947876
[haval224,5] => 0.12479996681213
[sha384] => 0.14040017127991
[sha512] => 0.14339995384216
[gost-crypto] => 0.14740014076233
[gost] => 0.15600109100342
[snefru256] => 0.25960087776184
[snefru] => 0.26519989967346
[md2] => 0.36480093002319
)
password_verify
— 验证密码是否和哈希匹配,时序攻击(timing attacks)
对此函数不起作用。 注意 password_hash()
返回的哈希包含了算法、 cost 和盐值。 因此,所有需要的信息都包含内。使得验证函数不需要储存额外盐值等信息即可验证哈希。
boolean password_verify ( string $password , string $hash )
<?php
// 想知道以下字符从哪里来,可参见 password_hash() 的例子
$hash = '$2y$07$BCryptRequires22Chrcte/VlQH0piJtjXl.0t1XkA8pw9dMXTpOq';
if (password_verify('rasmuslerdorf', $hash)) {
echo '密码正确!';
} else {
echo '密码错误!';
}