sha1和b未定义

时间:2016-02-11 10:42:14

标签: python python-3.x

template< class InputIt >
basic_string(InputIt first, InputIt last, const Allocator& alloc = Allocator());

我遇到问题,因为在此上下文中无法识别b,我需要它来执行此操作。如果我把b'astring'它会工作。 如何解决这个问题?感谢。

1 个答案:

答案 0 :(得分:3)

您对函数的b'...' 语法感到困惑。它不是,它是符号来创建bytes个对象,就像使用1230xdeadbeaf0o creates integer objects, and [...]一样创建一个列表。

传入字符串时的错误消息告诉您该怎么做:

>>> import hashlib
>>> hashlib.sha1('')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: Unicode-objects must be encoded before hashing

使用合适的编解码器对字符串进行编码。如果您想支持密码字符的完整Unicode范围(总是一个好主意),请使用UTF-8或UTF-16或UTF-32等编解码器:

 encrypted_password = hashlib.sha1((email + passw).encode('utf8'))