【问题标题】:Generating a Temporary url in Python在 Python 中生成临时 url
【发布时间】:2015-07-13 20:00:27
【问题描述】:

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

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)

【问题讨论】:

  • 我没有投反对票,但你的问题读起来像是某人的意识流。添加一些标点符号、停句和大写,以帮助读者解析您上面写的内容。
  • @Hooked 谢谢,好吧,我是个新手,所以我不太擅长发表我的想法,而且我有点沮丧并且在尝试修复它时迷失了方向,这让我更加困惑。无论如何感谢您的指点

标签: python encryption hash hyperlink temporary


【解决方案1】:

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

import binascii

然后我们为链接编码生成的可用字符串

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

我们在方法中使用它之前取消它

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-12-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-10
    • 1970-01-01
    相关资源
    最近更新 更多