devhelp / hash
介绍了 HashGenerator 类,可用于注册和使用不同的哈希算法
1.0
2014-09-25 16:26 UTC
Requires
- php: >=5.3
Requires (Dev)
This package is not auto-updated.
Last update: 2024-09-14 15:46:26 UTC
README
安装
建议使用 Composer 安装 Devhelp/Hash,请查看Composer 网站获取更多信息。
$ composer require 'devhelp/hash:dev-master'
目的
Devhelp/Hash 介绍了 HashGenerator 类,可用于注册和使用不同的哈希算法。它默认支持所有 PHP 核心哈希算法,但您也可以注册自定义的算法,作为 \Closure 或 Devhelp\Hash\Algorithm\HashAlgorithmInterface
使用方法
使用内置的 PHP 核心算法
底层使用 hash 函数
$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
覆盖内置的 PHP 核心算法
要做的只是定义自定义算法,并在与核心算法相同的名称下注册
$generator = new \Devhelp\Hash\HashGenerator();
$generator->register('sha512', new CustomHashAlgorithm());
$generator->generate('sha512', $data); //returns hash generated by CustomHashAlgorithm class
致谢
由 : Devhelp.pl 提供