45 if ( self::$algo !==
null ) {
49 $algos = hash_hmac_algos();
50 $preference = [
'whirlpool',
'sha256' ];
52 foreach ( $preference as $algorithm ) {
53 if ( in_array( $algorithm, $algos,
true ) ) {
54 self::$algo = $algorithm;
59 throw new DomainException(
'Could not find an acceptable hashing function.' );
71 $key = $raw ?
'binary' :
'hex';
72 if ( self::$hashLength[$key] ===
null ) {
73 self::$hashLength[$key] = strlen( self::hash(
'', $raw ) );
77 return self::$hashLength[$key];
88 public static function hash( $data, $raw =
true ) {
89 return hash( self::hashAlgo(), $data, $raw );
101 public static function hmac( $data, $key, $raw =
true ) {
102 if ( !is_string( $key ) ) {
104 throw new InvalidArgumentException(
'Invalid key type: ' . gettype( $key ) );
106 return hash_hmac( self::hashAlgo(), $data, $key, $raw );
static $hashLength
The number of bytes outputted by the hash algorithm.
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 $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...