hash_update

hash_update(HashContext $context, string $data): bool

Pushes data into a streaming hash context, allowing chunked feeding of large files without memory overhead.

C C23

C23
#include <stdio.h>
#include "sha256.h"

int main(void) {
    // The streaming three-step: init → update (repeatable) → final (read big files in chunks)
    SHA256_CTX ctx;
    BYTE hash[SHA256_BLOCK_SIZE];
    char hex[SHA256_BLOCK_SIZE * 2 + 1];
    sha256_init(&ctx);
    sha256_update(&ctx, (const BYTE *)"a", 1);
    sha256_update(&ctx, (const BYTE *)"bc", 2);
    sha256_final(&ctx, hash);
    for (int i = 0; i < SHA256_BLOCK_SIZE; i++) {
        sprintf(hex + i * 2, "%02x", hash[i]);
    }
    printf("%s\n", hex);
    return 0;
}
Output
ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad

C++ C++26

C++26
#include <cstdio>
#include "sha256.h"

int main() {
    // The streaming three-step: init → update (repeatable) → final (read big files in chunks)
    SHA256_CTX ctx;
    BYTE hash[SHA256_BLOCK_SIZE];
    char hex[SHA256_BLOCK_SIZE * 2 + 1];
    sha256_init(&ctx);
    sha256_update(&ctx, (const BYTE *)"a", 1);
    sha256_update(&ctx, (const BYTE *)"bc", 2);
    sha256_final(&ctx, hash);
    for (int i = 0; i < SHA256_BLOCK_SIZE; i++) {
        sprintf(hex + i * 2, "%02x", hash[i]);
    }
    printf("%s\n", hex);
    return 0;
}
Output
ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad

C# v11.0.100

v11.0.100
using System.Security.Cryptography;
using System.Text;

// Streaming: IncrementalHash accepts AppendData repeatedly (chunk-feeding for big files)
using var h = IncrementalHash.CreateHash(HashAlgorithmName.SHA256);
h.AppendData(Encoding.UTF8.GetBytes("a"));
h.AppendData(Encoding.UTF8.GetBytes("bc"));
string digest = Convert.ToHexString(h.GetHashAndReset()).ToLowerInvariant();
Console.WriteLine(digest);
using var oneShot = IncrementalHash.CreateHash(HashAlgorithmName.SHA256);
oneShot.AppendData(Encoding.UTF8.GetBytes("abc"));
Console.WriteLine(
    digest == Convert.ToHexString(oneShot.GetHashAndReset()).ToLowerInvariant()
        ? "same" : "diff");
Output
ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad
same

Go v1.27.4

v1.27.4
package main

import (
	"crypto/sha256"
	"fmt"
)

func main() {
	// Streaming: hash.Hash implements io.Writer, so Write can be split across calls (block-by-block for big files)
	h := sha256.New()
	h.Write([]byte("a"))
	h.Write([]byte("bc"))
	digest := fmt.Sprintf("%x", h.Sum(nil))
	fmt.Println(digest)
	fmt.Println(digest == fmt.Sprintf("%x", sha256.Sum256([]byte("abc"))))
}
Output
ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad
true

Java v27.0.1

v27.0.1
import java.security.MessageDigest;
import java.util.HexFormat;

public class Main {
    public static void main(String[] args) throws Exception {
        // Streaming: update can be called repeatedly (DigestInputStream wraps file streams)
        MessageDigest md = MessageDigest.getInstance("SHA-256");
        md.update("a".getBytes());
        md.update("bc".getBytes());
        String digest = HexFormat.of().formatHex(md.digest());
        System.out.println(digest);
        MessageDigest oneShot = MessageDigest.getInstance("SHA-256");
        System.out.println(digest.equals(
                HexFormat.of().formatHex(oneShot.digest("abc".getBytes())))
                ? "same" : "diff");
    }
}
Output
ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad
same

JavaScript v26.13.1

v26.13.1
const crypto = require("crypto");
// Streaming: update chains across multiple calls
const h = crypto.createHash("sha256");
h.update("a");
h.update("bc");
const digest = h.digest("hex");
console.log(digest);
console.log(
  digest === crypto.createHash("sha256").update("abc").digest("hex")
    ? "same"
    : "diff"
);
Output
ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad
same

PHP v8.6.0

v8.6.0
// The streaming hash three-step: init → update (repeatable) → final
$ctx = hash_init("sha256");
hash_update($ctx, "a");
hash_update($ctx, "bc");
$digest = hash_final($ctx, false);
echo $digest, "\n";
// The chunked result matches a one-shot hash() exactly
echo $digest === hash("sha256", "abc") ? "same" : "diff";
Output
ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad
same

Python v3.15.2

v3.15.2
import hashlib

# Streaming: update can be called repeatedly, equivalent to one-shot feeding
h = hashlib.sha256()
h.update(b"a")
h.update(b"bc")
digest = h.hexdigest()
print(digest)
print("same" if digest == hashlib.sha256(b"abc").hexdigest() else "diff")
Output
ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad
same

Rust v1.99.1

v1.99.1
use sha2::{Digest, Sha256};

fn main() {
    // Streaming: the Digest trait's update accepts repeated calls (chunk-reading for big files)
    let mut h = Sha256::new();
    h.update(b"a");
    h.update(b"bc");
    let digest: String = h.finalize().iter().map(|b| format!("{:02x}", b)).collect();
    println!("{digest}");
    println!("{}", digest == Sha256::digest(b"abc").iter().map(|b| format!("{:02x}", b)).collect::<String>());
}
Output
ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad
true

TypeScript v26.13.1

v26.13.1
import * as crypto from "crypto";
// Streaming: update chains across multiple calls
const h = crypto.createHash("sha256");
h.update("a");
h.update("bc");
const digest: string = h.digest("hex");
console.log(digest);
console.log(
  digest === crypto.createHash("sha256").update("abc").digest("hex")
    ? "same"
    : "diff"
);
Output
ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad
same

Language differences

C: the vendored Brad Conte sha256 (#include "sha256.h") is itself a streaming API — the sha256_init/sha256_update/sha256_final trio; large files are fed with fread chunk by chunk.

C++: same as C's vendor sha256 trio; with OpenSSL the names match (SHA256_Init/Update/Final).

C#: IncrementalHash.AppendData × N plus GetHashAndReset; the older HashAlgorithm.TransformBlock is deprecated.

Go: hash.Hash implements io.Writer; the idiomatic large-file pattern is io.Copy(h, file) — stream into the hash, then Sum.

Java: MessageDigest.update is repeatable; the standard library ships DigestInputStream, which wraps any input stream as read-while-hashing.

JavaScript: chained createHash().update(); with a ReadStream's data events you update chunk by chunk (Node).

PHP: the hash_init → hash_update → hash_final trio; the difference from one-shot hash() is memory — one-shot loads the whole file into a string, streaming feeds it block by block, so multi-GB files hash in constant memory (with fopen + an fread loop).

Python: hashlib objects accept repeated update; pair with open("rb") chunked reads (iter(lambda: f.read(8192), b"")).

Rust: the sha2 crate (RustCrypto) implements the Digest trait — call update repeatedly then finalize; stream files with std::io::Read in chunks.

TypeScript: same as JavaScript's chained update.