python 3 hashlib.sha256().update(newcontent) seems not to overwrite old content

3.1k Views Asked by At

When I try to use the hash function, it seems the update method doesn't overwrite the string:

For example, given a string magazine

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)

will print 73983538 65808855

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)

will print
73983538 73983538

What exactly is the update function, and is there a way to reset the string without creating a new hasher?

Many thanks,

1

There are 1 best solutions below

2
L3viathan On

Why don't you want to create a new hasher? One hasher represents the hash of one "thing", the update method exists, such that you can hash large amounts of data (some amount of bytes per time). I.e. both

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

and

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

lead to the same hash.

There is no way to reset the state of the hash object, as the state isn't even (directly) accessible via Python (as it's written in C).