Composer is preferred to install Devhelp/Hash, please check composer website for more information.
$ composer require 'devhelp/hash:dev-master'
Devhelp/Hash introduces HashGenerator class that can be used to register and use different hash algorithms. It supports all PHP core hash algorithms by default, but you can register custom ones as \Closure or Devhelp\Hash\Algorithm\HashAlgorithmInterface
it uses hash function under the hood
$generator = new \Devhelp\Hash\HashGenerator();
$generator->generate('sha256', 'some_data'); // returns hash generated by sha256 algorithm
$generator->generate('sha256', 'some_data', array('raw_output' => true);
class CustomHashAlgorithm implements \Devhelp\Hash\Algorithm\HashAlgorithmInterface
{
public function hash($data, array $options = array())
{
//...
}
}
$myAlgorithm = new CustomHashAlgorithm();
$myClosure = function ($data, $options) {
//...
};
$generator = new \Devhelp\Hash\HashGenerator();
$generator->register('my_algorithm', $myAlgorithm);
$generator->register('my_closure', $myClosure);
$generator->generate('my_algorithm', 'some_data'); //returns hash generated by CustomHashAlgorithm class
$generator->generate('my_closure', 'some_data'); //returns hash generated by $myClosure \Closure
Only thing to do is to define custom algorithm and register in under the same name as the core algorithm
$generator = new \Devhelp\Hash\HashGenerator();
$generator->register('sha512', new CustomHashAlgorithm());
$generator->generate('sha512', $data); //returns hash generated by CustomHashAlgorithm class
Brought to you by : Devhelp.pl