Skip to content

Core\Hash::stream

Starts a digest that you feed piece by piece, for data that arrives in parts.

Core\Hash::stream returns a Core\Hash\Stream for the algorithm you choose. You give it the data with update, one piece at a time, and you get the digest with finish. The result is the same digest that Core\Hash::of returns for all the pieces joined together. How you cut the data into pieces does not change the result.

Good to know: the stream keeps every piece until finish, so its memory grows with the data you feed it. After finish the stream is closed. For the next digest, start a new stream.

Parameters

  • $digestCore\Hash::DigestThe algorithm, any Core\Hash::Digest case.

Return value

Returns Core\Hash\Stream. A new, open stream with no data in it yet.

Examples

A text in two parts

<?nvs
// Shows how to compute a SHA-256 digest from a text that arrives in two parts.
Core\Hash\Stream $stream = Core\Hash::stream(Core\Hash::Digest::Sha256);
$stream->update('hel');
$stream->update('lo');
// This prints 2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824.
echo Core\Encoding::toHex($stream->finish()), "\n";
// `Core\Hash::of` over the whole text gives the same digest.
echo Core\Encoding::toHex(Core\Hash::of('hello', Core\Hash::Digest::Sha256)), "\n";
Output
2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824
2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824

Two algorithms at once

<?nvs
// Shows how to compute two digests of the same data with two streams.
// An older system checks the data with MD5. Your own records use SHA-256.
Core\Hash\Stream $forOldSystem = Core\Hash::stream(Core\Hash::Digest::Md5);
Core\Hash\Stream $forRecords = Core\Hash::stream(Core\Hash::Digest::Sha256);
array<string> $parts = ["invoice 2031\n", "total 49.90 EUR\n"];
foreach ($parts as string $part) {
$forOldSystem->update($part);
$forRecords->update($part);
}
// An MD5 digest has 16 bytes, so this prints 32 hex digits.
echo Core\Encoding::toHex($forOldSystem->finish()), "\n";
// A SHA-256 digest has 32 bytes, so this prints 64 hex digits.
echo Core\Encoding::toHex($forRecords->finish()), "\n";
Output
0c2899637604fed5be67ed65b7646605
8f0dfb4e3e64f0eb758b3a72693a304a0804de4cdd1baa530ed37f2e7864af3c

Check an upload sent in parts

<?nvs
// Shows how to check a file that a client uploads in several parts.
class Upload
{
// Returns `true` when the parts, joined in order, have the SHA-256 digest `$sentHex`.
public static function isComplete(array<string> $parts, string $sentHex): bool
{
Core\Hash\Stream $stream = Core\Hash::stream(Core\Hash::Digest::Sha256);
foreach ($parts as string $part) {
$stream->update($part);
}
return Core\Hash::equals($stream->finish(), Core\Encoding::fromHex($sentHex));
}
}
// Before the upload, the client sends the digest of the whole file.
string $sentHex = Core\Encoding::toHex(Core\Hash::of("name,city\nAnna,Graz\nLeo,Linz\n", Core\Hash::Digest::Sha256));
// All three parts arrived. This prints `complete`.
array<string> $parts = ["name,city\n", "Anna,Graz\n", "Leo,Linz\n"];
echo Upload::isComplete($parts, $sentHex) ? 'complete' : 'damaged', "\n";
// The last part is missing. This prints `damaged`.
array<string> $partial = ["name,city\n", "Anna,Graz\n"];
echo Upload::isComplete($partial, $sentHex) ? 'complete' : 'damaged', "\n";
Output
complete
damaged