Core\Signature::sign
Core\Signature::sign(
array<string> $payload,
{ keys: array<secret bytes>, until: ?Core\Time\Instant } $settings,
): stringCore\Signature::sign signs a set of named values with a secret key and returns a token. Later, Core\Signature::verify checks the token with the same key and returns the values. If anybody changed one character of the token, verify throws an error. This is how you make a link or a reset code that a user can carry but cannot edit.
You give sign two settings, and you must write both. keys is a list of keys, newest first, and the newest key signs. until is the time the token stops being valid, or null for a token that never expires. The time is inside the signed data, so nobody can change it.
The token is not encrypted. Anybody who has it can read the values, so never sign a password or another secret. The same values and settings always give the same token.
The examples below sign and check a token, show that the order of the names does not matter, and build a password reset link that expires after one hour.
Parameters
$payloadarray<string>The values to sign, each under a name. The order of the names is not signed.verifyreturns the values sorted by name.$settings{ keys: array<secret bytes>, until: ?Core\Time\Instant }The keys, and the time when the token stops being valid. You must write both.keysarray<secret bytes>The keys, newest first.$keys[0]signs the token. The older keys are there so thatverifystill accepts tokens made before you added a new key. A list of one key is[$key].until?Core\Time\InstantThe time after whichverifythrows an error for this token. The time is part of the signed data, so nobody can change it. Writenullfor a token that never expires.
Return value
Returns string. The token. It contains only A-Z, a-z, 0-9, - and _, so you can put it in a URL or a cookie without escaping it. It is about 4/3 × (payload + 40) characters long.
Errors
LogicError$settings.keysis empty, or its first key is not 32 bytes long.Core\Crypto::generateKey()returns a key of the right length.
Examples
Sign and check
<?nvs// Shows how to sign two values and read them back from the token.
secret bytes $key = Core\Crypto::generateKey();array<secret bytes> $keys = [$key];
// `until: null` means the token never expires.array<string> $values = ['file' => 'report.pdf', 'user' => '42'];string $token = Core\Signature::sign($values, { keys: $keys, until: null });
// The token is text that you can put in a URL.// For these two values it is always 87 characters long.echo Core\Str::length($token), "\n";
// `verify` returns the values that were signed.array<tainted string> $back = Core\Signature::verify($token, $keys);echo $back['user'], ' may download ', $back['file'], "\n";8742 may download report.pdfThe order of the names does not matter
<?nvs// Shows that `sign` gives the same token for the same values in any order.
secret bytes $key = Core\Crypto::generateKey();array<secret bytes> $keys = [$key];
// The same two values, written in two different orders.array<string> $first = ['order' => '1042', 'status' => 'paid'];array<string> $second = ['status' => 'paid', 'order' => '1042'];string $a = Core\Signature::sign($first, { keys: $keys, until: null });string $b = Core\Signature::sign($second, { keys: $keys, until: null });// This prints `same`.echo $a == $b ? 'same' : 'different', "\n";
// One changed value gives a different token. This prints `different`.array<string> $refunded = ['order' => '1042', 'status' => 'refunded'];string $c = Core\Signature::sign($refunded, { keys: $keys, until: null });echo $a == $c ? 'same' : 'different', "\n";
// `verify` returns the values sorted by name, so this prints `order status`.array<tainted string> $back = Core\Signature::verify($b, $keys);array<tainted string> $names = Core\Arr::keys($back);echo $names[0], ' ', $names[1], "\n";samedifferentorder statusA password reset link
<?nvs// Shows a password reset link that works for one hour.
// A real server loads this key from its configuration, so it stays the same between requests.secret bytes $key = Core\Crypto::generateKey();array<secret bytes> $keys = [$key];
// The link says which user may reset a password, and for what purpose.// The purpose stops somebody from using a token that was made for another feature.Core\Time\Instant $inOneHour = Core\Time::now()->plus(1h);array<string> $claims = ['purpose' => 'reset-password', 'user' => '42'];string $token = Core\Signature::sign($claims, { keys: $keys, until: $inOneHour });string $link = 'https://example.com/reset?token=' . $token;echo 'Sent a link of ', Core\Str::length($link), " characters\n";
// Later, the user opens the link and the server checks the token.// After one hour, `verify` throws a `RuntimeError`.try { array<tainted string> $back = Core\Signature::verify($token, $keys); if ($back['purpose'] == 'reset-password') { echo 'Choose a new password for user ', $back['user'], "\n"; }} catch (RuntimeError $e) { echo "This link is not valid. Ask for a new one.\n";}Sent a link of 144 charactersChoose a new password for user 42