I want to hash a simple array of strings
The documentation says you can't simple feed a string into hashlib's update() function,
so I tried a regular variable, but then I got the TypeError: object supporting the buffer API required
error.
Here's what I had so far
def generateHash(data):
# Prepare the project id hash
hashId = hashlib.md5()
hashId.update(data)
return hashId.hexdigest()
You can use the repr()
function to get the (Unicode) string representation of the array (or of whatever object that implements conversion to a representation). Then you encode the string to UTF-8 (the order of bytes is the same everywhere when using UTF-8). The resulting bytes can be hashed as you tried above:
#!python3
import hashlib
def hashFor(data):
# Prepare the project id hash
hashId = hashlib.md5()
hashId.update(repr(data).encode('utf-8'))
return hashId.hexdigest()
if __name__ == '__main__':
data1 = ['abc', 'de']
data2 = ['a', 'bcde']
print(hashFor(data1) + ':', data1)
print(hashFor(data2) + ':', data2)
It prints on my console:
c:\tmp\___python\skerit\so17412304>py a.py
d26d27d8cbb7c6fe50637155c21d5af6: ['abc', 'de']
dbd5ab5df464b8bcee61fe8357f07b6e: ['a', 'bcde']
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