基于高性能算法实现的优雅 ID 混淆扩展包 —— Laravel Hashid


Laravel Hashid 整合了 Base62Base64HashidsHexOptimus 等高性能编码算法,并提供了统一的、优雅的、简单易用的调用接口,将「敏感数据」混淆(编码)成可还原的、非连续的、URL 安全的标识符 (ID) 。

应用场景示例

  • 不希望对外暴露有规则的数据索引,比如用户 ID 、媒体资源 ID 、商品 ID 、订单号、注册码、优惠码等,防止爬虫侵扰。
  • 重构现有的发码(ID 生成)机制:使用数据库自带的索引主键,但是对外进行混淆。
  • 对加密串进一步混淆,并生成 URL 安全的字符串。
  • 简单、统一的调用方法使用不同的编码算法、同一算法的不同编码参数、或自定义算法。

项目主页

项目主页及详细文档: https://github.com/ElfSundae/laravel-hashid

新包求 Star 求反馈 ?

安装

$ composer require elfsundae/laravel-hashid

对于 Lumen 或 Laravel 低于 5.5 版本,需要手动注册 service provider:

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

配置

Hashid 的配置文件和 Laravel 框架的众多管理 (manager) 服务的配置极其类似,例如数据库、缓存、队列等。所以无需花费额外时间来学习如何配置它。

我们来看个例子:

'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() 全局函数获取 connection 或 driver 实例。
  • 使用 hashid_encode() 全局函数进行编码。
  • 使用 hashid_decode() 全局函数进行解码。
示例:
hashid_encode(123456);  // "xkNDJ"
hashid_decode('xkNDJ'); // 123456

hashid_encode(123456, 'optimus'); // 1101845824 hashid_decode(1101845824, 'optimus'); // 123456

hashid_encode(123456, 'base62'); // "W7E" hashid_encode('123456', 'base62'); // "FMJUCzH4" hashid_decode('W7E', 'base62_integer'); // 123456

内置驱动

  • Base62 : base62 , base62_integer
  • Base64 : base64 , base64_integer
  • Hashids : hashids , hashids_hex , hashids_integer , hashids_string
  • Hex : hex , hex_integer
  • Optimus : optimus

控制台命令

  • hashid:alphabet :生成随机串 0-9a-zA-Z
  • hashid:optimus :生成 Optimus 编码要用到的参数

自定义驱动

要使用自己的编解码算法,只需要创建一个类实现 ElfSundae\Laravel\Hashid\DriverInterface 接口即可,这个接口只有两个方法: encode 和 decode 。初始化方法可选接收一个名为 $config 的配置参数,同时也支持类型提示式依赖注入。

例如:

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</span>CustomDriver::class, 'serialize' => false, ],

// ... ]

调用示例:
hashid_encode(123456, 'custom');
如果想为自定义驱动使用一个短名字,注册一个容器绑定即可:
$this->app->bind('hashid.driver.custom', CustomDriver::class);
更多使用方法请参考项目主页:https://github.com/ElfSundae/laravel-hashid

点赞 取消点赞 收藏 取消收藏

<< 上一篇: 如果在所有 Laravel 项目中只安装一个扩展包,你会选择哪一个?

>> 下一篇: 诺诺开放平台 PHP-sdk,需要的来点一波 star 咯~