谷歌云存储签名url如何在python 3中生成sha256哈希?

时间:2016-06-24 10:09:14

标签: python-3.x google-cloud-storage pycrypto

我需要使用python3和google云存储tgt。示例代码here是用python2编写的,我试图更改脚本,使其在python3中工作。

在python 2中,SHA256工作正常。

import Crypto.Hash.SHA256 as SHA256

def _Base64Sign(self, plaintext):
    """Signs and returns a base64-encoded SHA256 digest."""
    shahash = SHA256.new(plaintext)

但是在python3中,它似乎没有生成相同的密钥。

import Crypto.Hash.SHA256 as SHA256
def _Base64Sign(self, plaintext):
        """Signs and returns a base64-encoded SHA256 digest."""
        t = plaintext.encode()
        shahash = SHA256.new(t)
        #print(shahash)
        signer = PKCS1_v1_5.new(self.key)
        signature_bytes = signer.sign(shahash)
        return base64.b64encode(signature_bytes)

所以我的问题是如果plaintext.encode()正在改变sha256哈希,或者只是pycrypto已经改变了python3。如何生成与python 2版本相同的密钥?我是否必须在python 3中移植python2?

任何建议都将不胜感激,谢谢。

0 个答案:

没有答案