Core\Random\Seeded::token
Makes a hex text from a seeded generator, such as a fixed ID for a test user or a test order.
You make the generator with new Core\Random\Seeded(42). The number 42 is the seed. Two generators with the same seed return the same tokens, on every run. By default token draws 32 bytes, and the result is a tainted string of 64 characters. The string is always twice as long as the number of bytes, and it contains only the characters 0 to 9 and a to f.
Asking for zero bytes throws an error.
Good to know: anybody who knows the seed knows every token. For a session ID, a password reset link or an API key, use Core\Random::token. The result is tainted, so a program that passes it as a key does not compile.
Parameters
$bytesuintdefault 32How many random bytes to use,32by default. The string has twice as many characters.
Return value
Returns tainted string. A tainted string of 2 * $bytes hex digits, 0–9 and a–f. It is tainted, so a parameter that needs a key does not accept it.
Errors
RuntimeError$bytesis0, or the result is larger than a buffer this process can allocate.
Examples
The same seed gives the same token
<?nvs// Shows that two generators with the same seed return the same tokens.
Core\Random\Seeded $first = new Core\Random\Seeded(42);Core\Random\Seeded $second = new Core\Random\Seeded(42);
// 8 bytes give a token of 16 characters. Both lines are the same.echo $first->token(8), "\n";echo $second->token(8), "\n";
// The next call returns the next token, so this line is different.echo $first->token(8), "\n";
// Without an argument, `token` draws 32 bytes. This prints 64.echo Core\Str::length($first->token()), "\n";956eeb2f2632d7bd956eeb2f2632d7bd03f166b233e3ef2864Zero bytes is an error
<?nvs// Shows what happens when a program asks a seeded generator for a token of zero bytes.
Core\Random\Seeded $generator = new Core\Random\Seeded(42);
// The size here comes from a setting that was left empty.uint $size = 0;
// `$generator->token(0)` throws an error.try { echo $generator->token($size), "\n";} catch (Throwable $e) { echo 'Error: ', $e->message, "\n";}
// The error used no part of the sequence. This prints the same token as a new generator.Core\Random\Seeded $fresh = new Core\Random\Seeded(42);echo $generator->token(4) == $fresh->token(4) ? 'same' : 'different', "\n";Error: Core\Random\Seeded::token(): a token of zero bytes is the empty string, which is no sequence at all — draw at least one bytesameIds for test users
<?nvs// Shows how to give test users an ID that is the same on every run.
// A test creates three users. The seed is fixed, so each user gets the same// ID every time, and the test can look a user up by that ID.Core\Random\Seeded $generator = new Core\Random\Seeded(2024);array<string> $names = ['Ana', 'Ben', 'Chen'];array<tainted string> $ids = [];foreach ($names as string $name) { tainted string $id = 'user_' . $generator->token(6); $ids[$name] = $id; echo $name, ': ', $id, "\n";}
// The test can look Ben up by this ID, because it never changes.echo 'find ', $ids['Ben'], "\n";Ana: user_d5ec8ef8ec8fBen: user_d2f21115bb30Chen: user_7fa5db58bf7cfind user_d2f21115bb30