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



我想散列一些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:UsersParsaDesktopprojectUntitled-2.py", line 11, in <module>
code.update(passwords)
TypeError: object supporting the buffer API required

hashlib.sha256实例的更新函数需要像对象一样的字节

使用类似字节的对象更新哈希对象。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'x1bxe2xe4Rxb4mzrx96Vxbbxb1xf7hxe8$x8exbax1buxbaxedexf5xd9x9exafxa9Hx89x9aj'
b'0124'
b'x91xb1xe2@xedx10?)x1cx16v\xb2x01xa9xe4xf2xc2?xf4x05pPxb9xfdxWx1f7:xce='
b'0125'
b'x17x9fx91-Q]xdbx97xa0x8fxeexb4xe3vx99aHxda;xb7xcb]x97Kx81<x8axfbxdcaf+'
b'0126'
b'x9dxa4x84d|xddxd7x98]x9exf5x06xf9xbdx15x80xf5xa8xdcx06R8xdbpx1bxc5~x08xa3<\xa9'
b'0127'
b'h|xdf<xaexaax88x8bx00x0exdfJx01xd1(xe3xb3 &xb0Oxe2Hxaa0xab/xabxa6xd3@3'
b'0128'
b'xeexb9xff>xa6X,xe2xbfx03xb9xbbxffx95x88"x90xb8xa8xe5(xa3x91xbc5ix17x92x8frx1cx06'
b'0129'
b'-x90|uxaaxb2$x85x0bkvxd1^/xd4q$x8exdfq]xe8xf7x9dxc8L-Ax1ff?x88'
b'0132'
b"xa7Hx02x8bx05x18xdax98xd8xd2Fxe6x1a8x96xa6wx05x97^'xc3xa0Bxb1Erxa9\xe3x9bU"
b'0134'
....

最新更新