Search by

nickjbedford / randomstringgenerator

nickjbedford

Provides methods to generate cryptographically strong random strings based on an input alphabet.

Package info

github.com/nickjbedford/randomstringgenerator

pkg:composer/nickjbedford/randomstringgenerator

Statistics

Installs: 2 279

Dependents: 0

Suggesters: 0

Stars: 0

Open Issues: 0

0.7.0 2026-11-26 23:39 UTC

This package is auto-updated.

Last update: 2026-11-26 23:40:03 UTC


README

Generators\RandomStringGenerator generates cryptographically strong random strings based on a supplied alphabet of characters (such as alphanumeric characters). This is perfect for generating API keys within a set of allowed characters. It can also be used to generate strong passwords.

The class relies on the random_int() function to select each character uniformly from the alphabet using PHP's cryptographically secure random number generator.

Requires PHP 8.4+ and the mbstring extension.

Usage

To generate a 16-character string using the default 62-character alphanumeric alphabet, the usage is as simple as the following:

$generator = new Generators\RandomStringGenerator();
$key = $generator->createString();
// string(16) "7m98Jvc0ArBDS4jK"

To use a different alphabet, such as uppercase hexadecimal characters, and to generate a random string of a different length, these parameters can be passed to the class as follows:

$generator = new RandomStringGenerator(RandomStringGenerator::ALPHABET_HEX_UPPER);
$key = $generator->createString(8);
// string(8) "CF70D24E"

A custom alphabet can be provided in the form of an input string of characters, such as:

$generator = new RandomStringGenerator('abc123');
$key = $generator->createString();
// string(16) "cb2c2a33c2c3a23b"

This is then converted to an array of characters internally. This array can be retrieved using the $generator->alphabet() function with no argument. An empty alphabet throws an InvalidArgumentException; passing null uses the default alphabet.

For one-off strings, the static generate() method (or the generate_random_string() helper) takes the length and alphabet directly:

$key = RandomStringGenerator::generate(8, RandomStringGenerator::ALPHABET_HEX_UPPER);
$key = generate_random_string(8, RandomStringGenerator::ALPHABET_HEX_UPPER);

Note: generate() is static and ignores any alphabet set on an instance. Calling $generator->generate(8) always uses the alphanumeric alphabet unless an alphabet is passed as the second argument. Use $generator->createString() to generate from an instance's alphabet.

Unicode alphabets

Alphabets are UTF-8 strings and are split into Unicode code points, so multibyte characters are treated as single characters. All lengths are measured in characters, not bytes.

$generator = new RandomStringGenerator('äöü߀😀');
$key = $generator->createString(8);
// mb_strlen($key) === 8

An alphabet that is not valid UTF-8 throws an InvalidArgumentException. Characters made of several code points (such as some emoji sequences or letters with separate combining accents) are split into their individual code points.

Entropy

calculateEntropyBits($length) gives a nominal guide based on the alphabet's length (log₂ of the number of characters, per character). It counts repeated alphabet characters as distinct.

To measure a specific string, calculateMinimumEntropyBits($sample) uses only the sample's own character frequencies (Shannon entropy per character, multiplied by its length). The generator's alphabet is not used.

$generator = new RandomStringGenerator(RandomStringGenerator::ALPHABET_HEX_LOWER);
$generator->calculateEntropyBits(4);                         // 16.0
RandomStringGenerator::calculateMinimumEntropyBits('abcd'); // 8.0
RandomStringGenerator::calculateMinimumEntropyBits('aaaa'); // 0.0

Composing strings with requirements

Generators\RandomStringComposer builds strings that contain a set number of characters from specific alphabets, filling the rest from a default alphabet (letters by default) and shuffling the result.

$composer = new RandomStringComposer();
$composer->requireDigits(2)->requirePunctuation(1, 0);
$password = $composer->createString(16);

$composer->satisfiesRequirements($password, 16); // true

createString() throws an InvalidArgumentException if the requirements' maximum counts add up to more than the requested length. maximumRequiredLength() returns that total.

satisfiesRequirements() checks that every character belongs to the default alphabet or a requirement alphabet, and that each requirement's count is within its minimum and (when $strictCounting is true) maximum. If a requirement's alphabet overlaps the default alphabet, characters from the default alphabet also count towards that requirement, so use $strictCounting = false for those configurations.