germania-kg / hash
PHP密码散列和验证的调用包装器,可选支持PSR3 Logger
2.1.0
2023-01-04 14:54 UTC
Requires
- php: ^7.4|^8.0
Requires (Dev)
- friendsofphp/php-cs-fixer: ^2.0
- php-coveralls/php-coveralls: ^2.0
- phpspec/prophecy-phpunit: ^2.0
- phpstan/phpstan: ^1.9
- phpunit/phpunit: ^8.0|^9.0
- spatie/phpunit-watcher: ^1.0
README
PHP密码散列和验证的调用包装器,可选支持PSR3 Logger
使用Composer安装
$ composer require germania-kg/hash $ composer require germania-kg/hash:^2
PasswordHashCallable
此类封装了PHP的password_hash函数,并以可调用类的方式提供。它可以选择接受任何PSR-3 Logger,其中每个调用都会调用其debug方法。
<?php use Germania\Hash\PasswordHashCallable; // Create Callable, optional with PSR-3 Logger $hashing = new PasswordHashCallable; $hashing = new PasswordHashCallable( $monolog ); // Get hashed value echo $hashing( "mysecret" );
配置
您可以定义PHP的password_hash函数的cost和algo参数。默认值为14
和\PASSWORD_BCRYPT
。
<?php use Germania\Hash\PasswordHashCallable; // Defaults PasswordHashCallable::$cost = 14; PasswordHashCallable::$algo = \PASSWORD_BCRYPT;
PasswordVerifyCallable
此类封装了PHP的password_verify函数,并以可调用类的方式提供。它可以选择接受任何PSR-3 Logger,其中每个调用都会调用其debug方法。
<?php use Germania\Hash\PasswordVerifyCallable; // Create Callable, optional with PSR-3 Logger $verifier = new PasswordVerifyCallable; $verifier = new PasswordVerifyCallable( $monolog ); // Get hashed value $secret = "foobar"; $hash = password_hash($secret, \PASSWORD_BCRYPT); $hash = password_hash($secret, \PASSWORD_DEFAULT) // Will be TRUE echo $verifier( "foobar", $hash ); // Will be FALSE echo $verifier( "wrong", $hash );
CallbackHashCallable
此类需要一个自定义回调(可调用或匿名函数)。它可以选择接受任何PSR-3 Logger,其中每个调用都会调用其debug方法。
<?php use Germania\Hash\CallbackHashCallable; // Create anonymous function $callback = function( $token ) { return password_hash( $token ); }; // Create Callable, optional with PSR-3 Logger $hashing = new CallbackHashCallable( $callback ); $hashing = new CallbackHashCallable( $callback, $monolog ); // Get hashed value echo $hashing( "mysecret" );
问题
请参阅问题列表。
开发
$ git clone https://github.com/GermaniaKG/Hash.git
$ cd Hash
$ composer install
单元测试
您可以复制phpunit.xml.dist
到phpunit.xml
并适应您的需求,或者保持不变。运行PhpUnit测试或composer脚本如下:
$ composer test # or $ vendor/bin/phpunit