The Hash
class provides functions for creating message digest from a supplied
block of data. It allows to choose an algorithm and contains additional methods
giving more info like it's block size.
Description: Creates hash digest using a static call syntax.
The usage of __callStatic
magic method allows simplified syntax for creating
a Hash
object (e.g. Hash::sha256($message)
). The $name
depicts the algorithm
which is checked if it's found. If not then HashException
is thrown. Otherwise
the new Hash
instance is returned.
name : string
- the algorithm name (e.g. sha256
, sha512
, md5
)
arguments : array
- there can be just one element which is message
Hash
: New instances of the class.
It can throw HashException
with code
HashException::HASH_ALGORITHM_NOT_FOUND
- the algorithm (name) is not found
echo \Crypto\Hash::sha256('abc')->hexdigest();
Description: Returns all hash algorithms.
This static method returns all hash algorithms. Their parameters
allow filtering of the result. Some algorithms have aliases that
can be returned if the $aliases
parameter is true
. The $prefix
allows filtering by the supplied prefix string.
aliases : bool
- whether to show aliases
prefix : string
- prefix that is used for filtering of the result
This method does not throw any exception.
array
: list of supported hash alorithms
print_r(\Crypto\Hash::getAlgorithms());
Description: Finds out wheter the supplied algorithm is supported
This static method checks if the supplied hash algorithm is supported.
algorithm : string
- algorithm name
This method does not throw any exception.
bool
: if the algorithm is supperted, returns true
, otherwise false
if (\Crypto\Hash::hasAlgorithm('sha512')) {
// use SHA512
}
Description: Creates a new Hash
class if supplied algorithm is supported.
The constructor first checks if the algorithm is found. If not, then
HashException
is thrown. Otherwise a new instance of Hash
is created
algorithm : string
- the algorithm name (e.g. sha256
, sha512
, md5
)
Hash
: New instances of the Hash
class.
It can throw HashException
with code
HashException::HASH_ALGORITHM_NOT_FOUND
- the supplied algorithm is not found
$hash = new \Crypto\Hash('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 {
$hash = new \Crypto\Hash($hash_algorithm);
}
catch (\Crypto\HashException $e) {
echo $e->getMessage();
}
Description: Returns a hash digest in binary encoding
This method returns a binary digest. It also finalizes the hash
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 that object.
If the Hash
object has not been updated, then the result will
be a hash 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 hash binary string with length equal to
the result of Hash::getSize()
$digest = \Crypto\Hash::sha256('abc')->digest();
Description: Returns a hash algorithm name.
It is a getter for internal Hash::$algorithm
read only property
which is set during the object creation.
This method has no parameters.
This method does not throw any exception.
string
: The name of the hash algorithm (e.g. sha256
)
$hash = new \Crypto\Hash('sha256');
// this will output SHA256
echo $hash->getAlgorithmName();
Description: Returns a hash block size in bytes.
This method returns the block size of the used hash algorithm. That
should not be confused with the output size (which is returned by
Hash::getSize()
). The block size is a size that the hash algorithm
operates on and 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
: hash block size in bytes
$hash = new \Crypto\Hash('sha256');
// this will output 64
echo $hash->getBlockSize();
Description: Returns a hash output size in bytes.
This method returns the output size of the used hash algorithm. It means
how many bytes will be returned by the Hash::digest()
method.
This method has no parameters.
This method does not throw any exception.
int
: hash output size in bytes
$hash = new \Crypto\Hash('sha256');
// this will output 32
echo $hash->getSize();
Description: Returns a hash digest in hex encoding
This method returns a hex digest. It also finalizes the hash
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 Hash
object has not been updated, then the result will
be a hash 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
: hash digest hex string
echo \Crypto\Hash::sha256('abc')->hexdigest();
Description: Updates the hash object with supplied data
This method updates Hash
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 hash
It can throw HashException
with code
HashException::INIT_FAIED
- initialization failedHashException::UPDATE_FAIED
- updating digest failed
Hash
: An instance of the called object (for chaining)
try {
$hash = new \Crypto\Hash('sha256');
while (($data = read_data_from_somewhere()) !== false) {
$hash->update($data);
}
echo $hash->hexdigest();
} catch (\Crypto\HashException $e) {
echo $e->getMessage();
}