Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
ericyzhu committed Sep 2, 2020
0 parents commit 05cf4d6
Show file tree
Hide file tree
Showing 22 changed files with 1,229 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/tests export-ignore
/.github export-ignore
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/vendor/
composer.lock
*.cache
*.log
.idea/
.DS_Store
92 changes: 92 additions & 0 deletions .php_cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
<?php

$header = <<<'EOF'
This file is part of Hyperf.
@link https://www.hyperf.io
@document https://hyperf.wiki
@contact [email protected]
@license https://github.com/hyperf/hyperf/blob/master/LICENSE
EOF;

return PhpCsFixer\Config::create()
->setRiskyAllowed(true)
->setRules([
'@PSR2' => true,
'@Symfony' => true,
'@DoctrineAnnotation' => true,
'@PhpCsFixer' => true,
'header_comment' => [
'commentType' => 'PHPDoc',
'header' => $header,
'separate' => 'none',
'location' => 'after_declare_strict',
],
'array_syntax' => [
'syntax' => 'short'
],
'list_syntax' => [
'syntax' => 'short'
],
'concat_space' => [
'spacing' => 'one'
],
'blank_line_before_statement' => [
'statements' => [
'declare',
],
],
'general_phpdoc_annotation_remove' => [
'annotations' => [
'author'
],
],
'ordered_imports' => [
'imports_order' => [
'class', 'function', 'const',
],
'sort_algorithm' => 'alpha',
],
'single_line_comment_style' => [
'comment_types' => [
],
],
'yoda_style' => [
'always_move_variable' => false,
'equal' => false,
'identical' => false,
],
'phpdoc_align' => [
'align' => 'left',
],
'multiline_whitespace_before_semicolons' => [
'strategy' => 'no_multi_line',
],
'constant_case' => [
'case' => 'lower',
],
'class_attributes_separation' => true,
'combine_consecutive_unsets' => true,
'declare_strict_types' => true,
'linebreak_after_opening_tag' => true,
'lowercase_static_reference' => true,
'no_useless_else' => true,
'no_unused_imports' => true,
'not_operator_with_successor_space' => true,
'not_operator_with_space' => false,
'ordered_class_elements' => true,
'php_unit_strict' => false,
'phpdoc_separation' => false,
'single_quote' => true,
'standardize_not_equals' => true,
'multiline_comment_opening_closing' => true,
])
->setFinder(
PhpCsFixer\Finder::create()
->exclude('bin')
->exclude('public')
->exclude('runtime')
->exclude('vendor')
->in(__DIR__)
)
->setUsingCache(false);
38 changes: 38 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
language: php

sudo: required

matrix:
include:
- php: 7.2
env: SW_VERSION="4.5.3RC1"
- php: 7.3
env: SW_VERSION="4.5.3RC1"
- php: 7.4
env: SW_VERSION="4.5.3RC1"

allow_failures:
- php: master

services:
- docker

before_install:
- export PHP_MAJOR="$(`phpenv which php` -r 'echo phpversion();' | cut -d '.' -f 1)"
- export PHP_MINOR="$(`phpenv which php` -r 'echo phpversion();' | cut -d '.' -f 2)"
- echo $PHP_MAJOR
- echo $PHP_MINOR

install:
- cd $TRAVIS_BUILD_DIR
- bash .travis/swoole.install.sh
- phpenv config-rm xdebug.ini || echo "xdebug not available"
- phpenv config-add .travis/ci.ini

before_script:
- cd $TRAVIS_BUILD_DIR
- composer config -g process-timeout 900 && composer update

script:
- composer analyse
- composer test
5 changes: 5 additions & 0 deletions .travis/ci.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[opcache]
opcache.enable_cli=1

[swoole]
extension = "swoole.so"
10 changes: 10 additions & 0 deletions .travis/swoole.install.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#!/usr/bin/env bash
wget https://github.com/swoole/swoole-src/archive/v"${SW_VERSION}".tar.gz -O swoole.tar.gz
mkdir -p swoole
tar -xf swoole.tar.gz -C swoole --strip-components=1
rm swoole.tar.gz
cd swoole || exit
phpize
./configure --enable-openssl --enable-mysqlnd --enable-http2
make -j "$(nproc)"
make install
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) Taylor Otwell

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
135 changes: 135 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
# Hyperf 哈希组件

该组件为存储用户密码提供了安全的 Bcrypt 和 Argon2 哈希加密方式。

