Skip to content

Commit 1c5a85a

Browse files
committed
Signed-off-by: jenkincei <jenkincei@hotmail.com> Init
0 parents  commit 1c5a85a

File tree

6 files changed

+148
-0
lines changed

6 files changed

+148
-0
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
.idea/
2+
vendor/

LICENSE.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# The MIT License (MIT)
2+
3+
> Permission is hereby granted, free of charge, to any person obtaining a copy
4+
> of this software and associated documentation files (the "Software"), to deal
5+
> in the Software without restriction, including without limitation the rights
6+
> to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7+
> copies of the Software, and to permit persons to whom the Software is
8+
> furnished to do so, subject to the following conditions:
9+
>
10+
> The above copyright notice and this permission notice shall be included in
11+
> all copies or substantial portions of the Software.
12+
>
13+
> THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14+
> IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15+
> FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16+
> AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17+
> LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18+
> OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19+
> THE SOFTWARE.

README.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
## 介绍
2+
Laravel对ID进行对称加密的辅助函数
3+
4+
只适用于正整数加密,目前只测试12位以内正整数有效
5+
## 安装
6+
```sh
7+
$ composer require jiaxincui/hashid
8+
```
9+
## 配置
10+
1. 复制`config/hashid.php`文件到Laravel项目的`config`文件夹
11+
2. 在.env文件添加配置项`HASH_ID_KEY=your-key`
12+
* 请务必手动重新生成HASH_ID_KEY,为0-9a-zA-Z共62个字符随机排序,字符不可重复,长度为16-62,可使用以下方法生成
13+
```php
14+
echo str_shuffle('0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ');
15+
```
16+
## 使用
17+
在laravel项目的任何地方均可使用`id_encode()``id_decode()`函数对ID进行加密或解密
18+
19+
### 例子
20+
```php
21+
22+
echo id_encode(4568); //输出:N5lkv0
23+
24+
echo id_decode('N5lkvO'); //输出:4568
25+
26+
//不可对float类型数字加密,不可对负数加密,给定任何非正整数参数都会返回null
27+
echo id_encode(2.36); //非正整数,输出:null
28+
echo id_encode(-23); //非正整数,输出:null
29+
30+
//解密时任何无效字符串参数或校验错误都将返回null, 如:
31+
echo id_decode('m_Dl9'); //包含无效字符,输出:null
32+
echo id_decode('nlK8GhRW'); //校验错误,输出:null
33+
34+
```

composer.json

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"name": "jiaxincui/hashid",
3+
"type": "library",
4+
"description": "Laravel ID Encrypt Helper",
5+
"keywords": ["laravel", "encrypt", "helper", "hashid"],
6+
"license": "MIT",
7+
"authors": [
8+
{
9+
"name": "Jiaxin Cui",
10+
"email": "jenkincei@hotmail.com"
11+
}
12+
],
13+
"require": {},
14+
"autoload": {
15+
"files": [
16+
"src/helpers.php"
17+
]
18+
},
19+
"minimum-stability": "stable"
20+
}

config/hashid.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
/**
3+
* key为0-9a-zA-Z的不重复字符串,最少16位,最多62位,推荐62位.
4+
* 可使用str_shuffle('0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ')对现有字符串重新生成复制到这里.
5+
*/
6+
7+
return [
8+
'key' => env('HASH_ID_KEY', 'Fi1yqx4mk3Bda7DfMCjWoOSUHYTRKhuszl2cg5pXLe6AwEGn8NvJ9VtZr0IQbP')
9+
];

src/helpers.php

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
<?php
2+
3+
if (! function_exists('id_encode')) {
4+
/**
5+
* 加密正整数
6+
*
7+
* @param $int
8+
* @return string | null
9+
*/
10+
function id_encode($int)
11+
{
12+
if (! is_numeric($int) || $int < 0 || ! is_int($int + 0)) {
13+
return null;
14+
}
15+
$sign = 0;
16+
foreach (str_split($int) as $v) {
17+
$sign += $v;
18+
}
19+
$keyArr = str_split(config('hashid.key'));
20+
$keyLen = count($keyArr);
21+
$rand = mt_rand(0, $keyLen - 1);
22+
$str = $keyArr[$rand];
23+
foreach (str_split(dechex($int)) as $v) {
24+
$offset = hexdec($v) + $rand;
25+
$str .= $keyArr[$offset % $keyLen];
26+
}
27+
return $str . $keyArr[$sign % $keyLen];
28+
}
29+
}
30+
31+
if (! function_exists('id_decode')) {
32+
/**
33+
* 对加密后的字符串解密
34+
*
35+
* @param $str
36+
* @return Number | null
37+
*/
38+
function id_decode($str)
39+
{
40+
if (! preg_match('/^[0-9a-zA-Z]{2,12}$/', $str)) {
41+
return null;
42+
}
43+
$strArr = str_split($str);
44+
$key = config('hashid.key');
45+
$keyArr = str_split($key);
46+
$keyLen = count($keyArr);
47+
$hex = '';
48+
$rand = strpos($key, array_shift($strArr));
49+
$verfy = array_pop($strArr);
50+
foreach ($strArr as $v) {
51+
$hex .= strpos($key, $v) >= $rand ? dechex(strpos($key, $v) - $rand) : dechex($keyLen - $rand + strpos($key, $v));
52+
}
53+
$dec = hexdec($hex);
54+
$decArr = str_split($dec);
55+
$sign = 0;
56+
foreach ($decArr as $v) {
57+
$sign += $v;
58+
}
59+
if ($verfy !== $keyArr[$sign % $keyLen]) {
60+
return null;
61+
}
62+
return $dec;
63+
}
64+
}

0 commit comments

Comments
 (0)