j3j5/hmac-bcrypt-laravel

用于密码散列的 HMAC-BCrypt 实现的 Laravel 散列器

0.2.1 2024-06-01 23:51 UTC

This package is auto-updated.

Last update: 2024-09-02 00:16:53 UTC


README

Coverage Badge PHPStan Badge, it reads "level 9" Build status for "main" branch

此仓库包含 Laravel 框架的 hmac-bcrypt 密码散列函数的实现。它基于由 @epixoip 创建的参考实现(特别是 PHP 版本)。

如果你在问自己为什么,你可以阅读原始实现的 技术论证

安装

如果你想要使用它,你可以使用 composer

composer require j3j5/hmac-bcrypt-laravel

配置

在你的 config/hashing.php 中,你可以将驱动程序更改为 hmac-bcrypt。为了运行,你需要设置一个 pepper,它应该是一个 唯一(每个项目)的秘密字符串。你有两个选项,要么在 .env 中设置 HMAC_BCRYPT_PEPPER(或作为环境变量),要么将以下数组添加到你的自己的 hashing.php 配置文件中:

'hmac-bcrypt' => [
    'pepper' => 'black-pepper'
],

bcrypt 所使用的轮数也可以自定义。你可以在 .env 中使用 HMAC_BCRYPT_ROUNDS(或作为环境变量),或者将 rounds 键添加到你的散列配置中的 hmac-bcrypt 键。

'hmac-bcrypt' => [
    'rounds' => 15
],

使用

现在你可以像使用 Laravel 中的散列器一样使用它

$clearTextPass = 'supersecret';

$hash = Hash::make($clearTextPass);

// Now store it on the db

稍后...

if (Hash::check($clearTextPass, $hash)) {
    // eccoli qua! you can log in your user!

    // Check whether your settings have changed since last time
    if (Hash::needsRehash($hash)) {
        $newHash = Hash::make($clearTextPass);
        // Store the new hash on the db
    }
}

最后笔记

虽然我在实现上尽力做到非常小心和彻底,但我制作这个驱动程序是为了好玩,所以请自行承担风险。我鼓励你深入研究代码,以确保我没有遗漏任何重要内容,或者检查测试,以便你可以自己验证当前的功能。它下面使用 SHA512 的本地 PHP 函数 hash_hmac()crypt() 进行 Bcrypt 加密(由 random_bytes() 生成的盐),所以这不是构建自己的加密库的案例,而是使用已经可用的库。