Skip to content

Core\Jwe\Key::shared

Makes a Core\Jwe\Key from a secret key of 32 bytes. Use it when the program that encrypts and the program that decrypts both have the same secret key, for example one server, or two services that share one configuration.

Core\Crypto::generateKey returns a key of the right length. A key with another length throws a LogicError. The error message says the length of the key but never shows the key itself, so the message is safe to log.

A token made with this key uses the JWE algorithm dir: the secret key encrypts the text directly. This is the fastest key for Core\Jwe::encrypt and Core\Jwe::decrypt.

The examples below make a new key, read a key that is saved as text, and share one key between two services.

Parameters

  • $keysecret bytes32 bytes that both sides already have. Core\Crypto::generateKey() returns such a key.

Return value

Returns Core\Jwe\Key. A key for which encrypt writes "alg":"dir". With this key, decrypt accepts only that alg.

Errors

  • LogicError$key is not 32 bytes long, so it is not a valid key.

Examples

Basics

<?nvs
// Shows how to make a key from a secret key, and what happens when the length is wrong.
// `Core\Crypto::generateKey` returns 32 random bytes. `shared` makes a key from them.
Core\Jwe\Key $key = Core\Jwe\Key::shared(Core\Crypto::generateKey());
// This prints `hello`.
echo Core\Jwe::decrypt(Core\Jwe::encrypt('hello', $key), [$key]), "\n";
// A key of 16 bytes is too short. `shared` throws a `LogicError`.
// This prints `a key must be 32 bytes`.
try {
Core\Jwe\Key::shared(Core\Bytes::repeat(Core\Encoding::fromHex('00'), 16));
} catch (LogicError $short) {
echo 'a key must be 32 bytes', "\n";
}
Output
hello
a key must be 32 bytes

A key saved as text

<?nvs
// Shows how to make a key from text in base64url, the way a key is often saved in a configuration file.
// In real code, this text comes from the configuration. Never write a real key in the source code.
string $saved = 'BwcHBwcHBwcHBwcHBwcHBwcHBwcHBwcHBwcHBwcHBwc';
// `Core\Encoding::fromBase64Url` returns the 32 bytes of the key.
Core\Jwe\Key $key = Core\Jwe\Key::shared(Core\Encoding::fromBase64Url($saved));
// A key made again from the same text opens the token. This prints `the same key`.
string $token = Core\Jwe::encrypt('the same key', $key);
Core\Jwe\Key $again = Core\Jwe\Key::shared(Core\Encoding::fromBase64Url($saved));
echo Core\Jwe::decrypt($token, [$again]), "\n";
Output
the same key

Two services share a key

<?nvs
// Shows how a shop sends an order to a warehouse service, and only the two of them can read it.
class Shop
{
// The shop encrypts the order before it sends it.
public static function send(string $order, Core\Jwe\Key $key): string
{
return Core\Jwe::encrypt($order, $key);
}
}
class Warehouse
{
// The warehouse decrypts the order with its own copy of the same key.
public static function receive(string $message, Core\Jwe\Key $key): tainted string
{
return Core\Jwe::decrypt($message, [$key]);
}
}
// Both services read the same secret key from their configuration.
secret bytes $secret = Core\Crypto::generateKey();
string $message = Shop::send('order 1042: 3 x blue mug', Core\Jwe\Key::shared($secret));
// This prints `order 1042: 3 x blue mug`.
echo Warehouse::receive($message, Core\Jwe\Key::shared($secret)), "\n";
Output
order 1042: 3 x blue mug