Reputation: 928
I need to generate a hash from a tuple. Ideally I would have liked to able to do it from a list, but that's not possible. I need something that I can use the hash to generate back the tuple, to finally access the original list with the items in the right order (items will be strings).
Here's what I'm trying to hash
l = ['x', 'y', 'z']
t = tuple(l)
I tried using hash()
, but that ended up not giving the same hash across Python sessions, which is something I need.
I need the hash because I want to create a file based off that list with the hash as the filename. I then want to lookup the file name and be able to access the list items (in the correct order) using just the hash.
My understanding is that this is possible, but I could be wrong. Any ideas?
Upvotes: 8
Views: 5710
Reputation: 198388
You can use MD5, which is fast, and will always give you the same result for the same input.
import hashlib
t = ('x', 'y', 'z')
m = hashlib.md5()
for s in t:
m.update(s.encode())
fn = m.hexdigest() # => 'd16fb36f0911f878998c136191af705e'
As user2357112 says, you cannot reconstruct l
from fn
; but if l
was saved in a file that bears the MD5 hash, you will be able to read it.
Upvotes: 4
Reputation: 613
Hash is theoretically irreversible. Encrypt seems to be want you want. For example, base64
================== update
base64 is encode technology.enter link description here
Maybe encrypt or encode is want you want.
Upvotes: -1
Reputation: 37267
No, this is not possible if your tuple contains strings and with the builtin hash()
.
The hash of strings are intentionally made variable across Python sessions, because in Python 3.4, it led to a potential security issue (PEP 456). In Python 3.5, this was fixed by making hashes of strings different in every Python session.
I recommend that you create a hashing function of your own so it's stable, or use some hashlib
.
Upvotes: 3