Today I learned that "password" tends to mean a memorizable string of an arbitrary number of characters, while "key" means a highly random string of bits (of a specific length based on the encryption algorithm used).
And so today I first heard of the concept of a Key derivation function.
I'm confused about how to derive a 32-byte key from a password of arbitrary length (in PHP).
The following approach works but ignores the instruction of "[The salt] should be generated randomly" (so does Sodium):
$salt = 'this salt remains constant';
$iterations = 10;
$length = 32;
$aesKey = hash_pbkdf2('sha256', $somePasswordOfArbitraryLength, $salt, $iterations, $length, false);
The following approach also works but doesn't quite feel right either:
$hash = password_hash($somePasswordOfArbitraryLength, PASSWORD_BCRYPT, ['cost' => $iterations]);
$aesKey = substr($hash, -$length);//this smells weird
With all of my searching, I'm surprised I haven't found an "official" way in PHP to derive a 32-byte key from a password deterministically.
How should I do it?
P.S. I'm using Laravel in PHP and want to use AES-256-CBC encryption like this:
$encrypter = new \Illuminate\Encryption\Encrypter($aesKey, 'AES-256-CBC');
$encryptedText = $encrypter->encrypt($text);
Laravel's encryption helper (e.g. Crypt::encryptString('Hello world.')
) seems unfit for my requirements since I want each user's data to be encrypted separately based on each individual's password.
Whatever key derivation function I use needs produce the same key every time since I'll be using symmetric encryption to then decrypt strings that that user had encrypted with that key.
P.P.S. Thanks to questions 1, 2, and 3 for introducing certain concepts to me.