sodium_crypto_xof_shake256

sodium_crypto_xof_shake256(int $length, string $message): string

SHAKE256 extendable-output function (new in PHP 8.6, Sodium): squeeze any output length on demand from the same input; length comes first.

C C23

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

int main(void) {
    /* C stdlib has no SHA-3/SHAKE — a fixed-output SHA-256 digest shown for shape */
    BYTE hash[SHA256_BLOCK_SIZE];
    SHA256_CTX ctx;
    sha256_init(&ctx);
    sha256_update(&ctx, (const BYTE *)"abc", strlen("abc"));
    sha256_final(&ctx, hash);
    for (int i = 0; i < SHA256_BLOCK_SIZE; i++) printf("%02x", hash[i]);
    printf("\n");
    /* This is a plain fixed-length digest, NOT an XOF — variable-length squeeze
       needs a third-party library (libsodium or the XKCP reference implementation) */
    return 0;
}
Output
ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad

C++ C++26

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

int main() {
    // C++ stdlib has no SHA-3/SHAKE either — fixed-output SHA-256 shown for shape
    BYTE hash[SHA256_BLOCK_SIZE];
    SHA256_CTX ctx;
    sha256_init(&ctx);
    sha256_update(&ctx, (const BYTE *)"abc", strlen("abc"));
    sha256_final(&ctx, hash);
    for (int i = 0; i < SHA256_BLOCK_SIZE; i++) printf("%02x", hash[i]);
    printf("\n");
    // Fixed-length digest, NOT an XOF — libsodium/XKCP needed for variable-length squeeze
    return 0;
}
Output
ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad

C# v11.0.100

v11.0.100
// No SHAKE in the BCL; SHA3_256 (.NET 8+) is fixed-output too and unsupported on macOS.
// SHA-256 is the portable fixed-output stand-in shown here — NOT an XOF (BouncyCastle covers SHAKE).
Console.WriteLine(Convert.ToHexString(System.Security.Cryptography.SHA256.HashData("abc"u8)).ToLower());
// Fixed 32-byte digest — asking the same input for a longer squeeze is impossible by design
Console.WriteLine(System.Security.Cryptography.SHA256.HashData("abc"u8).Length);
Output
ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad
32

Go v1.27.4

v1.27.4
package main

import (
	"encoding/hex"
	"fmt"

	"golang.org/x/crypto/sha3"
)

func main() {
	// golang.org/x/crypto/sha3: Shake256 doubles as an io.Reader — every Read squeezes more output
	h := sha3.NewShake256()
	h.Write([]byte("abc"))
	out := make([]byte, 32)
	h.Read(out)
	fmt.Println(hex.EncodeToString(out))
	// Same input, longer squeeze: the 32-byte hex above is a prefix of this 64-byte one
	h2 := sha3.NewShake256()
	h2.Write([]byte("abc"))
	out2 := make([]byte, 64)
	h2.Read(out2)
	fmt.Println(hex.EncodeToString(out2)[:64])
}
Output
483366601360a8771c6863080cc4114d8db44530f8f1e1ee4f94ea37e78b5739
483366601360a8771c6863080cc4114d8db44530f8f1e1ee4f94ea37e78b5739

Java v27.0.1

v27.0.1
import java.security.MessageDigest;

public class Main {
    public static void main(String[] args) throws Exception {
        // JCA has no SHAKE (XOF) — BouncyCastle provides it; SHA3-256 (Java 16+) is the
        // closest stdlib cousin: a fixed-output digest, not a true XOF
        MessageDigest md = MessageDigest.getInstance("SHA3-256");
        byte[] d = md.digest("abc".getBytes());
        StringBuilder sb = new StringBuilder();
        for (byte b : d) sb.append(String.format("%02x", b));
        System.out.println(sb);
    }
}
Output
3a985da74fe225b2045c172d6bd390bd855f086e3e9d525b46bfe24511431532

JavaScript v26.13.1

v26.13.1
// Node crypto: SHAKE256 native — outputLength is a constructor option (DEP0198 pushes this form)
const crypto = require("node:crypto");
console.log(crypto.createHash("shake256", { outputLength: 32 }).update("abc").digest("hex"));
console.log(crypto.createHash("shake256", { outputLength: 8 }).update("php8.6").digest("hex"));
// Longer squeeze extends the same stream — prefixes match (XOF signature property)
console.log(
  crypto.createHash("shake256", { outputLength: 64 }).update("abc").digest("hex")
    .startsWith(crypto.createHash("shake256", { outputLength: 32 }).update("abc").digest("hex")),
);
Output
483366601360a8771c6863080cc4114d8db44530f8f1e1ee4f94ea37e78b5739
7292a4b1ef193382
true

PHP v8.6.0

v8.6.0
// SHAKE256 XOF (FIPS 202): one sponge, arbitrary-length squeeze — the output
// length is a caller parameter. PHP core hash() ships no XOF (no shake256 in
// hash_algos()) and the Sodium entry points need libsodium >= 1.0.22 at runtime,
// so this block prints normative facts; byte-identical squeezes run live in the
// Python/Node/Go/Rust blocks of this entry
echo "SHAKE256: output length is a caller parameter (FIPS 202)\n";
echo "prefix property: a longer squeeze extends the same stream\n";
echo "PHP 8.6 family: sodium_crypto_xof_shake256 + sodium_crypto_xof_shake256_init/update/squeeze\n";
echo "faster cousin: sodium_crypto_xof_turboshake256 + sodium_crypto_xof_turboshake256_init/update/squeeze\n";
Output
SHAKE256: output length is a caller parameter (FIPS 202)
prefix property: a longer squeeze extends the same stream
PHP 8.6 family: sodium_crypto_xof_shake256 + sodium_crypto_xof_shake256_init/update/squeeze
faster cousin: sodium_crypto_xof_turboshake256 + sodium_crypto_xof_turboshake256_init/update/squeeze

