Created
March 13, 2019 03:14
-
-
Save MeiK2333/20291225fd7adcaabb833542261854db to your computer and use it in GitHub Desktop.
美团 Token 的加密与解密方法
This file contains hidden or 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 base64 | |
import json | |
import random | |
import time | |
import zlib | |
def meituan_encrypt(string): | |
""" 美团的加密算法 """ | |
if isinstance(string, str): | |
string = string.encode() | |
return base64.b64encode(zlib.compress(string)).decode() | |
def meituan_unencrypt(string): | |
""" 美团的解密算法 """ | |
if isinstance(string, str): | |
string = string.encode() | |
return zlib.decompress(base64.decodestring(string)).decode() | |
def meituan_token(Flag, LogVal, location, referer, url_without_token): | |
""" | |
生成美团的 Token | |
Flag: 在原始页面中找(Ctrl + F) | |
LogVal: 在原始页面中找 | |
location: 当前页面链接 | |
referer: 来源页面链接 | |
url_without_token: 要发出的请求链接(不包含 token) | |
return: 生成的 Token | |
""" | |
args = dict(map(lambda x: x.split('='), | |
url_without_token.split('?')[1].split('&'))) | |
sorted_keys = sorted(args.keys()) | |
raw_sign = '&'.join(['='.join([key, args[key]]) for key in sorted_keys]) | |
raw_sign = f'"{raw_sign}"' | |
sign = meituan_encrypt(raw_sign) | |
l = { | |
'rId': Flag, | |
'ts': int(time.time() * 1000), | |
'cts': int(time.time() * 1000) + random.randint(10000000, 50000000), | |
'brVD': [691, 669], | |
'brR': [[1366, 768], [1366, 741], 24, 24], | |
'bI': [location, referer], | |
'mT': [f'{random.randint(250, 650)},{random.randint(250, 650)}' for _ in range(30)], | |
'kT': [], | |
'aT': [], | |
'tT': [], | |
'sign': sign, | |
} | |
return meituan_encrypt(json.dumps(l).replace(' ', '')) | |
if __name__ == '__main__': | |
print(meituan_token( | |
Flag=100051, | |
LogVal='rohrdata', | |
location='https://hotel.meituan.com/50452379/', | |
referer='', | |
url_without_token="https://ihotel.meituan.com/productapi/v2/prepayList?type=1&utm_medium=PC&version_name=7.3.0&poiId=50452379&start=1552320000000&end=1552406400000&uuid=D8E61A98D07C634764A814DFBBE2E03514D4B41F14315ADF88C20EA30FCD4C0D") | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment