The HMAC
class provides functions for creating a keyed-hash message
authentication code (HMAC) message digest from a supplied key and
block of data. It allows to choose a message digest algorithm.
The HMAC
class extends MAC
class which extends Hash
class. It
means that with an exception of the constructor all methods are inherited
from Hash
class.
Description: Creates a new HMAC
class if supplied algorithm is supported.
The constructor first checks if the algorithm is found. If not, then
MACException
is thrown. Otherwise a new instance of HMAC
is created.
The key length is compared with block size:
- if the key length is the same as block size, then the key is used as it is
- if the key length is smaller, then the key is padded with zero bytes to the block size
- if the key length is greater, then the key is hashed (unless it's greater than C
INT_MAX
) - if the key length is greater then C
INT_MAX
, then the exception is thrown
key : string
- the key string
algorithm : string
- the algorithm name (e.g. sha256
, sha512
, md5
)
HMAC
: New instances of the HMAC
class.
It can throw MACException
with code
MACException::HASH_ALGORITHM_NOT_FOUND
- the supplied algorithm is not foundMACException::KEY_LENGTH_INVALID
- the supplied key length is too high (over C INT_MAX)
$hmac = new \Crypto\HMAC('key', 'sha256');
If the algorithm is passed by user in variable, then it might be a good idea to wrap it in a try/catch block:
try {
$hmac = new \Crypto\HMAC($key, $hash_algorithm);
}
catch (\Crypto\HashException $e) {
echo $e->getMessage();
}
Description: Returns a MAC in binary encoding
This method returns a binary message authentication code (MAC).
It also finalizes the HMAC context which means that if
HMAC::update
is called again, then the context is
reinitialized - the result is the same like creating a new object
using the same algorithm and key and then calling HMAC::update
.
If the HMAC
object has not been updated, then the result will
be a HMAC for an empty string.
This method has no parameters.
It can throw HashException
with code
HashException::INIT_FAIED
- initialization failedHashException::DEGEST_FAIED
- creating digest failed
string
: The MAC binary string with length equal to
the result of HMAC::getSize()
$hmac = new \Crypto\HMAC('key', 'sha256');
$digest = $hmac->update('abc')->digest();
Description: Returns an underlaying hash algorithm name.
It is a getter for internal inherited Hash::$algorithm
reod only property which is set during the object construction.
This method has no parameters.
This method does not throw any exception.
string
: The name of the underlaying hash algorithm (e.g. sha256
)
$hmac = new \Crypto\HMAC('key', 'sha256');
// this will output SHA256
echo $hmac->getAlgorithmName();
Description: Returns an underlaying hash block size in bytes.
This method returns a block size of the underlaying hash algorithm.
That should not be confused with the output size (which is returned by
HMAC::getSize()
). The block size is a size that the hash algorithm
operates on and it is bigger than output size (e.g. 64 bytes which is
512 bits for SHA256).
This method has no parameters.
This method does not throw any exception.
int
: underlaying hash block size in bytes
$hmac = new \Crypto\HMAC('key', 'sha256');
// this will output 64
echo $hmac->getBlockSize();
Description: Returns an underlaying hash output size in bytes.
This method returns the output size of the underlaying hash algorithm. It means
how many bytes will be returned by the HMAC::digest()
method.
This method has no parameters.
This method does not throw any exception.
int
: underlaying hash output size in bytes
$hash = new \Crypto\HMAC('key', 'sha256');
// this will output 32
echo $hash->getSize();
Description: Returns a MAC in hex encoding.
This method returns a message authentication code. It also
finalizes the HMAC
context which means that if Hash::update
is called again, then the context is reinitialized - the result
is the same like creating a new object using the same algorithm
and then calling Hash::update
on it.
If the HMAC
object has not been updated, then the result will
be a HMAC for an empty string.
This method has no parameters.
It can throw HashException
with code
HashException::INIT_FAIED
- initialization failedHashException::DEGEST_FAIED
- creating digest failed
string
: message authentication code hex string
$hmac = new \Crypto\HMAC('key', 'sha256');
echo $hmac->update('abc')->hexdigest();
Description: Updates the HMAC object with supplied data
This method updates HMAC
object context with supplied data. It can
be useful when reading data from database or big files.
Before the update, it also initializes the internal context if it's the first update. If the initialization or update fails, the exception is thrown.
data : string
- data that updates the HMAC
It can throw HashException
with code
HashException::INIT_FAIED
- initialization failedHashException::UPDATE_FAIED
- updating HMAC failed
HMAC
: An instance of the called object (for chaining)
try {
$key = 'secret_key';
$hmac = new \Crypto\HMAC($key, 'sha256');
while (($data = read_data_from_somewhere()) !== false) {
$hmac->update($data);
}
echo $hmac->hexdigest();
} catch (\Crypto\HashException $e) {
echo $e->getMessage();
}