> 移植自 [illuminate/hashing](https://github.com/illuminate/hashing )
## 安装

```shell script
composer require hyperf-ext/hashing
```

## 发布配置

```shell script
php bin/hyperf.php vendor:publish hyperf-ext/hashing
```

> 配置文件位于 `config/autoload/ext-hashing.php`
## 默认配置

```php
<?php

declare(strict_types=1);

return [
'default' => 'bcrypt',
'driver' => [
'bcrypt' => [
'class' => \HyperfExt\Hashing\Driver\BcryptDriver::class,
'rounds' => env('BCRYPT_ROUNDS', 10),
],
'argon' => [
'class' => \HyperfExt\Hashing\Driver\Argon2IDriver::class,
'memory' => 1024,
'threads' => 2,
'time' => 2,
],
'argon2id' => [
'class' => \HyperfExt\Hashing\Driver\Argon2IdDriver::class,
'memory' => 1024,
'threads' => 2,
'time' => 2,
],
],
];
```

你可以在 `config/autoload/ext-hashing.php` 配置文件中配置默认哈希驱动程序。目前支持三种驱动程序: Bcrypt 和 Argon2(Argon2i 和 Argon2id variants)。

> 注意:Argon2i 驱动程序需要 PHP 7.2.0 或更高版本,而 Argon2id 驱动程序则需要 PHP 7.3.0 或更高版本。
## 使用

你可以通过 `\HyperfExt\Hashing\Hash` 类来加密你的密码:

```php
<?php

declare(strict_types=1);

namespace App\Http\Controller;

use Hyperf\HttpServer\Request;
use HyperfExt\Hashing\Hash;

class UpdatePasswordController
{
public function update(Request $request)
{
// ……

$user->fill([
'password' => Hash::make($request->input('new_password'))
])->save();
}
}
```

### 调整 Bcrypt 加密系数

如果使用 Bcrypt 算法,你可以在 `make` 方法中使用 `rounds` 选项来配置该算法的加密系数。然而,对大多数应用程序来说,默认值就足够了:

```php
$hashed = Hash::make('password', [
'rounds' => 12
]);
```

### 调整 Argon2 加密系数

如果使用 Argon2 算法,你可以在 `make` 方法中使用 `memory``time``threads` 选项来配置该算法的加密系数。然后,对大多数应用程序来说,默认值就足够了:

```php
$hashed = Hash::make('password', [
'memory' => 1024,
'time' => 2,
'threads' => 2,
]);
```

> 有关这些选项的更多信息,请查阅 [PHP 官方文档](https://secure.php.net/manual/en/function.password-hash.php )
### 密码哈希验证

`check` 方法能为您验证一段给定的未加密字符串与给定的哈希值是否一致:

```php
if (Hash::check('plain-text', $hashedPassword)) {
// 密码匹配…
}
```

### 检查密码是否需要重新哈希

`needsRehash` 方法可以为您检查当哈希的加密系数改变时,您的密码是否被新的加密系数重新加密过:

```php
if (Hash::needsRehash($hashed)) {
$hashed = Hash::make('plain-text');
}
```

### 使用指定驱动

```php
$hasher = Hash::getDriver('argon2i');
$hasher->make('plain-text');
```

### 使用自定义哈希类

实现 `\HyperfExt\Hashing\Contract\DriverInterface` 接口,并参照配置文件中的其他算法进行配置。
57 changes: 57 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
{
"name": "hyperf-ext/hashing",
"type": "library",
"license": "MIT",
"keywords": [
"php",
"hyperf",
"hashing"
],
"description": "The unofficial Hyperf Hashing package.",
"authors": [
{
"name": "Eric Zhu",
"email": "[email protected]"
},
{
"name": "Taylor Otwell",
"email": "[email protected]"
}
],
"autoload": {
"psr-4": {
"HyperfExt\\Hashing\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"HyperfTest\\": "tests"
}
},
"require": {
"php": ">=7.2",
"ext-swoole": ">=4.5",
"hyperf/config": "^2.0",
"hyperf/di": "^2.0",
"hyperf/framework": "^2.0"
},
"require-dev": {
"friendsofphp/php-cs-fixer": "^2.14",
"hyperf/testing": "^2.0",
"phpstan/phpstan": "^0.12",
"swoole/ide-helper": "dev-master"
},
"config": {
"sort-packages": true
},
"scripts": {
"test": "co-phpunit -c phpunit.xml --colors=always",
"analyse": "phpstan analyse --memory-limit 1024M -l 0 ./src",
"cs-fix": "php-cs-fixer fix $1"
},
"extra": {
"hyperf": {
"config": "HyperfExt\\Hashing\\ConfigProvider"
}
}
}
15 changes: 15 additions & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit bootstrap="tests/bootstrap.php"
backupGlobals="false"
backupStaticAttributes="false"
verbose="true"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false">
<testsuite name="Testsuite">
<directory>./tests/</directory>
</testsuite>
</phpunit>
Loading

0 comments on commit 05cf4d6

Please sign in to comment.