2016-02-11 1 views
0

J'utilise PHP V5.6.16crypt(): Aucun paramètre salt n'a été spécifié. Vous devez utiliser un sel généré de façon aléatoire et une forte fonction de hachage pour produire un hachage sécurisé

Je vais avoir cette erreur en essayant de créer un jeton d'accès

crypt(): No salt parameter was specified. You must use a randomly generated salt and a strong hash function to produce a secure hash 

C'est la fonction que j'utilise.

/** 
    * 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); 
} 

Comment le réparer?

+3

Je vous recommande d'utiliser ['password_hash'] (http://php.net/manual/fr/function.password-hash.php). –

+0

essayez d'exécuter un 'si (CRYPT_EXT_DES == 1) echo" supported "' pour voir si vous avez un support pour 'CRYPT_EXT_DES' – uruloke

+0

Y at-il une raison spécifique pour laquelle vous ne voudriez pas créer votre propre sel et ne pas utiliser le' La méthode password_hash' comme la méthode 'password_has' est beaucoup plus simple. – uruloke

Répondre

3

Je vais avoir cette erreur en essayant de créer un jeton d'accès

Ne pas utiliser crypt(). Prenez une copie de paragonie/random_compat et utilisez random_bytes() à la place.

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