I want to generate random alphanumeric strings in PHP. They will be used in places where the power of random numbers is important (public identifiers in URLs, etc.).
As I understand it, in PHP the main source of cryptographically strong randomness is openssl_random_pseudo_bytes() . However, this returns an array of bytes, not alphanumeric characters.
To convert them to alphanumeric characters, I could either haveh them (which would create a longer than necessary string of a limited set of hexadecimal characters), or base64_encode() them (which would lead to the creation of a string with + , / and = , not alphabetic characters).
So, I think that instead, I could use random bytes as a source of entropy and generate my own string consisting only of characters 0-9a-zA-Z .
Then the problem arises - how to translate from 256 separate values ββ(one byte of input) to 62 different values ββ(one character of output). And in a sense, that all 62 characters are equally likely. (Otherwise there will be 8 characters that appear more often than the rest).
Or maybe I should use a different approach? I would like my string to be as short as possible (for example, 20 characters or more - shorter URLs) and consist only of alphanumeric characters (so this does not need to be specially escaped).
Vilx- source share