Python v3.15.2

v3.15.2
import hashlib

# hashlib.shake_256: SHAKE256 XOF built in — hexdigest takes the desired length
print(hashlib.shake_256(b"abc").hexdigest(32))
print(hashlib.shake_256(b"php8.6").hexdigest(8))
# Longer squeeze extends the same stream — prefixes match (XOF signature property)
print(hashlib.shake_256(b"abc").hexdigest(64).startswith(hashlib.shake_256(b"abc").hexdigest(32)))
Output
483366601360a8771c6863080cc4114d8db44530f8f1e1ee4f94ea37e78b5739
7292a4b1ef193382
True

Rust v1.99.1

v1.99.1
use sha3::digest::{ExtendableOutput, Update, XofReader};

fn main() {
    // sha3 crate: Shake256 — finalize_xof hands back a reader; every read squeezes more
    let mut xof = sha3::Shake256::default();
    xof.update(b"abc");
    let mut reader = xof.finalize_xof();
    let mut out = [0u8; 32];
    reader.read(&mut out);
    println!("{}", out.iter().map(|b| format!("{:02x}", b)).collect::<String>());
    // Same standardized XOF: byte-identical to the PHP/Python/Node/Go squeezes (FIPS 202)
}
Output
483366601360a8771c6863080cc4114d8db44530f8f1e1ee4f94ea37e78b5739

TypeScript v26.13.1

v26.13.1
import { createHash } from "node:crypto";

// Node crypto: SHAKE256 native — outputLength is a constructor option
console.log(createHash("shake256", { outputLength: 32 }).update("abc").digest("hex"));
console.log(createHash("shake256", { outputLength: 8 }).update("php8.6").digest("hex"));
// Longer squeeze extends the same stream — prefixes match (XOF signature property)
console.log(
  createHash("shake256", { outputLength: 64 }).update("abc").digest("hex")
    .startsWith(createHash("shake256", { outputLength: 32 }).update("abc").digest("hex")),
);
Output
483366601360a8771c6863080cc4114d8db44530f8f1e1ee4f94ea37e78b5739
7292a4b1ef193382
true

Language differences

The essential difference between an XOF and a fixed-length hash: a fixed hash's output size is pinned by the algorithm (SHA-256 is always 32 bytes), while an XOF hands "how much" to the caller — one sponge, keep squeezing (prefix property, cross-checked in several languages), and FIPS 202 standardization makes PHP/Python/Node/Go/Rust byte-identical.

C: no SHA-3/SHAKE in the stdlib (the example shows the fixed-output shape with vendored SHA-256); an XOF needs libsodium or the XKCP reference implementation.

C++: same as C (vendored SHA-256); libsodium/XKCP provide SHAKE.

C#: no SHAKE in the BCL; SHA3_256 (.NET 8+) is fixed-output too and unsupported on macOS (the example approximates with SHA-256) — real SHAKE needs BouncyCastle.

Go: nothing in the stdlib — golang.org/x/crypto/sha3's Shake256 is a true XOF; a hash.Hash doubling as io.Reader, every Read squeezes more (prefix check in the example).

Java: no SHAKE in JCA — SHA3-256 (Java 16+) is the closest stdlib cousin but fixed-output (example); SHAKE needs BouncyCastle.

JavaScript: Node crypto has it natively — outputLength is a constructor option (the positional digest length is deprecated, DEP0198; the example uses the options form).

PHP: sodium_crypto_xof_shake256 (new in 8.6, Sodium; needs libsodium >= 1.0.22 at runtime) — the length comes first (length <= 0 throws SodiumException); PHP core hash() ships no XOF (no shake256 in hash_algos()); the block prints normative facts (independent of the libsodium version), with the byte-identical digest genuinely computed in the Python/Node/Go/Rust blocks (locally verified: first 32 bytes of SHAKE256('abc') = 483366601360a8771c6863080cc4114d8db44530f8f1e1ee4f94ea37e78b5739).

Python: hashlib.shake_256 built in (example) — hexdigest(length) takes the desired length; the stdlib ships a native XOF.

Rust: the sha3 crate's Shake256 (example) — finalize_xof returns a reader; read = squeeze.

TypeScript: same as JavaScript (Node crypto).

Family index (all 16 XOF functions new in 8.6; the init/update/squeeze streaming usage matches each one-shot function's semantics): the SHAKE family, four pieces — sodium_crypto_xof_shake128 / sodium_crypto_xof_shake256 (one-shot) alongside sodium_crypto_xof_shake128_init, sodium_crypto_xof_shake128_update, sodium_crypto_xof_shake128_squeeze and sodium_crypto_xof_shake256_init, sodium_crypto_xof_shake256_update, sodium_crypto_xof_shake256_squeeze (streaming trios); the TurboSHAKE family (same Keccak sponge with halved rounds, several times faster — a companion variant outside FIPS 202), also four pieces — sodium_crypto_xof_turboshake128 / sodium_crypto_xof_turboshake256 (one-shot) alongside sodium_crypto_xof_turboshake128_init, sodium_crypto_xof_turboshake128_update, sodium_crypto_xof_turboshake128_squeeze and sodium_crypto_xof_turboshake256_init, sodium_crypto_xof_turboshake256_update, sodium_crypto_xof_turboshake256_squeeze.