PHP code example of regexp1987 / blake3

1. Go to this page and download the library: Download regexp1987/blake3 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/ */

    

regexp1987 / blake3 example snippets


// Default 256-bit hash
$hash = blake3("hello world");
// => "d74981efa70a0c880b8d8c1985d075dbcbf679b99a5f9914e5aaf96b831a9e24"

// Custom output length (extendable output)
$hash4 = blake3("hello", 4);
// => "f9406799"

// Raw binary output
$raw = blake3("hello", 32, null, true);
// => "\xf9\x40\x67\x99..." (32 bytes)

// Keyed mode (MAC)
$key = random_bytes(32);
$mac = blake3("message", 32, $key);

$hash = blake3_file("/var/log/syslog");
// => 64-char hex string

// Streaming — works for GB-sized files without loading into memory
$raw = blake3_file("/var/backups/db.sql.gz", 32, null, true);

// Keyed file hash (MAC)
$key = random_bytes(32);
$mac = blake3_file("/var/backups/db.sql.gz", 32, $key);

$ctx = blake3_init();
blake3_update($ctx, "chunk1");
blake3_update($ctx, "chunk2");
$hash = blake3_final($ctx);

$key = random_bytes(32);
$ctx = blake3_init($key);
blake3_update($ctx, $data);
$mac = blake3_final($ctx);

$ctx = blake3_init();
$fp = fopen("large_file.bin", "r");
while (($chunk = fread($fp, 8192)) !== false) {
    blake3_update($ctx, $chunk);
}
fclose($fp);
$hash = blake3_final($ctx);

// Generate a 64-byte digest (hex)
$hash64 = blake3_xof("data", 64);

// Raw bytes
$raw = blake3_xof("data", 64, null, true);

// Keyed
$key = random_bytes(32);
$mac = blake3_xof("data", 64, $key);

$derivedKey = blake3_derive_key('my-app v1 session-key', $masterSecret);
// => 64-char hex string (32 bytes)

// Raw bytes, custom length
$rawKey = blake3_derive_key('my-app v1 encryption-key', $masterSecret, 32, true);

echo blake3_version();
// => "1.0.0"

$ctx = blake3_init();
// $ctx is of type Blake3Context (internal resource)

$data = str_repeat("x", 1024 * 1024); // 1 MB

$start = microtime(true);
for ($i = 0; $i < 100; ++$i) {
    blake3($data);
}
printf("BLAKE3:  %.2f ms/op\n", (microtime(true) - $start) / 100 * 1000);

$start = microtime(true);
for ($i = 0; $i < 100; ++$i) {
    hash('sha256', $data);
}
printf("SHA-256: %.2f ms/op\n", (microtime(true) - $start) / 100 * 1000);

// BLAKE3("") = af1349b9f5f9a1a6a0404dea36dcc9499bcb25c9adc112b7cc9a93cae41f3262
assert(blake3("") === 'af1349b9f5f9a1a6a0404dea36dcc9499bcb25c9adc112b7cc9a93cae41f3262');

// BLAKE3("hello") = ea8f163db38682925e4491c5e58d4bb3506ef8c14eb78a86e908c5624a67200f
assert(blake3("hello") === 'ea8f163db38682925e4491c5e58d4bb3506ef8c14eb78a86e908c5624a67200f');
bash
# Install PIE (if not already available)
curl -sS https://github.com/php/pie/releases/latest/download/pie.phar -o pie.phar
chmod +x pie.phar

# Install BLAKE3 extension
php pie.phar install regexp1987/blake3

# Verify
php -r "echo blake3('hello') . PHP_EOL;"
# Output: ea8f163db38682925e4491c5e58d4bb3506ef8c14eb78a86e908c5624a67200f
bash
cd path/to/blake3

# Generate build system
phpize

# Configure
./configure

# Build (use all CPU cores)
make -j$(nproc)

# Run test suite
make test

# Install to PHP extension directory
sudo make install

# Enable the extension
echo "extension=blake3.so" | sudo tee /etc/php/8.4/mods-available/blake3.ini
sudo phpenmod blake3

# Verify
php -r "echo blake3('hello') . PHP_EOL;"
# Output: ea8f163db38682925e4491c5e58d4bb3506ef8c14eb78a86e908c5624a67200f
bat
cd path\to\blake3
phpize
configure --enable-blake3
nmake
php -r "echo blake3('hello');"