Core\Hash::hmac
Core\Hash::hmac(
Core\Bytes::Source|string|Core\Bytes::Chunks $data,
secret bytes $key,
Core\Hash::Digest::Sha224|Core\Hash::Digest::Sha256|Core\Hash::Digest::Sha384|Core\Hash::Digest::Sha512|Core\Hash::Digest::Sha512_224|Core\Hash::Digest::Sha512_256|Core\Hash::Digest::Sha3_224|Core\Hash::Digest::Sha3_256|Core\Hash::Digest::Sha3_384|Core\Hash::Digest::Sha3_512 $digest,
): bytesComputes a signature for some data with a secret key, so you can later check that nobody changed the data.
This is called an HMAC. Only somebody who has the same key can compute the same signature. You send or store the data together with its signature. When the data comes back, you compute the signature again and compare the two with Core\Hash::equals. The result is bytes. Use Core\Encoding::toHex or Core\Encoding::toBase64Url to put it in a URL or a header.
The data can be a string, bytes, an open file, an uploaded file, Core\Request::bodyStream(), or a list or loop that gives bytes pieces. A file or a stream is read one piece at a time.
In plain words: a digest is a fingerprint that anybody can compute. An HMAC is a fingerprint that only the owner of the key can compute.
Good to know: only the SHA-2 and SHA-3 algorithms are allowed. Core\Hash::Digest::Md5 or Core\Hash::Digest::Sha1 here does not compile.
Parameters
$dataCore\Bytes::Source|string|Core\Bytes::ChunksThe data to sign:bytes, astring, an openCore\IO\File, an uploaded file, the request body, or the pieces of a loop. Astringis read as its UTF-8 bytes. A file is read one piece at a time.$keysecret bytesThe secret key, of any length. A long key is hashed first, and a short key is padded with zero bytes, as RFC 2104 § 2 says.$digestCore\Hash::Digest::Sha224|Core\Hash::Digest::Sha256|Core\Hash::Digest::Sha384|Core\Hash::Digest::Sha512|Core\Hash::Digest::Sha512_224|Core\Hash::Digest::Sha512_256|Core\Hash::Digest::Sha3_224|Core\Hash::Digest::Sha3_256|Core\Hash::Digest::Sha3_384|Core\Hash::Digest::Sha3_512The algorithm, one of the ten SHA-2 and SHA-3Core\Hash::Digestcases.
Return value
Returns bytes. The MAC as bytes, with the length of the chosen algorithm.
Examples
Sign a message
<?nvs// Shows how to compute the signature of a message with a secret key.
// `as bytes` converts the text "Jefe" into the bytes of the key.bytes $key = 'Jefe' as bytes;
// This is a published test value, so you can check the output.// This prints 5bdcc146bf60754e6a042426089575c75a003f089d2739839dec58b964ec3843.bytes $signature = Core\Hash::hmac('what do ya want for nothing?', $key, Core\Hash::Digest::Sha256);echo Core\Encoding::toHex($signature), "\n";
// A different key gives a completely different signature for the same message.bytes $otherKey = 'Joe' as bytes;echo Core\Encoding::toHex(Core\Hash::hmac('what do ya want for nothing?', $otherKey, Core\Hash::Digest::Sha256)), "\n";5bdcc146bf60754e6a042426089575c75a003f089d2739839dec58b964ec3843ab7d1f0991fab76610a4ff2b156d411ae6ea40829f2ab143d5184bbdd903680eA link that cannot be changed
<?nvs// Shows how to sign a download link, so a visitor cannot change the file name in it.
class SignedLink{ // A real program reads this key from a secret setting. It never writes it in the code. private static function key(): bytes { return 'a key only the server knows' as bytes; }
// Returns the signature of `$path` as text that is safe to put in a URL. public static function sign(string $path): string { bytes $signature = Core\Hash::hmac($path, SignedLink::key(), Core\Hash::Digest::Sha256); return Core\Encoding::toBase64Url($signature); }
// Returns `true` when `$signature` belongs to `$path`. public static function check(string $path, string $signature): bool { bytes $expected = Core\Hash::hmac($path, SignedLink::key(), Core\Hash::Digest::Sha256); return Core\Hash::equals($expected, Core\Encoding::fromBase64Url($signature)); }}
string $signature = SignedLink::sign('/download?file=invoice-17.pdf');echo '/download?file=invoice-17.pdf&sig=', $signature, "\n";
// The link as the server made it. This prints `true`.echo SignedLink::check('/download?file=invoice-17.pdf', $signature) ? 'true' : 'false', "\n";// A visitor changed 17 to 18. The signature no longer matches, so this prints `false`.echo SignedLink::check('/download?file=invoice-18.pdf', $signature) ? 'true' : 'false', "\n";/download?file=invoice-17.pdf&sig=xSujWFUSOTOoZH1QnygX4-rgiXfdqT_73kQjZaDihbMtruefalseCheck a webhook
<?nvs// Shows how to check that a webhook message really comes from the service that sent it.
class Webhook{ // Returns `true` when `$signatureHex` is the SHA-256 signature of `$body` under `$secret`. public static function isGenuine(string $body, string $signatureHex, bytes $secret): bool { bytes $expected = Core\Hash::hmac($body, $secret, Core\Hash::Digest::Sha256); // `Core\Hash::equals` takes the same time wherever the two signatures differ. // An attacker cannot guess the signature one byte at a time by measuring it. return Core\Hash::equals($expected, Core\Encoding::fromHex($signatureHex)); }}
// The payment service and the shop both know this secret.bytes $secret = 'whsec_shop_payments' as bytes;string $body = '{"order":1042,"status":"paid"}';
// The service sends the signature in a header. Here it is computed in the same way.string $header = Core\Encoding::toHex(Core\Hash::hmac($body, $secret, Core\Hash::Digest::Sha256));echo 'signature: ', $header, "\n";
// The message as the service sent it. This prints `accepted`.echo Webhook::isGenuine($body, $header, $secret) ? 'accepted' : 'rejected', "\n";
// Somebody changed the status on the way. This prints `rejected`.string $forged = '{"order":1042,"status":"refunded"}';echo Webhook::isGenuine($forged, $header, $secret) ? 'accepted' : 'rejected', "\n";signature: 4a7abd7115cbbcab0388feb8a93f517e0229c0d89bc9d5908d0b8e7e5a9fc736acceptedrejected