Giter VIP home page Giter VIP logo

hashid's Introduction

介绍

一个对Laravel应用模型ID进行对称加密的辅助函数。

依赖于hashids/hashids

只适用于正整数加密。

安装

$ composer require jiaxincui/hashid

配置

  1. 复制config/hashid.php文件到Laravel项目的config文件夹。
  2. 在.env文件添加配置项HASH_ID_ALPHABET=your-key
  • 为了Hash成更安全的字符串,请手动重新生成HASH_ID_ALPHABET,为0-9a-zA-Z共62个字符随机排序,字符不可重复,长度为16-62,可使用以下方法生成
echo str_shuffle('0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ');

简单使用

包含两个辅助方法id_encode()id_decode()。 在Laravel项目的任何地方均可使用这两个函数对ID进行加密或解密。

例子

echo id_encode(4568); //输出:N5lkv0
  
echo id_decode('N5lkvO'); //输出:4568
  
//不可对float类型数字加密,不可对负数加密,给定任何非正整数参数都会抛出错误,如:
echo id_encode(2.36); //非正整数,抛出错误
echo id_encode(-23); //非正整数,抛出错误
  
//解密时任何无效字符串参数或校验错误都会抛出错误, 如:
echo id_decode('m_Dl9'); //包含无效字符,抛出错误
echo id_decode('nlK8GhRW'); //校验错误,抛出错误

hashids/hashids不同的是hashids提供多个数字或包含多个数字的数组加密成一个字符串, 解密时以数组形式返回,这在实际应用中并不常见,反而给使用带来一定麻烦, 而此包对此进行了一些处理,不提供多个数字或多个数字数组的加密,解密时直接返回解密后的数字。

Laravel深度应用

加密

有2种方法实现自动加密

  • 如果模型主键为id

    通过Hashid提供的trait,在数据库模型中使用use Hashid;,对结果中的id字段自动加密成字符串,例如:

<?php
namespace App;
  
use Illuminate\Database\Eloquent\Model;
use Jiaxincui\Hashid\Traits\Hashid;
 
class User extends Model
{
    use Hashid;

}
  • 如果模型中的主键不是id

    你需要在模型中定义一个访问器,如你的主键为pid,在Model中添加访问器如下:

public function getPidAttribute($value)
{
  return id_encode($value);
}

解密

通过Hashid提供的middleware对路由参数解码,在控制器中无需做任何操作即可解码加密后的路由参数。 首先在App\Http\Kernel.php中注册中间件,在Kernel类的$routeMiddleware属性添加中间件条目。例如:

'hashid' => \Jiaxincui\Hashid\Http\Middleware\Hashid::class,

现在你可以在路由中分配中间件了。例如:

Route::resource('/users', 'UserController')->middleware('hashid');

中间件参数

默认情况下,Hashid中间件会解密当前路由的所有路由参数,如果你想指定被解密的路由参数可在中间件传入参数,例如:

Route::get('users/{user}/posts/{post}/comments/{comment}', function ($user, $post, $comment) {
    //
})->middleware('hashid:user,post');

以上例子中间件只解密给出的参数,如以上例子会解密路由参数userpost,不会解密commnent

现在你的应用已经具备完整的加密和解密模型ID的功能。

License

MIT © JiaxinCui

hashid's People

Contributors

jiaxincui avatar

Stargazers

伏毅 avatar  avatar xyxu avatar BuWenGuiQi_ avatar  avatar wilbur.yu avatar 蜗牛先生 avatar Wyvern avatar tegic avatar luxury avatar Bing avatar lsshu avatar afart avatar 赵兴壮 avatar  avatar liyq avatar  avatar cuckooemm avatar  avatar Mead avatar magicdev avatar Finn avatar  avatar

Watchers

James Cloos avatar

hashid's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.