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+ }
0 commit comments