Hi

On 6/25/24 18:03, Rowan Tommins [IMSoP] wrote:
Then again, if you _actually_ want it to be unique, rather than random,
those aren't the right replacements anyway.

They are for all intents and purposes if the generated string is long enough. By the pigeonhole principle you can't guarantee uniqueness for a fixed-length string, but when you have 128 bits of entropy you are statistically all but guaranteed to receive a unique string. I've made an example calculation for the "session.sid_length and session.sid_bits_per_character" bit of this very RFC.

The replacement I suggested to Marco `bin2hex(random_bytes(16))` does use exactly 128 bits (16 bytes) of secure randomness for that reason.

For Randomizer::getBytesFromString() you can calculate the entropy as follows:

    var_dump(log(strlen($string) ** $length, 2));

You can calculate the minimum length to have 128 bits of entropy for a given alphabet string as follows:

    var_dump(ceil(log(2**128, strlen($string))));

For some example alphabets, the minimum length for 128 bits of entropy would be:

- [0-9]      : 39
- [0-9a-f]   : 32
- [a-z]      : 28
- [a-z0-9]   : 25
- [a-zA-Z]   : 23
- [a-zA-Z0-9]: 22

Best regards
Tim Düsterhus

Reply via email to