Skip to content

Core\Hash::Digest

Which hashing algorithm a Core\Hash call computes.

Core\Hash::Digest is an enum inside the class Core\Hash. Core\Hash::of, Core\Hash::hmac and Core\Hash::stream take it as an argument.

You pass the algorithm as an argument, so one method works for every algorithm. The list has the checksums, the two broken algorithms Md5 and Sha1, the SHA-2 and SHA-3 families, and BLAKE3. Use Sha256 when nothing else decides the choice for you.

Crc32, Md5 and Sha1 are in the list because other systems still use them. An old database column, an ETag, a package manifest or another program's signature can need one of them.

Good to know: where the choice matters for security, the types decide for you. Core\Hash::hmac takes only the ten SHA-2 and SHA-3 cases, so a broken algorithm there does not compile.

Examples

Which algorithm and what for

<?nvs
// Every algorithm you can ask for, and what each is actually for. Three of
// them are not secure and are in the list because other people's file formats
// and databases still name them.
class Digests
{
public static function purpose(Core\Hash::Digest $digest): string
{
return match ($digest) {
Core\Hash::Digest::Crc32 => 'Crc32: catches a file that got corrupted on the way, and nothing more',
Core\Hash::Digest::Crc32c => 'Crc32c: the same idea, in the spelling storage systems and network protocols use',
Core\Hash::Digest::Md5 => 'Md5: broken since 2004, kept because older formats and APIs still ask for it',
Core\Hash::Digest::Sha1 => 'Sha1: broken since 2017, kept for the same reason',
Core\Hash::Digest::Sha224 => 'Sha224: SHA-2, cut to 28 octets',
Core\Hash::Digest::Sha256 => 'Sha256: SHA-2, and the one to reach for when nothing else decides it',
Core\Hash::Digest::Sha384 => 'Sha384: SHA-2, 48 octets',
Core\Hash::Digest::Sha512 => 'Sha512: SHA-2, 64 octets',
Core\Hash::Digest::Sha512_224 => 'Sha512_224: SHA-512 cut short, which is faster than SHA-256 on a 64-bit machine',
Core\Hash::Digest::Sha512_256 => 'Sha512_256: the same, cut to 32 octets',
Core\Hash::Digest::Sha3_224 => 'Sha3_224: SHA-3, a different design to SHA-2 rather than a wider one',
Core\Hash::Digest::Sha3_256 => 'Sha3_256: SHA-3, 32 octets',
Core\Hash::Digest::Sha3_384 => 'Sha3_384: SHA-3, 48 octets',
Core\Hash::Digest::Sha3_512 => 'Sha3_512: SHA-3, 64 octets',
Core\Hash::Digest::Blake3 => 'Blake3: very fast and secure, but not one of the cases you may authenticate with here',
};
}
}
array<Core\Hash::Digest> $everything = [
Core\Hash::Digest::Crc32,
Core\Hash::Digest::Crc32c,
Core\Hash::Digest::Md5,
Core\Hash::Digest::Sha1,
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,
Core\Hash::Digest::Blake3,
];
foreach ($everything as Core\Hash::Digest $digest) {
echo Digests::purpose($digest), "\n";
}
Output
Crc32: catches a file that got corrupted on the way, and nothing more
Crc32c: the same idea, in the spelling storage systems and network protocols use
Md5: broken since 2004, kept because older formats and APIs still ask for it
Sha1: broken since 2017, kept for the same reason
Sha224: SHA-2, cut to 28 octets
Sha256: SHA-2, and the one to reach for when nothing else decides it
Sha384: SHA-2, 48 octets
Sha512: SHA-2, 64 octets
Sha512_224: SHA-512 cut short, which is faster than SHA-256 on a 64-bit machine
Sha512_256: the same, cut to 32 octets
Sha3_224: SHA-3, a different design to SHA-2 rather than a wider one
Sha3_256: SHA-3, 32 octets
Sha3_384: SHA-3, 48 octets
Sha3_512: SHA-3, 64 octets
Blake3: very fast and secure, but not one of the cases you may authenticate with here

The ones you may authenticate with

<?nvs
// Hashing a message and authenticating a message are different jobs. You can
// hash with any case of `Core\Hash::Digest`. To authenticate is to prove that a message
// came from somebody with the shared key. Only the ten SHA-2 and SHA-3 cases can do that.
// `Core\Hash::of` accepts any case, so you can still check an old MD5 checksum.
echo 'md5 of the file: ', Core\Encoding::toHex(Core\Hash::of('hello', Core\Hash::Digest::Md5)), "\n";
// `Core\Hash::hmac` needs a shared key and one of the ten cases.
// `Core\Hash::Digest::Md5` on the line below does not compile.
bytes $key = Core\Encoding::fromHex('4a656665');
bytes $tag = Core\Hash::hmac('hello', $key, Core\Hash::Digest::Sha256);
echo 'tag on the message: ', Core\Encoding::toHex($tag), "\n";
// The receiver has the same key. It computes the tag again and compares the two.
// `Core\Hash::equals` compares in constant time. Use it for anything secret.
bytes $expected = Core\Hash::hmac('hello', $key, Core\Hash::Digest::Sha256);
if (Core\Hash::equals($tag, $expected)) {
echo "the message is from whoever holds the key\n";
}
Output
md5 of the file: 5d41402abc4b2a76b9719d911017c592
tag on the message: bed3e4bb7da1795901a7d9a54531cabbe39ade5a2c89298cdc5f8889091d174d
the message is from whoever holds the key

Checking a download against its checksum

<?nvs
// A publisher gives a SHA-256 checksum with a file. This program checks that the
// downloaded file has the same checksum. Always use the algorithm the publisher names.
// The program writes the file first, so that it has a download to check.
string $path = Core\Path::join(Core\IO::createTemporaryDirectory(), 'release.tar');
Core\IO::write($path, 'the release archive');
// The checksum as the publisher wrote it. A checksum is usually written as hex,
// so `Core\Encoding::fromHex` converts it to bytes below.
string $published = '9287352f960eca8487b5cf180199b11a82306e1e6fff02b566d48a229a8ebe32';
// `Core\Hash::of` reads the open file one piece at a time.
// A download of any size needs only a small amount of memory.
Core\IO\File $file = Core\IO::open($path, Core\IO::FileMode::Read);
bytes $ours = Core\Hash::of($file, Core\Hash::Digest::Sha256);
$file->close();
bytes $theirs = Core\Encoding::fromHex($published);
if (Core\Hash::equals($ours, $theirs)) {
echo "checksum matches, the file is the one they published\n";
} else {
// The file is damaged, or it is not the file the publisher released.
// Do not install it. Print both checksums so somebody can compare them.
echo "checksum does not match\n";
echo ' they published: ', $published, "\n";
echo ' we have: ', Core\Encoding::toHex($ours), "\n";
}
Output
checksum matches, the file is the one they published