0
我不了解hashlib的一些东西。我不知道为什么我可以解码常规字节对象,但无法解码作为字节对象返回的散列。我不断收到此错误:Python Hashlib&解码()上的字节对象
的UnicodeDecodeError:在位置1“UTF-8”编解码器不能解码字节写入0xAD:无效的起始字节
这里是一个的产生这个错误我的测试代码。该错误是在第8行(h2 = h.decode('utf-8')
)
import hashlib
pw = '[email protected])r'
salt = 'b7u2qw^T&^#[email protected])hvx7ivRoxr^tDyua'
pwd = pw + salt
h = hashlib.sha512(pwd.encode('utf-8')).digest()
print(h)
h2 = h.decode('utf-8')
print(h2)
如果我不散列它,它工作完全正常......
>>> pw = '[email protected])r'
>>> salt = 'b7u2qw^T&^#[email protected])hvx7ivRoxr^tDyua'
>>> pwd = pw + salt
>>> h = pwd.encode('utf-8')
>>> print(h)
b'[email protected])rb7u2qw^T&^#[email protected])hvx7ivRoxr^tDyua'
>>> h2 = h.decode('utf-8')
>>> print(h2)
[email protected])rb7u2qw^T&^#[email protected])hvx7ivRoxr^tDyua
所以我猜我不理解的东西有关散列,但我不知道我错过了什么。
散列是字节串,它们不是文本,你不能解码它们。也许你真的想使用.hexdigest()如果你想要一个文本形式的哈希? – gps 2012-08-17 05:27:08