17protectedstatic ?
string$algo =
null;
29 $algorithm = self::$algo;
30if ( $algorithm !==
null ) {
34 $algos = hash_hmac_algos();
35 $preference = [
'whirlpool',
'sha256' ];
37foreach ( $preference as $algorithm ) {
38if ( in_array( $algorithm, $algos,
true ) ) {
39 self::$algo = $algorithm;
44thrownew DomainException(
'Could not find an acceptable hashing function.' );
56 self::$hashLength ??= strlen( self::hash(
'',
true ) );
57// Optimisation: Skip computing the length of non-raw hashes. 58// The algos in hashAlgo() all produce a digest that is a multiple 59// of 8 bits, where hex is always twice the length of binary byte length. 60return $raw ? self::$hashLength : self::$hashLength * 2;
71publicstaticfunctionhash( $data, $raw =
true ) {
72returnhash( self::hashAlgo(), $data, $raw );
84publicstaticfunctionhmac( $data, $key, $raw =
true ) {
85if ( !is_string( $key ) ) {
86// hash_hmac tolerates non-string (would return null with warning) 87thrownew InvalidArgumentException(
'Invalid key type: ' . get_debug_type( $key ) );
89return hash_hmac( self::hashAlgo(), $data, $key, $raw );
Utility functions for generating hashes.
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...