python 3 hashlib.sha256().update(newcontent)似乎不会覆盖旧内容



当我尝试使用哈希函数时,更新方法似乎不会覆盖字符串:

例如,给定一个字符串库

hasher = hashlib.sha256() #set the hasher
hasher.update(magazine.encode('utf-8'))
print( int( hasher.hexdigest(), 16 ) % 10**8)
hasher.update(magazine.encode('utf-8'))
print( int( hasher.hexdigest(), 16 ) % 10**8)

将打印7398353865808855

hasher = hashlib.sha256()
hasher.update(magazine.encode('utf-8'))
print( int( hasher.hexdigest(), 16 ) % 10**8)
hasher = hashlib.sha256() #reset the hasher
hasher.update(magazine.encode('utf-8'))
print( int( hasher.hexdigest(), 16 ) % 10**8)

将打印
7398353873983538

更新函数到底是什么?有没有一种方法可以在不创建新散列的情况下重置字符串?

非常感谢,

为什么不想创建一个新的哈希器?一个散列表示一个"事物"的散列,更新方法存在,这样你就可以散列大量的数据(每次一定数量的字节(。即两个

hasher = hashlib.sha256()
hasher.update(b"foo")
hasher.update(b"bar")

hasher = hashlib.sha256()
hasher.update(b"foobar")

导致相同的散列。

没有办法重置哈希对象的状态,因为该状态甚至不能通过Python(用C编写((直接(访问。

最新更新