samedi 11 mars 2017

can randomly generated string contains URI invalid characters

For several use-case in my application, I need to generate random strings, for instance, generate unique tokens to be used.

I am using PHP7 random_bytes() function, converted to an hexadecimal string using bin2hex().

Can I be sure the generated strings are URI safe, and will not contain characters such as spaces, slashes etc?

I would assume that every hexadecimal codes are only composed of characters between [0-9][a-f]...

public function generateRandomString(int $length)
{
    $length = max($length, 2);

    return bin2hex(random_bytes($length / 2));
}

Is there any case I would get an URI-invalid string with such a method?




Aucun commentaire:

Enregistrer un commentaire