【问题标题】:crypt(): No salt parameter was specified. You must use a randomly generated salt and a strong hash function to produce a secure hash [duplicate]crypt():没有指定盐参数。您必须使用随机生成的盐和强哈希函数来生成安全哈希 [重复]
【发布时间】: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


【解决方案1】:

我在尝试创建访问令牌时遇到此错误

不要使用crypt()。获取paragonie/random_compat 的副本并改用random_bytes()

function getToken($randomIdLength = 10)
{
    $token = '';
    do {
        $bytes = random_bytes($randomIdLength);
        $token .= str_replace(
            ['.','/','='], 
            '',
            base64_encode($bytes)
        );
    } while (strlen($token) < $randomIdLength);
    return $token;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-04-28
    • 1970-01-01
    • 2019-03-13
    • 2015-08-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-15
    相关资源
    最近更新 更多