Files
newspage/classes/Application/Helper/NumberHash.php
2024-10-07 22:58:27 +02:00

29 lines
649 B
PHP

<?php
namespace Application\Helper;
class NumberHash
{
/**
* Return a number only hash
*
* @see https://stackoverflow.com/a/23679870/175071
*
* @param string $string string to hash
* @param null|int $length optional specific length
*
* @return int
*/
public static function numHash(string $string, ?int $length = null): int
{
$stringHash = md5($string, true);
$numberHash = unpack('N2', $stringHash);
$hash = $numberHash[1] . $numberHash[2];
if ($length) {
$hash = substr($hash, 0, $length);
}
return (int)$hash;
}
}