PHP密码散列和验证的调用包装器,可选支持PSR3 Logger

2.1.0 2023-01-04 14:54 UTC

This package is auto-updated.

Last update: 2024-09-04 18:32:08 UTC


README

PHP密码散列和验证的调用包装器,可选支持PSR3 Logger

Packagist PHP version Tests

使用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函数的costalgo参数。默认值为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.distphpunit.xml并适应您的需求,或者保持不变。运行PhpUnit测试或composer脚本如下:

$ composer test
# or
$ vendor/bin/phpunit