30 protected static ?
string $algo =
null;
42 $algorithm = self::$algo;
43 if ( $algorithm !==
null ) {
47 $algos = hash_hmac_algos();
48 $preference = [
'whirlpool',
'sha256' ];
50 foreach ( $preference as $algorithm ) {
51 if ( in_array( $algorithm, $algos,
true ) ) {
52 self::$algo = $algorithm;
57 throw new DomainException(
'Could not find an acceptable hashing function.' );
69 self::$hashLength ??= strlen( self::hash(
'',
true ) );
73 return $raw ? self::$hashLength : self::$hashLength * 2;
84 public static function hash( $data, $raw =
true ) {
85 return hash( self::hashAlgo(), $data, $raw );
97 public static function hmac( $data, $key, $raw =
true ) {
98 if ( !is_string( $key ) ) {
100 throw new InvalidArgumentException(
'Invalid key type: ' . gettype( $key ) );
102 return hash_hmac( self::hashAlgo(), $data, $key, $raw );
static hashAlgo()
Decide on the best acceptable hash algorithm we have available for hash()
static hashLength( $raw=true)
Return the byte-length output of the hash algorithm we are using in self::hash and self::hmac.
static int $hashLength
The number of bytes outputted by the hash algorithm.
static string $algo
The hash algorithm being used.
static hmac( $data, $key, $raw=true)
Generate a keyed cryptographic hash value (HMAC) for a string, making use of the best hash algorithm ...
static hash( $data, $raw=true)
Generate a cryptographic hash value (message digest) for a string, making use of the best hash algori...