Base de conhecimentoCentro de Exames
PT · PTPortuguês (Portugal)
Escolher idioma13

CKTIS / PHP

hash_hmac()

Autentica os bytes da mensagem com uma chave secreta e uma função de resumo criptográfico.

Versão: PHP 8.4; repository runtime, production runtime not attested

Sintaxe

hash_hmac(string $algo, string $data, string $key, bool $binary = false): string

Autentica os bytes da mensagem com uma chave secreta e uma função de resumo criptográfico. Devolve hexadecimal em minúsculas ou bytes em bruto com true. Em PHP 8 um algoritmo desconhecido ou não criptográfico gera ValueError. Verifique com a mesma chave e formato.

Assinatura

hash_hmac(string $algo, string $data, string $key, bool $binary = false): string

Saída do exemplo

[32,true,false]

Exemplos

<?php
$tag=hash_hmac("sha256","batch=42","example-key",true);
echo json_encode([strlen($tag),hash_equals($tag,hash_hmac("sha256","batch=42","example-key",true)),hash_equals($tag,hash_hmac("sha256","batch=43","example-key",true))]);

Resultado

[32,true,false]

Fontes