Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I use a MD5 hash (or other binary data) as a key name?

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

like image 842
Noah McIlraith Avatar asked Dec 22 '10 10:12

Noah McIlraith


People also ask

How do you generate the MD5 hash of a string?

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.

What is MD5 hash generator?

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.

Is MD5 still used?

MD5 is still being used today as a hash function even though it has been exploited for years.


1 Answers

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()
like image 97
vz0 Avatar answered Oct 23 '22 05:10

vz0