Core\Jwe\Key::password
Core\Jwe\Key::password(secret string $password): Core\Jwe\KeyCore\Jwe\Key::password( secret string $password, ): Core\Jwe\Key
Makes a Core\Jwe\Key from a password. Use it when a person chooses the secret, for example a password that protects an exported file. Any text is a password, and no length is too short or too long.
A password is not a key, so Core\Jwe::encrypt first turns it into a key with a slow key derivation (PBKDF2). The slow step makes it expensive for an attacker to guess many passwords. A token made with this key uses the JWE algorithm PBES2-HS256+A128KW.
Only the exact same text opens the token. A password that differs in one letter, or only in upper and lower case, throws a RuntimeError in Core\Jwe::decrypt.
Every call runs the slow step again. For a secret that a program makes itself, Core\Jwe\Key::shared is much faster.
The examples below encrypt with a password, show that the case of each letter counts, and protect an exported file.
Parameters
$passwordsecret stringThe password, of any length. It can come fromCore\Cli::askSecretor a form field. It is not hashed for storage. UseCore\Passwordfor that.
Return value
Returns Core\Jwe\Key. A key that encrypt derives with the minimum iteration count. The count and the salt are written into the header, so the other side can derive the same key.
Examples
Basics
<?nvs// Shows how to make a key from a password and use it to encrypt and decrypt a text.
// `password` makes a key from any text.Core\Jwe\Key $key = Core\Jwe\Key::password('blue-river-42');string $token = Core\Jwe::encrypt('the wifi password is sunflower', $key);
// A key made from the same password opens the token.// This prints `the wifi password is sunflower`.echo Core\Jwe::decrypt($token, [Core\Jwe\Key::password('blue-river-42')]), "\n";the wifi password is sunflowerThe exact text counts
<?nvs// Shows that only the exact password opens a token. Upper and lower case letters are different.
string $token = Core\Jwe::encrypt('meeting at 10:00', Core\Jwe\Key::password('Blue-River-42'));
// This loop tries three passwords. Only the first one is exact. The last one ends with a space.// This prints `Blue-River-42: meeting at 10:00`, then `wrong` two times.array<string> $guesses = ['Blue-River-42', 'blue-river-42', 'Blue-River-42 '];foreach ($guesses as string $guess) { try { tainted string $text = Core\Jwe::decrypt($token, [Core\Jwe\Key::password($guess)]); echo $guess, ': ', $text, "\n"; } catch (RuntimeError $wrong) { echo $guess, ': wrong', "\n"; }}Blue-River-42: meeting at 10:00blue-river-42: wrongBlue-River-42 : wrongProtecting an export
<?nvs// Shows how to protect an exported list of customers with a password before you send it to a colleague.
// The export is a JSON document. In real code, the rows come from a database.string $export = Core\Json::encode([ { id: 1, name: 'Anna Berger', email: 'anna@example.com' }, { id: 2, name: 'Ben Okafor', email: 'ben@example.com' },]);
// You tell the colleague the password by phone, and send the token by email.string $token = Core\Jwe::encrypt($export, Core\Jwe\Key::password('green tea at noon'));
// The colleague opens the token with the same password. This prints the JSON document.echo Core\Jwe::decrypt($token, [Core\Jwe\Key::password('green tea at noon')]), "\n";[{"email":"anna@example.com","id":1,"name":"Anna Berger"},{"email":"ben@example.com","id":2,"name":"Ben Okafor"}]