PHP code example of tourze / blake3-php

1. Go to this page and download the library: Download tourze/blake3-php library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

tourze / blake3-php example snippets


use Tourze\Blake3\Blake3;

// Create a new hasher instance
$hasher = Blake3::newInstance();

// Update with data
$hasher->update('hello ');
$hasher->update('world');

// Get the hash (default 32 bytes)
$hash = $hasher->finalize();
echo bin2hex($hash); // Output as hex string

use Tourze\Blake3\Blake3;

// Create a 32-byte key
$key = random_bytes(32); // Use a secure random key in production

// Create a keyed hasher
$hasher = Blake3::newKeyedInstance($key);
$hasher->update('message to authenticate');
$mac = $hasher->finalize();

use Tourze\Blake3\Blake3;

// Derive keys from a context string
$hasher = Blake3::newKeyDerivationInstance('my-app 2024-01-01 user-key');
$hasher->update($inputKeyMaterial);

// Derive a 32-byte key
$derivedKey = $hasher->finalize(32);

use Tourze\Blake3\Blake3;

// Enable low memory mode for large files
$hasher = Blake3::newInstance(lowMemoryMode: true);

// Stream file in chunks
$handle = fopen('large-file.bin', 'rb');
while (!feof($handle)) {
    $chunk = fread($handle, 8192); // 8KB chunks
    $hasher->update($chunk);
}
fclose($handle);

$hash = $hasher->finalize();

use Tourze\Blake3\Blake3;

$hasher = Blake3::newInstance();
$hasher->update('data');

// Get different output lengths
$hash256bit = $hasher->finalize(32);  // 256-bit (default)
$hash512bit = $hasher->finalize(64);  // 512-bit
$hash1024bit = $hasher->finalize(128); // 1024-bit

$hasher = Blake3::newInstance(lowMemoryMode: true);

use Tourze\Blake3\Util\BufferSizeManager;

// Configure custom buffer size
$bufferManager = new BufferSizeManager();
$optimalSize = $bufferManager->getOptimalBufferSize($dataSize);

$hasher = Blake3::newInstance();

// Process data from multiple sources
$hasher->update($header);
$hasher->update($body);
$hasher->update($footer);

$finalHash = $hasher->finalize();

$password = 'user_password';
$salt = random_bytes(16);
$context = "MyApp v1.0 key derivation {$salt}";

$hasher = Blake3::newKeyDerivationInstance($context);
$hasher->update($password . $salt);
$stretchedKey = $hasher->finalize(32);

$inputs = ['data1', 'data2', 'data3'];
$results = [];

$hasher = Blake3::newInstance();
foreach ($inputs as $input) {
    $hasher->reset();
    $hasher->update($input);
    $results[] = $hasher->finalize();
}
bash
composer 
text
src/
├── Blake3.php                 # Main hasher class
├── ChunkState/               # Chunk processing logic
│   └── Blake3ChunkState.php
├── Constants/                # Algorithm constants
│   └── Blake3Constants.php
├── Exception/                # Custom exceptions
│   ├── Blake3InvalidArgumentException.php
│   └── Blake3RuntimeException.php
├── Output/                   # Output generation
│   └── Blake3Output.php
└── Util/                     # Utility functions
    ├── Blake3Util.php
    └── BufferSizeManager.php