支持缓冲区 API 的对象需要 sha256 错误

object supporting the buffer API required sha256 error

我想散列一些 4 位数字 但它给了我(支持缓冲区的对象 API 需要)错误

这是我的代码

 import hashlib
import itertools as it

number=[0,1,2,3,4,5,6,7,8,9]
code = hashlib.sha256()
passwords = list(it.permutations(number, 4))
 #hpass is hash password
for hpass in passwords :
    code.update(passwords)
    
    print(hpass)

输出为

Traceback (most recent call last):
  File "c:\Users\Parsa\Desktop\project\Untitled-2.py", line 11, in <module>
    code.update(passwords)
TypeError: object supporting the buffer API required

hashlib.sha256实例的更新函数需要bytes-like对象

用类似字节的对象更新散列对象。 https://docs.python.org/3/library/hashlib.html

而且似乎在更新时输入了密码列表

很难知道你的代码的意图。 但我想你想通过 4 位数字获得一组哈希值 如果正确,试试这个。

import hashlib
import itertools as it

number=[0,1,2,3,4,5,6,7,8,9]

passwords = list(it.permutations(number, 4))
# hpass is hash password
for hpass in passwords :
    
    encoded_hpass = ''.join(map(str, hpass)).encode('ascii')
    
    code = hashlib.sha256()
    code.update(encoded_hpass)
    
    print(encoded_hpass)
    print(code.digest())

输出

b'0123'
b'\x1b\xe2\xe4R\xb4mz\r\x96V\xbb\xb1\xf7h\xe8$\x8e\xba\x1bu\xba\xede\xf5\xd9\x9e\xaf\xa9H\x89\x9aj'
b'0124'
b'\x91\xb1\xe2@\xed\x10?)\x1c\x16v\\xb2\x01\xa9\xe4\xf2\xc2?\xf4\x05pP\xb9\xfdxW\x1f7:\xce='
b'0125'
b'\x17\x9f\x91-Q]\xdb\x97\xa0\x8f\xee\xb4\xe3v\x99aH\xda;\xb7\xcb]\x97K\x81<\x8a\xfb\xdcaf+'
b'0126'
b'\x9d\xa4\x84d|\xdd\xd7\x98]\x9e\xf5\x06\xf9\xbd\x15\x80\xf5\xa8\xdc\x06R8\xdbp\x1b\xc5~\x08\xa3<\\xa9'
b'0127'
b'h|\xdf<\xae\xaa\x88\x8b\x00\x0e\xdfJ\x01\xd1(\xe3\xb3 &\xb0O\xe2H\xaa0\xab/\xab\xa6\xd3@3'
b'0128'
b'\xee\xb9\xff>\xa6X,\xe2\xbf\x03\xb9\xbb\xff\x95\x88"\x90\xb8\xa8\xe5(\xa3\x91\xbc5i\x17\x92\x8fr\x1c\x06'
b'0129'
b'-\x90|u\xaa\xb2$\x85\x0bkv\xd1^/\xd4q$\x8e\xdfq]\xe8\xf7\x9d\xc8L-A\x1ff?\x88'
b'0132'
b"\xa7H\x02\x8b\x05\x18\xda\x98\xd8\xd2F\xe6\x1a8\x96\xa6w\x05\x97^'\xc3\xa0B\xb1E\r\xa9\\xe3\x9bU"
b'0134'
....