A utility class for generating random strings with various character set options. Perfect for creating secure tokens, captcha codes, temporary passwords, and other randomized strings commonly used throughout web applications.
RandomString is a static utility class that provides flexible random string generation with multiple predefined character sets and the ability to define custom character pools.
Package: Subway\core\hash
Version: 0.1.1
Minimum PHP: 8.3 (requires 8.4.x)
License: CC BY-SA 4.0
Include the class in your namespace:
use Subway\core\hash\RandomString;Generates a random string with the specified length and character set.
-
$iNumOfChars(int, default:8)
The number of characters to generate in the output string. -
$aType(string, default:"alphanum")
The type of characters to use. Accepts predefined types or a custom character string.
Returns a shuffled string of the specified length containing characters from the selected character set.
| Type | Characters | Example Output |
|---|---|---|
alphanum |
a-z, A-Z, 0-9 |
abC2puwm |
alpha |
a-z, A-Z |
aBcDeFgH |
chars |
a-z, A-Z (alias for alpha) |
XyZaBcDe |
hex |
0-9, A-F (hexadecimal uppercase) |
A1B2C3D4 |
hex-lower |
0-9, a-f (hexadecimal lowercase) |
a1b2c3d4 |
lower |
a-z only |
abcdefgh |
num |
0-9 only |
0898124 |
pass |
a-z, A-Z, 0-9, _, -, . (password-safe) |
aB3_c-De. |
Pass any custom string or array of characters:
// Custom string
RandomString::generate(10, 'Aldus'); // Result: sAdludsAls
// Custom array
RandomString::generate(10, ['A', 'l', 'd', 'u', 's']); // Result: dAldusAldA$token = RandomString::generate();
// Result: 'abC2puwm' (8 alphanumeric characters)$shortCode = RandomString::generate(5);
// Result: 'abc56' (5 alphanumeric characters)
$longToken = RandomString::generate(32);
// Result: 'aBcD1eFgH2iJkL3mNoPqR4sTuVwXyZ5ab' (32 characters)$numericCode = RandomString::generate(8, 'num');
// Result: '0898124' (8 digits)$hexColor = RandomString::generate(6, 'hex-lower');
// Result: 'afb2c2' (lowercase hex)
$hexToken = RandomString::generate(16, 'hex');
// Result: 'A1B2C3D4E5F6A7B8' (uppercase hex)$password = RandomString::generate(16, 'pass');
// Result: 'aB3_c-De.fGh4iJk' (includes letters, numbers, and special chars)$captcha = RandomString::generate(6, 'alpha');
// Result: 'aBcDeF' (letters only)$diceRoll = RandomString::generate(5, '123456');
// Result: '34251' (only digits 1-6)
$customToken = RandomString::generate(10, 'ACGT');
// Result: 'ATCGATCGAT' (DNA bases)$resetToken = RandomString::generate(32, 'alphanum');$captchaCode = RandomString::generate(6, 'alpha');$tempPassword = RandomString::generate(12, 'pass');$sessionId = RandomString::generate(32, 'hex-lower');$apiKey = RandomString::generate(32, 'alphanum');If the requested number of characters exceeds the size of the character set, the salt is automatically repeated:
$result = RandomString::generate(100, 'alpha');
// Generates 100 letters (salt repeated internally)The $aType parameter is case-insensitive:
RandomString::generate(10, 'ALPHANUM'); // Works
RandomString::generate(10, 'alphanum'); // Works
RandomString::generate(10, 'AlphaNum'); // WorksEach call produces a new random string due to str_shuffle(). Multiple calls will produce different results:
$token1 = RandomString::generate();
$token2 = RandomString::generate();
// $token1 !== $token2alphanum and alpha types is missing the letter 'd':
alphanumsalt:abcefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890alphasalt:abcefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ
This appears to be intentional but may warrant review.
- The method uses
str_shuffle(), which is suitable for most use cases - For cryptographic security requirements, consider using
random_bytes()andbin2hex()instead - Generation is fast even for large strings (1000+ characters)
This class is suitable for:
- Non-security-critical random strings (CAPTCHA codes, form tokens)
- Session identifiers with other security measures
- Randomized UI elements
This class is NOT suitable for:
- Cryptographic keys
- Security tokens without additional entropy
- Password generation (use dedicated password generation libraries)
For cryptographically secure random generation, use:
$randomBytes = random_bytes(16);
$token = bin2hex($randomBytes);| Version | Changes |
|---|---|
| 0.1.1 | Initial release |
Creative Commons Attribution-ShareAlike 4.0 International (CC BY-SA 4.0)
See: https://creativecommons.org/licenses/by-sa/4.0/
Kant (Aldus)
- Subway Project
- WBCE CMS 1.6.x Compatibility