Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / PHP

hash_equals()

Compare two byte strings using a timing-safe equality check for equal-length inputs.

Version: PHP 8.4; repository runtime, production runtime not attested

Syntax

hash_equals(string $known_string, string $user_string): bool

Compare two byte strings using a timing-safe equality check for equal-length inputs. Pass the known value first. Different lengths return false immediately and may reveal the known length. Hexadecimal text and raw digest bytes are different representations.

Signature

hash_equals(string $known_string, string $user_string): bool

Example output

[true,false,false]

Examples

<?php
$expected=hash("sha256","release");
echo json_encode([hash_equals($expected,hash("sha256","release")),hash_equals($expected,hash("sha256","draft")),hash_equals($expected,hash("sha256","release",true))]);

Result

[true,false,false]

Sources