Skip to content

Core\Crypto::deriveKey

Turns a password into a key, by putting it through a slow calculation many times over.

A password is short and people reuse it, so it is never used as a key directly. This member stretches it instead. You give it three things: the password, a salt, and how many rounds to run. The salt is at least 16 bytes, drawn once per password with Core\Random::bytes(16). It is not a secret, and it is stored next to the key it produced. The number of rounds is between 100,000 and 2,000,000, and you have to name it, because a key you want to read back later must be produced with the same number you chose the first time.

The same password, salt and number of rounds always give the same key.

Good to know: this is the member for a value a person typed. For material that is already random, such as a shared secret, use Core\Crypto::expandKey.

Parameters

  • $passwordsecret stringThe password. A secret value is allowed, and it is not revealed. The result is secret bytes and contains no byte of the password.
  • $saltbytesAt least 16 bytes. Make a new salt for each password, and store it next to the derived key. Core\Random::bytes(16) makes one. The salt is not a secret.
  • $iterationsuintHow many rounds to compute, from 100,000 to 2,000,000. It is required and has no default. To derive the same key again later, you must use the same count as before.

Return value

Returns secret bytes. 32 bytes as secret bytes, the key size that seal and open use. The result is not random: the same three arguments always give the same key.

Errors

  • LogicError$iterations is less than 100,000 or more than 2,000,000, or $salt is shorter than 16 bytes. With fewer rounds, the key is easy to attack. With more rounds, one call uses so much CPU time that it blocks the process.

Examples

Turn a password into a key

<?nvs
// Shows how to make a key from a password and use it to seal a note.
secret string $password = 'correct horse battery staple';
// The salt is drawn once and stored next to what the key protects.
bytes $salt = Core\Random::bytes(16);
secret bytes $key = Core\Crypto::deriveKey($password, $salt, 100000);
bytes $sealed = Core\Crypto::seal(
'the spare key is under the mat' as bytes,
$key,
Core\Crypto::Cipher::XChaCha20Poly1305,
);
// The same password, salt and number of rounds give the same key back, so the
// note opens again. This prints the note.
secret bytes $again = Core\Crypto::deriveKey($password, $salt, 100000);
echo Core\Crypto::open($sealed, $again, Core\Crypto::Cipher::XChaCha20Poly1305) as string, "\n";
Output
the spare key is under the mat

One password two salts

<?nvs
// Shows what the salt is for. Two people picked the same password. Each gets
// their own salt, so each ends up with a different key.
secret string $password = 'hunter2';
bytes $firstSalt = Core\Random::bytes(16);
bytes $secondSalt = Core\Random::bytes(16);
secret bytes $first = Core\Crypto::deriveKey($password, $firstSalt, 100000);
secret bytes $second = Core\Crypto::deriveKey($password, $secondSalt, 100000);
// This prints `two keys from one password: yes`.
echo 'two keys from one password: ', ($first != $second ? 'yes' : 'no'), "\n";
// The number of rounds changes the key as well, so it is stored with the salt.
secret bytes $slower = Core\Crypto::deriveKey($password, $firstSalt, 200000);
echo 'more rounds, another key: ', ($first != $slower ? 'yes' : 'no'), "\n";
Output
two keys from one password: yes
more rounds, another key: yes

Store a backup under a passphrase

<?nvs
// Shows how a program stores something under a passphrase and opens it again
// later. The salt and the number of rounds are stored next to the sealed bytes.
// All three are needed to make the same key a second time.
secret string $passphrase = 'a long sentence only I know';
bytes $salt = Core\Random::bytes(16);
uint $rounds = 200000;
secret bytes $key = Core\Crypto::deriveKey($passphrase, $salt, $rounds);
bytes $sealed = Core\Crypto::seal(
'account: 12345, pin: 6789' as bytes,
$key,
Core\Crypto::Cipher::XChaCha20Poly1305,
);
// These three go into the file together. The passphrase is the only part that
// stays in the person's head.
string $storedSalt = Core\Encoding::toHex($salt);
uint $storedRounds = $rounds;
string $storedBackup = Core\Encoding::toHex($sealed);
// Later, the person types the passphrase again and the file is read back.
secret bytes $opened = Core\Crypto::deriveKey(
$passphrase,
Core\Encoding::fromHex($storedSalt),
$storedRounds,
);
echo Core\Crypto::open(
Core\Encoding::fromHex($storedBackup),
$opened,
Core\Crypto::Cipher::XChaCha20Poly1305,
) as string, "\n";
Output
account: 12345, pin: 6789