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,
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
and
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).