Skip to content
This repository has been archived by the owner on Jan 13, 2022. It is now read-only.

CSPRSG address space is very wasteful #1247

Open
kralos opened this issue Jul 26, 2021 · 1 comment
Open

CSPRSG address space is very wasteful #1247

kralos opened this issue Jul 26, 2021 · 1 comment

Comments

@kralos
Copy link

kralos commented Jul 26, 2021

Any strings yielded by the built in PseudoRandomStringGenerators only use [0-9][a-f] however the alphabet has a lot more to offer. Especially if you count uppercase...

Facebook\PseudoRandomString\PseudoRandomStringGeneratorTrait

    public function binToHex($binaryData, $length)
    {
        return \substr(\bin2hex($binaryData), 0, $length);
    }

e.g.

class RandomIntBase62CSPRSG implements \Facebook\PseudoRandomString\PseudoRandomStringGeneratorInterface
{
    /**
     * Indexing a string is faster than using \chr()
     */
    private const CHARS = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';

    public static function getPseudoRandomString(
        int $length
    ): string {
        $string = '';
        for ($i = 0; $i < $length; $i++) {
            $string .= static::CHARS[\random_int(0, 61)];
        }
        return $string;
    }
}
@JexPY
Copy link

JexPY commented Jul 26, 2021

I don't think they care anymore....

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants