hash_hkdf
(PHP 7 >= 7.1.2)
hash_hkdf — Generate a HKDF key derivation of a supplied key input
Description
$algo
, string $ikm
[, int $length
= 0
[, string $info
= ''
[, string $salt
= ''
]]] ) : stringParameters
-
algo
-
Name of selected hashing algorithm (i.e. "sha256", "sha512", "haval160,4", etc..) See hash_algos() for a list of supported algorithms.
Note:
Non-cryptographic hash functions are not allowed.
-
ikm
-
Input keying material (raw binary). Cannot be empty.
-
length
-
Desired output length in bytes. Cannot be greater than 255 times the chosen hash function size.
If
length
is 0, the output length will default to the chosen hash function size. -
info
-
Application/context-specific info string.
-
salt
-
Salt to use during derivation.
While optional, adding random salt significantly improves the strength of HKDF.
Return Values
Returns a string containing a raw binary representation of the derived key
(also known as output keying material - OKM); or FALSE
on failure.
Errors/Exceptions
An E_WARNING
will be raised if ikm
is empty, algo
is unknown/non-cryptographic,
length
is less than 0 or too large
(greater than 255 times the size of the hash function).
Examples
Example #1 hash_hkdf() example
<?php
// Generate a random key, and salt to strengthen it during derivation.
$inputKey = random_bytes(32);
$salt = random_bytes(16);
// Derive a pair of separate keys, using the same input created above.
$encryptionKey = hash_hkdf('sha256', $inputKey, 32, 'aes-256-encryption', $salt);
$authenticationKey = hash_hkdf('sha256', $inputKey, 32, 'sha-256-authentication', $salt);
var_dump($encryptionKey !== $authenticationKey); // bool(true)
?>
The above example produces a pair of separate keys, suitable for creation of an encrypt-then-HMAC construct, using AES-256 and SHA-256 for encryption and authentication respectively.
See Also
- hash_pbkdf2() - Generate a PBKDF2 key derivation of a supplied password
- » RFC 5869
- » userland implementation
English translation
You have asked to visit this site in English. For now, only the interface is translated, but not all the content yet.If you want to help me in translations, your contribution is welcome. All you need to do is register on the site, and send me a message asking me to add you to the group of translators, which will give you the opportunity to translate the pages you want. A link at the bottom of each translated page indicates that you are the translator, and has a link to your profile.
Thank you in advance.
Document created the 30/01/2003, last modified the 26/10/2018
Source of the printed document:https://www.gaudry.be/en/php-rf-function.hash-hkdf.html
The infobrol is a personal site whose content is my sole responsibility. The text is available under CreativeCommons license (BY-NC-SA). More info on the terms of use and the author.
References
These references and links indicate documents consulted during the writing of this page, or which may provide additional information, but the authors of these sources can not be held responsible for the content of this page.
The author This site is solely responsible for the way in which the various concepts, and the freedoms that are taken with the reference works, are presented here. Remember that you must cross multiple source information to reduce the risk of errors.