Last active
January 10, 2016 17:33
-
-
Save RickGray/ec14ee5dc0adfb83fb80 to your computer and use it in GitHub Desktop.
"sys_auth()" function in PHPCMS and Discuz writed by Python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import time | |
import hashlib | |
import base64 as b64 | |
def md5(string): | |
return hashlib.md5(str(string)).hexdigest() | |
# "sys_auth()" function in PHPCMS and Discuz | |
def sys_auth_v1(string, operation='ENCODE', key='', expiry=0): | |
key_length = 4 | |
key = md5(key) | |
fixedkey = md5(key) | |
egiskeys = md5(fixedkey[16: 16 + 16]) | |
runtokey = (md5(time.time())[-key_length:] if operation == 'ENCODE' else string[: key_length]) \ | |
if key_length else '' | |
keys = md5(runtokey[: 16] + fixedkey[: 16] + runtokey[16:] + fixedkey[16:]) | |
string = (('%010d' % (expiry + int(time.time()) if expiry else 0)) + md5(string + egiskeys)[: 16] + string) \ | |
if operation == 'ENCODE' else b64.b64decode(string[key_length:]) | |
result = '' | |
string_length = len(string) | |
for i in range(string_length): | |
result += chr(ord(string[i]) ^ ord(keys[i % 32])) | |
if operation == 'ENCODE': | |
return runtokey + b64.b64encode(result).rstrip('=') | |
else: | |
if int(result[: 10]) == 0 or int(result[: 10]) - int(time.time()) > 0 \ | |
or result[10: 26] == md5(result[26: ] + egiskeys)[: 16]: | |
return result[26:] | |
else: | |
return '' | |
def sys_auth_v2(string, operation='ENCODE', key='', expiry=0): | |
ckey_length = 4 | |
key = md5(key if key != '' else key) | |
keya = md5(key[: 16]) | |
keyb = md5(key[16: 16 + 16]) | |
keyc = (string[: ckey_length] if operation == 'DECODE' else md5(time.time())[-ckey_length:]) \ | |
if ckey_length else '' | |
cryptkey = keya + md5(keya + keyc) | |
key_length = len(cryptkey) | |
string = b64.b64decode(string[4:].replace('-', '+').replace('_', '/')) \ | |
if operation == 'DECODE' else (('%010d' % (expiry + int(time.time()) if expiry else 0)) + md5(string + keyb)[: 16] + string) | |
string_length = len(string) | |
result = '' | |
box = range(0, 256) | |
rndkey = {} | |
for i in range(256): | |
rndkey[i] = ord(cryptkey[i % key_length]) | |
j = 0 | |
for i in range(256): | |
j = (j + box[i] + rndkey[i]) % 256 | |
tmp = box[i] | |
box[i] = box[j] | |
box[j] = tmp | |
a = 0 | |
j = 0 | |
for i in range(string_length): | |
a = (a + 1) % 256 | |
j = (j + box[a]) % 256 | |
tmp = box[a] | |
box[a] = box[j] | |
box[j] = tmp | |
result += chr(ord(string[i]) ^ (box[(box[a] + box[j]) % 256])) | |
if operation == 'DECODE': | |
if (int(result[: 10]) == 0 or (int(result[: 10]) - int(time.time())) > 0) and (result[10: 10 + 16] == md5(result[26:] + keyb)[: 16]): | |
return result[26:] | |
else: | |
return '' | |
else: | |
return keyc + b64.b64encode(result).replace('+', '-').replace('/', '_').rstrip('=') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment