laravel5.5使用hashids加密id

1.在laravel中安装hashids

composer require vinkla/hashids

2.在config/app.php中的providers数组中添加

Vinkla\Hashids\HashidsServiceProvider::class

3.在config/app.php中的aliases数组中添加

'Hashids' => Vinkla\Hashids\Facades\Hashids::clas

4.以config下生成hashids.php配置文件

php artisan vendor:publish

5.修改hashids.php中的connections的盐值和加密输出长度

其中盐值可以是任意长度任意字符的字符串,加密和盐值有直接的关系,盐值是解密的钥匙。我直接取项目的密钥作为其盐值,以让项目统一,且不同项目的加密结果不一样。

'connections' => [
        'main' => [
            'salt' => env('APP_KEY'),
            'length' => '16',
        ],
        'alternative' => [
            'salt' => env('APP_KEY'),
            'length' => '6',
        ],
        'recommend' => [
            'salt' => env('APP_KEY'),
            'length' => '6',
        ],
    ],

6.Hashids的加密解密使用方式

  • 加密的使用方式
Hashids::encode(123);//返回经过加密后的字符串a9M4pPZqO0rJ6QWK
  • 解密的使用方式
    注意返回值是数组
Hashids::decode('a9M4pPZqO0rJ6QWK');//返回经过解密后的数组[123]
  • 同时加密多个参数
Hashids::encode(1,2,3);//M0BKxg8cYSNrVAjp
  • 解密多个参数的加密字符串
Hashids::decode('M0BKxg8cYSNrVAjp')//返回经过解密后的数组[1,2,3]
  • 切换不同的盐值和加密长度
    我们可能需要对多个不同类型的id进行加密,盐值和返回长度也各有不同。所以config的hashids中的多个数组可以派上用场了。其中main数组是作为默认连接,可以自行添加其他的加密数组。
Hashids::connection('recommend')->encode(1);
Hashids::connection('recommend')->decode("jflkasdjfkasdjfl");

-------------------------------------------------以上来自https://www.jianshu.com/p/5683c507f1ba----------------------------------------------------------

1,控制器:

use Vinkla\Hashids\Facades\Hashids;

public function test_hashids()
    {
        // 加密单个参数
//        $id = Hashids::encode(123);
//        $str = Hashids::decode($id);
//        var_dump($id, $str);
//        exit;

        // 同时加密多个参数
//        $_id = Hashids::encode(1,2,3);
//        $str = Hashids::decode($_id);
//        var_dump($_id,$str);

        //切换不同的盐值和加密长度
        $_id = Hashids::connection('recommend')->encode(1);
        $str = Hashids::connection('recommend')->decode($_id);
        var_dump($_id,$str);
    }

2,路由:

Route::get('/test/id', 'Test\TestController@test_hashids');

3,config/app:  同上  2.在config/app.php中的providers数组中添加  与  3.在config/app.php中的aliases数组中添加

// 新增 Hashids,生成混淆不一的id
// 位置:vendor\vinkla\hashids\src\HashidsServiceProvider.php
Vinkla\Hashids\HashidsServiceProvider::class,

// 新增 vendor\vinkla\hashids\src\Facades\Hashids.php
'Hashids' => Vinkla\Hashids\Facades\Hashids::class,

4,新文件 hashids.php

php artisan vendor:publish  


    'connections' => [
//        'main' => [
//            'salt' => 'your-salt-string',
//            'length' => 'your-length-integer',
//        ],
//
//        'alternative' => [
//            'salt' => 'your-salt-string',
//            'length' => 'your-length-integer',
//        ],
        'main' => [
            'salt' => env('APP_KEY'),
            'length' => '16',
        ],
        'alternative' => [
            'salt' => env('APP_KEY'),
            'length' => '6',
        ],
        'recommend' => [
            'salt' => env('APP_KEY'),
            'length' => '6',
        ],


    ],

];

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值