在Python中生成临时URL

时间:2021-08-13 18:28:41

I've been trying to generate a temporary url in python, the url will some data that i need to make sure isn't changed so i'll add a hash in the end but i keep ending with a bString no matter what i try, can anyone point out what i'm doing wrong? Here's a sample of my code oh and i know that maybe changing the algorithms/encoding might solve the problem but i can't find a suitable one, can any downvoter explain why he downvoted

我一直在尝试在python中生成一个临时url,url将需要确保的一些数据不会改变所以我会在最后添加一个哈希但是无论我尝试什么,我都会以bString结尾谁能指出我做错了什么?这是我的代码示例哦,我知道可能更改算法/编码可能会解决问题,但我找不到合适的,可以任何downvoter解释为什么他downvoted

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)

1 个解决方案

#1


The problem was not being able to putout a normal string because the encryption will result in characters that are almost impossible to encode, so the solution is to use binascii to encode the bStrings for us and decode them

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

import binascii

then we encode the resulting usable string for the link

然后我们编码链接的结果可用字符串

hexedLink = binascii.hexlify(link).decode()

and we unhexify it before using it in the method

我们在方法中使用它之前将其取消通知

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

#1


The problem was not being able to putout a normal string because the encryption will result in characters that are almost impossible to encode, so the solution is to use binascii to encode the bStrings for us and decode them

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

import binascii

then we encode the resulting usable string for the link

然后我们编码链接的结果可用字符串

hexedLink = binascii.hexlify(link).decode()

and we unhexify it before using it in the method

我们在方法中使用它之前将其取消通知

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