Skip to content

Core\Hash::equals

Checks whether two digests or signatures are the same, and takes the same time wherever they differ.

Use it every time you compare a secret value, such as a signature, a token or a digest, with the value a visitor sent. A normal comparison stops at the first byte that differs. An attacker can measure that time and guess a correct signature one byte at a time. Core\Hash::equals always reads every byte, so the time shows nothing. Two values of different lengths return false at once, because the length of a digest is never a secret.

In plain words: a normal comparison is like a guard who says "wrong" as soon as the first letter of the password is wrong. This one always listens to the whole password first.

Good to know: both values must be bytes.

Parameters

  • $abytesOne digest.
  • $bbytesThe other digest.

Return value

Returns bool. true when the two have the same bytes, and false otherwise. Two values of different lengths return false at once. The length of a digest is not a secret.

Examples

Two digests

<?nvs
// Shows how to check whether two digests are the same.
bytes $first = Core\Hash::of('report.pdf', Core\Hash::Digest::Sha256);
bytes $again = Core\Hash::of('report.pdf', Core\Hash::Digest::Sha256);
bytes $other = Core\Hash::of('report.PDF', Core\Hash::Digest::Sha256);
// The same text gives the same digest. This prints `true`.
echo Core\Hash::equals($first, $again) ? 'true' : 'false', "\n";
// One changed letter gives another digest. This prints `false`.
echo Core\Hash::equals($first, $other) ? 'true' : 'false', "\n";
Output
true
false

Different lengths

<?nvs
// Shows that two values of different lengths are never equal.
bytes $short = Core\Hash::of('abc', Core\Hash::Digest::Sha256);
bytes $long = Core\Hash::of('abc', Core\Hash::Digest::Sha512);
// This prints 32 and 64.
echo Core\Bytes::length($short), ' and ', Core\Bytes::length($long), "\n";
// The lengths differ, so the result is `false`.
echo Core\Hash::equals($short, $long) ? 'true' : 'false', "\n";
// An empty value is equal only to another empty value. This prints `true`, then `false`.
echo Core\Hash::equals('' as bytes, '' as bytes) ? 'true' : 'false', "\n";
echo Core\Hash::equals('' as bytes, $short) ? 'true' : 'false', "\n";
Output
32 and 64
false
true
false

Check an api token

<?nvs
// Shows how to check an API token that a client sends with each request.
class ApiTokens
{
// The server stores only the SHA-256 digest of each token, never the token itself.
private static function storedDigest(): bytes
{
return Core\Hash::of('tok_live_4f9a2c', Core\Hash::Digest::Sha256);
}
// Returns `true` when `$sent` is the right token.
public static function isValid(string $sent): bool
{
bytes $sentDigest = Core\Hash::of($sent, Core\Hash::Digest::Sha256);
return Core\Hash::equals(ApiTokens::storedDigest(), $sentDigest);
}
}
// This prints `allowed`, then `denied` twice.
array<string> $attempts = ['tok_live_4f9a2c', 'tok_live_4f9a2d', ''];
foreach ($attempts as string $token) {
echo ApiTokens::isValid($token) ? 'allowed' : 'denied', "\n";
}
Output
allowed
denied
denied