Python hashlib.md5() TypeError: Unicode 对象必须在散列之前编码
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27519306/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
hashlib.md5() TypeError: Unicode-objects must be encoded before hashing
提问by Shahab
I am new to coding and have ran into a problem trying to encode a string.
我是编码新手,在尝试对字符串进行编码时遇到了问题。
>>> import hashlib
>>> a = hashlib.md5()
>>> a.update('hi')
Traceback (most recent call last):
File "<pyshell#22>", line 1, in <module>
a.update('hi')
TypeError: Unicode-objects must be encoded before hashing
>>> a.digest()
b'\xd4\x1d\x8c\xd9\x8f\x00\xb2\x04\xe9\x80\t\x98\xec\xf8B~'
Is (a) now considered to be encoded?
(a) 现在被认为是被编码的吗?
Second question: When I run the same code above in a script I get this error:
第二个问题:当我在脚本中运行上面相同的代码时,出现此错误:
import hashlib
a = hashlib.md5()
a.update('hi')
a.digest()
Traceback (most recent call last): File "C:/Users/User/Desktop/Logger/Encoding practice.py", line 3, in a.update('hi') TypeError: Unicode-objects must be encoded before hashing
回溯(最近一次调用):文件“C:/Users/User/Desktop/Logger/Encoding practice.py”,第 3 行,在 a.update('hi') 中 TypeError: Unicode-objects must be encoding before hashing
Why is the code working in the shell and not the script? I am working with Windows and Python 3.4
为什么代码在 shell 中工作而不是在脚本中工作?我正在使用 Windows 和 Python 3.4
Thanks.
谢谢。
回答by Ignacio Vazquez-Abrams
It's not working in the REPL. It's hashed nothing, since you've passed it nothing valid to hash. Try encoding first.
它在 REPL 中不起作用。它没有散列任何东西,因为你传递了它没有任何有效的散列。先尝试编码。
3>> hashlib.md5().digest()
b'\xd4\x1d\x8c\xd9\x8f\x00\xb2\x04\xe9\x80\t\x98\xec\xf8B~'
3>> a = hashlib.md5()
3>> a.update('hi'.encode('utf-8'))
3>> a.digest()
b'I\xf6\x8a\\x84\x93\xec,\x0b\xf4\x89\x82\x1c!\xfc;'
回答by Steve Barnes
Since you are encoding simple strings I deduce that you are running Python 3 where all strings are unicode objects, you have two options:
由于您正在编码简单的字符串,我推断您正在运行 Python 3,其中所有字符串都是 unicode 对象,您有两个选择:
- Provide an encoding for the strings, e.g.:
"Nobody inspects".encode('utf-8')
Use binary strings as shown in the manuals:
m.update(b"Nobody inspects") m.update(b" the spammish repetition")
- 为字符串提供编码,例如:
"Nobody inspects".encode('utf-8')
使用手册中所示的二进制字符串:
m.update(b"Nobody inspects") m.update(b" the spammish repetition")
The reason for the differing behaviour in the script to the shell is that the script stops on the error whereas in the shell the last line is a separate command but still not doing what you wish it to because of the previous error.
脚本中与 shell 不同行为的原因是脚本停止在错误处,而在 shell 中,最后一行是一个单独的命令,但由于先前的错误,仍然没有执行您希望的操作。
回答by NeoWu
Under the different versions of Python is different,I use Python 2.7,same as you write, it works well.
不同版本的Python下是不同的,我用的是Python 2.7,和你写的一样,效果很好。
hashlib.md5(data) function, the type of data parameters should be 'bytes'.That is to say, we must put the type of data into bytes before hashes.
hashlib.md5(data) 函数,数据参数的类型应该是'bytes'。也就是说,我们必须在hash之前将数据的类型放入bytes中。
Requirements before the hash code conversion, because the same string have different values under different coding systems(utf8\gbk.....), in order to ensure not happen ambiguity has to be a dominant conversion.
哈希码转换前的要求,因为同一个字符串在不同的编码系统下有不同的值(utf8\gbk..
回答by Matej Butkovi?
The solution I've found is to simply encode the data right away in the line where you're hashing it:
我找到的解决方案是立即在您对其进行散列的行中简单地对数据进行编码:
hashlib.sha256("a".encode('utf-8')).hexdigest()
hashlib.sha256("a".encode('utf-8')).hexdigest()
It worked for me, hope it helps!
它对我有用,希望它有所帮助!
回答by Haris Np
For Python3 the following worked.
对于 Python3,以下工作有效。
secretKey = b"secret key that you get from Coginito -> User Pool -> General Settings -> App Clients-->Click on Show more details -> App client secret"
clientId = "Coginito -> User Pool -> General Settings -> App Clients-->App client id"
digest = hmac.new(secretKey,
msg=(user_name + clientId).encode('utf-8'),
digestmod=hashlib.sha256
).digest()
signature = base64.b64encode(digest).decode()
The username user_name in the above is same as the user that you want to register in the cognito.
上面的用户名 user_name 与您要在 cognito 中注册的用户相同。