18: Extend Hash Output with SHAKE and TurboSHAKE
Overview
Section titled “Overview”Generate digests at a length chosen by your application. An extendable-output function can produce any requested output length within the API’s limits.
Prerequisites
Section titled “Prerequisites”- PHP 8.6 CLI. Confirm the version with
php --version. - The
sodiumextension with SHAKE and TurboSHAKE support. - Review the previous chapter if you’re following the series in order.
Allow about 10 minutes for the walkthrough and exercise.
What You’ll Build
Section titled “What You’ll Build”A runnable sodium-xof.php example.
Step 1: Run the example (~5 min)
Section titled “Step 1: Run the example (~5 min)”Run the following from the root of the Code with PHP repository.
php code/php-8-6/18-sodium-xof/sodium-xof.php<?php
declare(strict_types=1);
$data = 'order-42';$digest = sodium_crypto_xof_shake128(32, $data);
$state = sodium_crypto_xof_shake128_init();sodium_crypto_xof_shake128_update($state, 'order-');sodium_crypto_xof_shake128_update($state, '42');echo 'SHAKE128 chunks agree: ', hash_equals($digest, sodium_crypto_xof_shake128_squeeze($state, 32)) ? 'yes' : 'no', PHP_EOL;
echo 'SHAKE256 bytes: ', strlen(sodium_crypto_xof_shake256(32, $data)), PHP_EOL;echo 'TurboSHAKE128 bytes: ', strlen(sodium_crypto_xof_turboshake128(16, $data)), PHP_EOL;echo 'TurboSHAKE256 bytes: ', strlen(sodium_crypto_xof_turboshake256(64, $data)), PHP_EOL;Expected Result
Section titled “Expected Result”SHAKE128 chunks agree: yesSHAKE256 bytes: 32TurboSHAKE128 bytes: 16TurboSHAKE256 bytes: 64Why It Works
Section titled “Why It Works”SHAKE and TurboSHAKE return raw bytes, so use bin2hex() for textual storage. The stateful API accepts chunks through update() and then reads output through squeeze(). Pick the algorithm and output length required by the protocol or file format and keep them fixed for comparisons.
Exercise
Section titled “Exercise”Change the requested output to 16 bytes in both SHAKE128 calls. Confirm that they still agree and that the hex representation has 32 characters.
Wrap-up
Section titled “Wrap-up”Continue with Resume a TLS Stream Session.