Skip to content

Core\Hash\Stream::update

$stream->update(bytes|string $data): void$stream->update(
    bytes|string $data,
): void

Adds the next piece of data to a digest that is still open.

Each call adds its data after the data from the calls before it. The order matters: the same pieces in a different order give a different digest. How you cut the data does not matter, and an empty piece changes nothing. The data can be a string or bytes.

Good to know: the stream keeps each piece until you call finish. If you pass the same text many times, it is kept only once. Calling update after finish throws a RuntimeError.

Parameters

  • $databytes|stringThe next part of the data. A string is read as its UTF-8 bytes.

Return value

Returns void. Nothing. The stream keeps a reference to $data until finish.

Errors

  • RuntimeErrorThe stream is already finished. A digest cannot change, so open a new stream.

Examples

The order of the pieces

<?nvs
// Shows that `update` adds each piece after the pieces before it.
class Pieces
{
// Returns the SHA-256 digest of the pieces, fed in the order given, as hex digits.
public static function digest(array<string> $pieces): string
{
Core\Hash\Stream $stream = Core\Hash::stream(Core\Hash::Digest::Sha256);
foreach ($pieces as string $piece) {
$stream->update($piece);
}
return Core\Encoding::toHex($stream->finish());
}
}
array<string> $inOrder = ['ab', 'cd'];
array<string> $swapped = ['cd', 'ab'];
// "ab" and then "cd" is the text "abcd". This prints `true`.
string $whole = Core\Encoding::toHex(Core\Hash::of('abcd', Core\Hash::Digest::Sha256));
echo Pieces::digest($inOrder) == $whole ? 'true' : 'false', "\n";
// The same pieces in another order are the text "cdab". This prints `false`.
echo Pieces::digest($swapped) == $whole ? 'true' : 'false', "\n";
Output
true
false

Text and bytes in one stream

<?nvs
// Shows that `update` takes both a `string` and `bytes`, and that an empty piece changes nothing.
Core\Hash\Stream $stream = Core\Hash::stream(Core\Hash::Digest::Sha256);
$stream->update('price:');
// A `string` is read as its UTF-8 bytes, so text and `bytes` can be mixed.
$stream->update('20' as bytes);
// An empty piece adds nothing to the digest.
$stream->update('');
$stream->update(' EUR');
// The digest is the same as the digest of "price:20 EUR". This prints `true`.
bytes $whole = Core\Hash::of('price:20 EUR', Core\Hash::Digest::Sha256);
echo Core\Hash::equals($stream->finish(), $whole) ? 'true' : 'false', "\n";
Output
true

A digest of a report written line by line

<?nvs
// Shows how to compute a checksum of a report while you write it, one line at a time.
class Report
{
// Prints each order as one line, and returns the SHA-256 digest of all lines as hex digits.
public static function write(array<string> $orders): string
{
Core\Hash\Stream $checksum = Core\Hash::stream(Core\Hash::Digest::Sha256);
foreach ($orders as string $order) {
string $line = 'order ' . $order . "\n";
echo $line;
// Each line goes into the checksum right after it is printed.
$checksum->update($line);
}
return Core\Encoding::toHex($checksum->finish());
}
}
array<string> $orders = ['1042', '1043', '1044'];
string $checksum = Report::write($orders);
// The receiver computes the same checksum and knows that no line is missing.
echo 'checksum: ', $checksum, "\n";
Output
order 1042
order 1043
order 1044
checksum: 181f15118bbc391639233f7309e9792e3c3023965c0057b3f24bee0e058d4167