I want to have a unique hash for the same string in python. I use following code for getting a hash :
import hashlib
mysha1 = hashlib.sha1()
mysha1.update("my_url")
print mysha1.hexdigest()
mysha1.update("my_url")
print mysha1.hexdigest() # which is generating a different hash
is there something I missed here?
the update()
function feeds strings to be concatenated.
https://docs.python.org/2/library/hashlib.html
>>> import hashlib
>>> mysha1 = hashlib.sha1()
>>> mysha1.update("my_url")
>>> print mysha1.hexdigest()
ebde90b9f0c047ff9f86bec3b71afe5d00594030
>>> mysha1.update("my_url")
>>> print mysha1.hexdigest()
efa6ba48cedd0da886a553ad0e7c131ec79b452e
>>>
>>>
>>> sha = hashlib.sha1()
>>> sha.update("my_urlmy_url")
>>> print sha.hexdigest()
efa6ba48cedd0da886a553ad0e7c131ec79b452e
When you call update("my_url")
, you're concatenating that string to the hash input.
You can now feed this object with arbitrary strings using the update() method. At any point you can ask it for the digest of the concatenation of the strings fed to it so far using the digest() or hexdigest() methods.
You need to make a new sha1
object every time you want a new hash.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With