<?php //bk2020 cc0
// SipHash-2-4
// Aumasson and Bernstein 2012, https://131002.net/siphash/
// https://github.com/jedisct1/libsodium/tree/master/src/libsodium/crypto_shorthash
// https://github.com/php/php-src/tree/master/ext/sodium/libsodium.c#L472
function bytes_as_hex(&$str, $len=null) {
if( !$len ) { $len = strlen($str); }
// zero-based array index for unpack, kennwhite 2005:
// https://www.php.net/manual/en/function.unpack.php#45188
$arrhead = array_merge( unpack('C*', substr($str,0,$len)) );
return array_reduce( $arrhead, function($a,$b) {
return ($b>15) ? $a.dechex($b) : $a.'0'.dechex($b);
}, '' );
}
$SIPHASH = null;
if( function_exists('\sodium_crypto_shorthash') ) {
$SIPHASH = 'sodium_crypto_shorthash';
}
else if( function_exists('\Sodium\crypto_shorthash') ) {
$SIPHASH = '\Sodium\crypto_shorthash';
}
else {
die('Unable to test SipHash-2-4 using sodium_crypto_shorthash');
}
$SIPKEY = '1234567890abcdef'; // 16-byte key for SipHash-2-4
echo '# ', $SIPHASH, PHP_EOL;
// thanks to: https://3v4l.org/EsGSU
// (note: bin2hex won't print leading zero on byte values <16)
for ($i = 0; $i < 256; ++$i) {
$msg = bin2hex( chr(($i & 0x3F) | 0x80) );
$aref = hrtime();
$hsh = $SIPHASH( $msg, $SIPKEY );
$anow = hrtime();
echo $i, "\t", bytes_as_hex($hsh,8), $aref, $anow, PHP_EOL;
}
Abusive script
This script was stopped while abusing our resources
preferences:
62.58 ms | 413 KiB | 5 Q