I've been trying to use a MD5 hash as a key name on AppEngine, but the code I wrote raises a UnicodeDecodeError
from google.appengine.ext import db
import hashlib
key = db.Key.from_path('Post', hashlib.md5('thecakeisalie').digest())
I don't want to use hexdigest()
as that is not only a kludge, but an inferior one too (base64 would do a better job).
An MD5 hash is created by taking a string of an any length and encoding it into a 128-bit fingerprint. Encoding the same string using the MD5 algorithm will always result in the same 128-bit hash output.
The MD5 hashing algorithm uses a complex mathematical formula to create a hash. It converts data into blocks of specific sizes and manipulates that data a number of times. While this is happening, the algorithm adds a unique value into the calculation and converts the result into a small signature or hash.
MD5 is still being used today as a hash function even though it has been exploited for years.
The App Engine Python docs says:
A key_name is stored as a Unicode string (with str values converted as ASCII text).
The key has to be an unicode-encodeable-string. You need to change the digest() call to hexdigest(), ie:
k = hashlib.md5('thecakeisalie').hexdigest()
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