create a blake2s hash from a string in php

To create a BLAKE2s hash from a string in PHP, you can use the hash() function with the "blake2s" algorithm:

main.php
$string = "Hello, world!";
$hash = hash("blake2s", $string);
echo $hash;
73 chars
4 lines

This will output the BLAKE2s hash of the string "Hello, world!" in hexadecimal format.

You can also specify a custom hash length and a custom key for the BLAKE2s hash:

main.php
$string = "Hello, world!";
$key = "my-secret-key";
$hashLength = 32; // in bytes
$rawOutput = true; // if true, the hash will be in raw binary format

$hash = hash("blake2s", $string, $key, $rawOutput);
$hash = substr($hash, 0, $hashLength);

echo bin2hex($hash);
264 chars
10 lines

This will output the BLAKE2s hash of the string "Hello, world!" with a length of 32 bytes and a custom key "my-secret-key". The hash will be output in hexadecimal format. Note that we use the bin2hex() function to convert the raw binary hash to hexadecimal format.

gistlibby LogSnag