【发布时间】:2016-05-21 22:42:52
【问题描述】:
我正在使用 PHP V5.6.16
我在尝试创建访问令牌时遇到此错误
crypt(): No salt parameter was specified. You must use a randomly generated salt and a strong hash function to produce a secure hash
这是我正在使用的功能。
/**
* Generate a token
* @return string token
*/
private function getToken()
{
$random_id_length = 15;
//generate a random id encrypt it and store it in $rnd_id
$rnd_id = crypt(uniqid(rand(), CRYPT_EXT_DES));
//to remove any slashes that might have come
$rnd_id = strip_tags(stripslashes($rnd_id));
//Removing any . or / and reversing the string
$rnd_id = str_replace(".", "", $rnd_id);
$rnd_id = strrev(str_replace("/", "", $rnd_id));
//finally I take the first 10 characters from the $rnd_id
$rnd_id = substr($rnd_id, 0, $random_id_length);
return urldecode($rnd_id);
}
如何解决?
【问题讨论】:
-
我推荐使用
password_hash。 -
尝试运行
if (CRYPT_EXT_DES == 1) echo "supported",看看您是否支持CRYPT_EXT_DES -
是否有特定的原因说明您不想创建自己的盐而不使用
password_hash方法,因为password_has方法更直接。 -
"salt 参数是可选的。但是,crypt() 会创建一个没有 salt 的弱密码。PHP 5.6 或更高版本没有它会引发 E_NOTICE 错误。确保指定足够强的 salt为了更好的安全性。” – php.net/crypt
-
您实际上只是在这里拼凑了一堆随机函数,其中大部分您都在滥用。这段代码的目的是什么?生成随机字符串?有更好和更随机的方法来做到这一点......
标签: php random hash crypt php-5.6