Core\Hash\Stream::finish
$stream->finish(): bytesEnds a digest and returns it.
finish returns the digest of everything you gave to update, as bytes. This is the same value that Core\Hash::of returns for all the pieces joined together. After finish, the stream is closed and frees the pieces it kept.
Good to know: a stream gives its digest only once. A second finish, or an update after it, throws a RuntimeError. For the next digest, start a new stream with Core\Hash::stream.
Return value
Returns bytes. The digest as bytes. After this call the stream is finished, and its data is freed.
Errors
RuntimeErrorThe stream is already finished. A second call tofinishthrows this error.
Examples
The digest as bytes
<?nvs// Shows what `finish` returns: the digest as `bytes`.
Core\Hash\Stream $stream = Core\Hash::stream(Core\Hash::Digest::Sha512);$stream->update('hello');bytes $digest = $stream->finish();
// A SHA-512 digest has 64 bytes. This prints 64.echo Core\Bytes::length($digest), "\n";// `Core\Encoding::toHex` turns the bytes into text you can print or store.echo Core\Encoding::toHex($digest), "\n";649b71d224bd62f3785d96d46ad3ea3d73319bfbc2890caadae2dff72519673ca72323c3d99ba5c11d7c7acc6e14b8c5da0c4663475c2e5c3adef46f73bcdec043A stream gives its digest once
<?nvs// Shows that a stream is closed after `finish`, and how to start the next digest.
Core\Hash\Stream $stream = Core\Hash::stream(Core\Hash::Digest::Sha256);$stream->update('first file');echo Core\Encoding::toHex($stream->finish()), "\n";
// The stream is closed now, so a second `finish` throws a `RuntimeError`.try { $stream->finish();} catch (RuntimeError $e) { echo "closed\n";}
// For the next digest, start a new stream.$stream = Core\Hash::stream(Core\Hash::Digest::Sha256);$stream->update('second file');echo Core\Encoding::toHex($stream->finish()), "\n";bf41cf94047f1a3443ca654a235bc8f830f7997da9b6f3b2b041a866bc6e3b6fclosed54811cbc6c86311729b0a33e26c89087881b36b9ca3217d15cb5196e35f9a7e3Check a download
<?nvs// Shows how to check a download against the checksum that its website publishes.
class Download{ // Returns `true` when the parts, joined in order, have the published SHA-256 checksum. public static function isIntact(array<string> $parts, string $publishedHex): bool { Core\Hash\Stream $stream = Core\Hash::stream(Core\Hash::Digest::Sha256); foreach ($parts as string $part) { $stream->update($part); } // `finish` returns the checksum of the whole download. bytes $checksum = $stream->finish(); return Core\Hash::equals($checksum, Core\Encoding::fromHex($publishedHex)); }}
// The checksum the website publishes next to the download link.string $published = Core\Encoding::toHex(Core\Hash::of('release 2.4.1 of the tool', Core\Hash::Digest::Sha256));
// This prints `intact`.array<string> $received = ['release 2.4.1 ', 'of the tool'];echo Download::isIntact($received, $published) ? 'intact' : 'broken', "\n";
// One character changed on the way. This prints `broken`.array<string> $changed = ['release 2.4.1 ', 'of the toot'];echo Download::isIntact($changed, $published) ? 'intact' : 'broken', "\n";intactbroken