64,648
社区成员
发帖
与我相关
我的任务
分享
<?php
function hash_password($password, $salt = FALSE, $h_byte_size = FALSE)
{
$hash_algos = array(
128 => 'sha512',
64 => 'sha256',
40 => 'sha1',
32 => 'md5'
);
// Even for md5, collisions usually happen above 1024 bits, so
// we artifically limit their password to reasonable size.
if ( ! $password OR strlen($password) > 250)
{
return FALSE;
}
// No hash function specified? Use the best one
// we have access to in this environment.
if ($h_byte_size === FALSE)
{
reset($hash_algos);
$h_byte_size = key($hash_algos);
}
elseif ( ! isset($hash_algos[$h_byte_size]))
{
// What are they feeding us? This can happen if
// they move servers and the new environment is
// less secure. Nothing we can do but fail. Hard.
die('Fatal Error: No matching hash algorithm.');
}
// No salt? (not even blank), we'll regenerate
if ($salt === FALSE)
{
$salt = '';
// The salt should never be displayed, so any
// visible ascii character is fair game.
for ($i = 0; $i< $h_byte_size; $i++)
{
$salt .= chr(mt_rand(33, 126));
}
}
elseif (strlen($salt) !== $h_byte_size)
{
// they passed us a salt that isn't the right length,
// can happen if old code resets a new password
// ignore it
$salt = '';
}
return array(
'salt' => $salt,
'password' => hash($hash_algos[$h_byte_size], $salt.$password)
);
}
?>
c++没有提供sha512,sha256,sha1,md5这几个算法直接可用的函数,要转换需要其它库的支持