Skip to content

Core\Hash::of

Computes a digest of some data: a short, fixed-size fingerprint that changes completely when the data changes by even one byte.

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 is read from its first byte, one piece at a time. So you pass a large file directly, and it is never in memory as a whole.

You choose the algorithm with a Core\Hash::Digest case, such as Core\Hash::Digest::Sha256. The same data and the same algorithm always give the same digest, on every computer. The result is bytes, not text. To show it or store it as text, convert it with Core\Encoding::toHex.

In plain words: a digest is like a fingerprint for data. Two files with the same fingerprint are the same file. You cannot rebuild the file from the fingerprint.

Good to know: Md5, Sha1 and the two Crc32 cases are there so you can talk to older systems. Do not use them to protect anything. To store passwords, use Core\Password.

Parameters

  • $dataCore\Bytes::Source|string|Core\Bytes::ChunksThe data to hash: bytes, a string, an open Core\IO\File, an uploaded file, the request body, or the pieces of a loop. A string is read as its UTF-8 bytes. A file is read one piece at a time, so a large file is never held in memory.
  • $digestCore\Hash::DigestThe algorithm. Every Core\Hash::Digest case is allowed, also the broken ones.

Return value

Returns bytes. The digest as bytes, with the length of the chosen algorithm. Every algorithm accepts any input, also an empty one.

Examples

The digest of a word

<?nvs
// Shows how to compute the SHA-256 digest of a short text.
// `Core\Hash::of` returns 32 bytes. `Core\Encoding::toHex` turns them into 64 hex digits.
bytes $digest = Core\Hash::of('hello', Core\Hash::Digest::Sha256);
// This prints 2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824.
echo Core\Encoding::toHex($digest), "\n";
// One changed letter gives a completely different digest.
echo Core\Encoding::toHex(Core\Hash::of('hellO', Core\Hash::Digest::Sha256)), "\n";
Output
2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824
04a6f55face2f46be8c23f627d539827615851e10751b63ec59db6d2c706b770

One text under four algorithms

<?nvs
// Shows that the algorithm is an argument, so the same call works for every algorithm.
array<Core\Hash::Digest> $algorithms = [
Core\Hash::Digest::Crc32,
Core\Hash::Digest::Md5,
Core\Hash::Digest::Sha256,
Core\Hash::Digest::Sha512,
];
// Each algorithm gives a digest of its own fixed size.
// This prints 4, 16, 32 and 64 bytes.
foreach ($algorithms as Core\Hash::Digest $digest) {
bytes $result = Core\Hash::of('abc', $digest);
echo Core\Bytes::length($result), ' bytes: ', Core\Encoding::toHex($result), "\n";
}
Output
4 bytes: 352441c2
16 bytes: 900150983cd24fb0d6963f7d28e17f72
32 bytes: ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad
64 bytes: ddaf35a193617abacc417349ae20413112e6fa4e89a97ea20a9eeee64b55d39a2192992a274fc1a836ba3c23a3feebbd454d4423643ce80e2a9ac94fa54ca49f

A version tag for a page

<?nvs
// Shows how to make a version tag for a page, so a browser can tell when the page has changed.
class PageVersion
{
// Returns the first 16 hex digits of the page's SHA-256 digest.
public static function of(string $body): string
{
string $hex = Core\Encoding::toHex(Core\Hash::of($body, Core\Hash::Digest::Sha256));
return Core\Str::slice($hex, 0, 16);
}
}
string $before = PageVersion::of('<h1>Price: 20 EUR</h1>');
string $same = PageVersion::of('<h1>Price: 20 EUR</h1>');
string $after = PageVersion::of('<h1>Price: 25 EUR</h1>');
// The same page always gets the same tag. This prints `true`.
echo $before == $same ? 'true' : 'false', "\n";
// A changed page gets a new tag, so the browser loads it again. This prints `true`.
echo $before != $after ? 'true' : 'false', "\n";
echo $before, "\n", $after, "\n";
Output
true
true
5fd3a3a64351a745
38b6084844ea3dc9