Skip to content

Commit c8930e6

Browse files
committed
authcode func & readme & composer
1 parent 4473c4c commit c8930e6

File tree

3 files changed

+168
-2
lines changed

3 files changed

+168
-2
lines changed

Authcode.php

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
<?php
2+
3+
namespace Authcode;
4+
5+
class Authcode
6+
{
7+
/**
8+
* The authcode key.
9+
*
10+
* @var string
11+
*/
12+
protected $key;
13+
14+
/**
15+
* Create a new authcode instance.
16+
*
17+
* @param string $key
18+
* @return void
19+
*
20+
* @throws \RuntimeException
21+
*/
22+
public function __construct($key)
23+
{
24+
$this->key = $key;
25+
}
26+
27+
/**
28+
* Encode the given value.
29+
*
30+
* @param string $value
31+
* @param bool $serialize
32+
* @return string
33+
*
34+
* @throws \Illuminate\Contracts\Encryption\EncryptException
35+
*/
36+
public function encode($value, $expiry = 0)
37+
{
38+
$this->authcode($value, 'ENCODE', $this->key, $expiry);
39+
}
40+
41+
/**
42+
* Decode the given value.
43+
*
44+
* @param string $value
45+
* @param bool $serialize
46+
* @return string
47+
*
48+
*/
49+
public function decode($value, $expiry = 0)
50+
{
51+
$this->authcode($value, 'DECODE', $this->key, $expiry);
52+
}
53+
54+
/**
55+
* Authcode function, from discuz source.
56+
*
57+
* @param string $string
58+
* @param string $operation
59+
* @param string $key
60+
* @param integer $expiry
61+
* @return string
62+
*/
63+
protected function authcode($string, $operation = 'DECODE', $key = '', $expiry = 0)
64+
{
65+
66+
$ckey_length = 4;
67+
68+
$key = md5($key ? $key : UC_KEY);
69+
$keya = md5(substr($key, 0, 16));
70+
$keyb = md5(substr($key, 16, 16));
71+
$keyc = $ckey_length ? ($operation == 'DECODE' ? substr($string, 0, $ckey_length): substr(md5(microtime()), -$ckey_length)) : '';
72+
73+
$cryptkey = $keya.md5($keya.$keyc);
74+
$key_length = strlen($cryptkey);
75+
76+
$string = $operation == 'DECODE' ? base64_decode(substr($string, $ckey_length)) : sprintf('%010d', $expiry ? $expiry + time() : 0).substr(md5($string.$keyb), 0, 16).$string;
77+
$string_length = strlen($string);
78+
79+
$result = '';
80+
$box = range(0, 255);
81+
82+
$rndkey = array();
83+
for($i = 0; $i <= 255; $i++) {
84+
$rndkey[$i] = ord($cryptkey[$i % $key_length]);
85+
}
86+
87+
for($j = $i = 0; $i < 256; $i++) {
88+
$j = ($j + $box[$i] + $rndkey[$i]) % 256;
89+
$tmp = $box[$i];
90+
$box[$i] = $box[$j];
91+
$box[$j] = $tmp;
92+
}
93+
94+
for($a = $j = $i = 0; $i < $string_length; $i++) {
95+
$a = ($a + 1) % 256;
96+
$j = ($j + $box[$a]) % 256;
97+
$tmp = $box[$a];
98+
$box[$a] = $box[$j];
99+
$box[$j] = $tmp;
100+
$result .= chr(ord($string[$i]) ^ ($box[($box[$a] + $box[$j]) % 256]));
101+
}
102+
103+
if($operation == 'DECODE') {
104+
if((substr($result, 0, 10) == 0 || substr($result, 0, 10) - time() > 0) && substr($result, 10, 16) == substr(md5(substr($result, 26).$keyb), 0, 16)) {
105+
return substr($result, 26);
106+
} else {
107+
return '';
108+
}
109+
} else {
110+
return $keyc.str_replace('=', '', base64_encode($result));
111+
}
112+
}
113+
114+
/**
115+
* Get the authcode key.
116+
*
117+
* @return string
118+
*/
119+
public function getKey()
120+
{
121+
return $this->key;
122+
}
123+
}

README.md

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,16 @@
1-
# authcode
2-
PHP authcode function. From discuz.
1+
# Authcode
2+
PHP string encrypt and decode function. From discuz.
3+
4+
## Installation & loading
5+
6+
Just add this line to your `composer.json` file:
7+
8+
```json
9+
"wilon/authcode": "~5.2"
10+
```
11+
12+
or
13+
14+
```sh
15+
composer require wilon/authcode
16+
```

composer.json

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
{
2+
"name": "wilon/authcode",
3+
"description": "Ecode and decode string.",
4+
"license": "MIT",
5+
"homepage": "https://wilon.github.io",
6+
"support": {
7+
"issues": "https://github.com/wilon/authcode/issues",
8+
"source": "https://github.com/wilon/authcode"
9+
},
10+
"keywords": ["encrypt", "decode"],
11+
"authors": [
12+
{
13+
"name": "Weilong Wang",
14+
"email": "wilonx@163.com"
15+
}
16+
],
17+
"require": {
18+
"php": ">=5.2"
19+
},
20+
"autoload": {
21+
"psr-4": {
22+
"Authcode\\": ""
23+
}
24+
},
25+
"config": {
26+
"sort-packages": true
27+
},
28+
"minimum-stability": "dev"
29+
}

0 commit comments

Comments
 (0)