elfsundae/laravel-hashid

通过生成可逆的、非顺序的、URL安全的标识符来混淆数据的一种简单、优雅的方法。

1.7.2 2023-05-16 10:59 UTC

This package is auto-updated.

Last update: 2024-09-16 13:54:20 UTC


README

Latest Version on Packagist Software License tests StyleCI SymfonyInsight Grade Quality Score Code Coverage Total Downloads

Laravel Hashid 提供了一个统一的 API,支持多种驱动,如 Base62Base64HashidsOptimus,并支持多连接或不同的编码选项。它通过生成可逆的、非顺序的、URL安全的标识符,提供了一种简单、优雅的混淆数据的方法。

安装

您可以使用 Composer 管理器安装此包。

$ composer require elfsundae/laravel-hashid

对于 Lumen 或 Laravel v5.5 之前的版本,您需要手动注册服务提供程序。

ElfSundae\Laravel\Hashid\HashidServiceProvider::class

然后发布配置文件。

# For Laravel application:
$ php artisan vendor:publish --tag=hashid

# For Lumen application:
$ cp vendor/elfsundae/laravel-hashid/config/hashid.php config/hashid.php

配置

我们的配置文件经过良好的文档说明,与许多 Laravel 管理器集成(如数据库、队列、缓存和文件系统)的配置非常相似。因此,您不需要额外的时间来学习如何配置 Hashid。

此外,为了简单起见,您不需要在配置文件中添加像 Base64 这样的单例驱动程序,因为它们没有编码选项,除非您想指定一个有意义的连接名称。

让我们看看配置的一个示例。

'default' => 'id',

'connections' => [

    'basic' => [
        'driver' => 'base64',
    ],

    'hashids' => [
        'driver' => 'hashids',
        'salt' => 'sweet girl',
    ],

    'id' => [
        'driver' => 'hashids_integer',
        'salt' => 'My Application',
        'min_length' => 6,
        'alphabet' => '1234567890abcdef',
    ],

    'base62' => [
        'driver' => 'base62',
        'characters' => 'f9FkqDbzmn0QRru7PBVeGl5pU28LgIvYwSydK41sCO3htaicjZoWAJNxH6EMTX',
    ],

],

使用

可以使用 hashid() 辅助函数或 Hashid 门面与任何配置的连接或驱动程序交互。

use ElfSundae\Laravel\Hashid\Facades\Hashid;

// Obtain the default connection instance
hashid();
Hashid::connection();

// Obtain the "base62" connection instance
hashid('base62');
Hashid::connection('base62');

// Obtain the Base64 driver instance
hashid('base64');
Hashid::connection('base64');
Hashid::driver('base64');

您只需要了解两个方法来使用任何连接或驱动程序。

  • encode($data) 用于编码数据。
  • decode($data) 用于解码数据。
hashid()->encode(123456);

hashid('base64')->decode('TGFyYXZlbA');

Hashid::encode(123456);

Hashid::connection('hashids')->decode('X68fkp');

还有两个相应的辅助函数

  • hashid_encode($data, $name = null)
  • hashid_decode($data, $name = null)
hashid_encode(123456);

hashid_decode('TGFyYXZlbA', 'base64');

内置驱动

Base62

  • 驱动程序:base62base62_integer
  • 配置
    • characters:62个独特的字符
  • 后端:tuupola/base62
  • 注意
    • 您可以使用 hashid:alphabet 命令生成随机字符。
    • 强烈推荐使用 GMP,因为它比纯 PHP 快得多。

Base64

Hashids

  • 驱动程序:hashidshashids_hexhashids_integerhashids_string
  • 配置
    • 最小长度
    • alphabet:至少16个独特的字符
  • 后端:hashids/hashids
  • 注意
    • 您可以使用 hashid:alphabet 命令生成随机字母表。
    • 强烈推荐使用 GMP

十六进制

  • 驱动程序:hexhex_integer

Optimus

  • 驱动程序:optimus
  • 配置
    • prime:小于 2147483647 的大素数
    • inverse:逆素数,使得 (PRIME * INVERSE) & MAXID == 1
    • random:小于 2147483647 的大随机整数
  • 后端:jenssegers/optimus
  • 注意
    • 您可以使用 hashid:optimus 命令生成所需的数字。
    • 仅适用于整数。
    • 能够正确处理的最大数字是 2147483647

自定义驱动

要创建自定义的Hashid驱动程序,您只需要实现包含两个方法(encodedecode)的接口 ElfSundae\Laravel\Hashid\DriverInterface。构造函数可以可选地接收来自 $config 参数的驱动配置,并且支持类型提示的依赖注入。

<?php

namespace App\Hashid;

use ElfSundae\Laravel\Hashid\DriverInterface;
use Illuminate\Contracts\Encryption\Encrypter;

class CustomDriver implements DriverInterface
{
    protected $encrypter;

    protected $serialize;

    public function __construct(Encrypter $encrypter, array $config = [])
    {
        $this->encrypter = $encrypter;

        $this->serialize = $config['serialize'] ?? false;
    }

    public function encode($data)
    {
        return $this->encrypter->encrypt($data, $this->serialize);
    }

    public function decode($data)
    {
        return $this->encrypter->decrypt($data, $this->serialize);
    }
}

现在您可以使用此驱动程序配置连接。

'connections' => [

    'custom' => [
        'driver' => App\Hashid\CustomDriver::class,
        'serialize' => false,
    ],

    // ...
]

如果您希望为您的驱动程序使用简短名称,只需使用 hashid.driver. 前缀注册容器绑定即可。

$this->app->bind('hashid.driver.custom', CustomDriver::class);

测试

$ composer test

许可证

本软件包是开源软件,采用 MIT 许可证授权。