(function () { var data = getSignTime(); var token = "a8a0cde0d32347b79fca1efaf2aff907"; var nonce=("0000" + (Math.random()*Math.pow(36,4) << 0).toString(36)).slice(-4); var sign = calcSign(data,token,nonce); postman.setGlobalVariable('nonce', nonce); postman.setGlobalVariable('sign', sign); postman.setGlobalVariable('signTime', data); })(); function getSignTime(){ return parseInt(new Date().getTime() / 1000); } function calcSign(data,token,nonce) { delete data['sign']; var sign = CryptoJS.SHA256(data+token+nonce+data).toString(); return sign.toUpperCase(); }能将此JS代码转换成python代码吗
时间: 2023-06-10 17:04:32 浏览: 104
Unexpected token c in JSON at position 0 报错信息及解决
5星 · 资源好评率100%
当然,这段 JS 代码可以转换成 Python 代码,如下所示:
import hashlib
import random
import time
def getSignTime():
return int(time.time())
def calcSign(data, token, nonce):
del data['sign']
data_str = ''.join([key+str(value) for key,value in data.items()])
raw_str = f"{data_str}{token}{nonce}{data_str}"
sign = hashlib.sha256(raw_str.encode('utf-8')).hexdigest()
return sign.upper()
data = {"param1": "value1", "param2": "value2", "sign": "xxx"}
token = "a8a0cde0d32347b79fca1efaf2aff907"
nonce = "0000" + format(int(random.random() * pow(36, 4)), 'x')[-4:]
sign = calcSign(data, token, nonce)
print("nonce:", nonce)
print("sign:", sign)
print("signTime:", getSignTime())
阅读全文