在 Python 中生成临时 url

Generating a Temporary url in Python

我一直在尝试在 python 中生成一个临时的 url,url 会包含一些我需要确保未更改的数据,因此我将添加最后是一个散列,但无论我尝试什么,我都会以 bString 结尾,有人能指出我做错了什么吗? 这是我的代码示例 哦,我知道也许改变 algorithms/encoding 可能会解决问题,但我找不到合适的,任何投反对票的人都可以解释为什么他投反对票

import hashlib
import datetime
from Crypto.Cipher import AES

def checkTemp(tempLink):
    encrypter = AES.new('1234567890123456', AES.MODE_CBC, 'this is an iv456')
    decryption = encrypter.decrypt(tempLink)
    length = len(decryption)

    hash_code = decryption[length-32:length]
    data= decryption[:length-32].strip()
    hasher = hashlib.sha256()
    hasher.update(data)
    hashCode = hasher.digest()

    if(hash_code==hashCode):
        array = data.decode().split(",",5)
        print("expiry date is :"+ str(array[5]))
        return array[0],array[1],array[2],array[3],array[4]
    else:
        return "","","","",""

def createTemp(inviter,email,role,pj_name,cmp_name):
    delim = ','
    data = inviter+delim+email+delim+role+delim+pj_name+delim+cmp_name+delim+str(datetime.datetime.now().time())
    data = data.encode(encoding='utf_8', errors='strict')

    hasher = hashlib.sha256()
    hasher.update(data)

    hashCode = hasher.digest()
    encrypter = AES.new('1234567890123456', AES.MODE_CBC, 'this is an iv456')
    # to make the link a multiple of 16 by adding for AES with the addition of spaces
    newData = data+b' '*(len(data)%16)
    result = encrypter.encrypt(newData+hashCode)

    return result
#print(str(link).split(",",5))
link = createTemp("name","email@homail.com","Designer","Project Name","My Company")
print(link)
inviter,email,role,project,company = checkTemp(link)

问题是无法输出正常的字符串,因为加密会导致几乎无法编码的字符,所以解决方案是使用binascii为我们编码bStrings并解码它们

import binascii

然后我们为 link

编码生成的可用字符串
hexedLink = binascii.hexlify(link).decode()

并且我们在方法中使用它之前将其解开

inviter,email,role,project,company = checkTemp(binascii.unhexlify(hexedLink))