views:

89

answers:

3

I am having a hard time figuring out a reasonable way to generate a mixed-case hash in Python.

I want to generate something like: aZeEe9E

Right now I'm using MD5, which doesn't generate case-sensitive hashes.

Do any of you know how to generate a hash value consisting of upper- and lower- case characters + numbers?

-

Okay, GregS's advice worked like a charm (on the first try!):

Here is a simple example:

>>> import hashlib, base64
>>> s = 'http://gooogle.com'
>>> hash = hashlib.md5(s).digest()
>>> print hash
46c4f333fae34078a68393213bb9272d
>>> print base64.b64encode(hash)
NDZjNGYzMzNmYWUzNDA3OGE2ODM5MzIxM2JiOTI3MmQ=
+3  A: 

you can base64 encode the output of the hash. This has a couple of additional characters beyond those you mentioned.

GregS
That is an interesting idea! I'll check it out and report back.
pyrony
Thanks GregS! I had to do a reply to the question in order to be able to include code...but that is SO's fault.THANK YOU!
pyrony
@pyrony, edit your original question rather than adding "pseudo-answers" -- no fault of SO, just use it properly!-)
Alex Martelli
+2  A: 

Maybe you can use base64-encoded hashes?

Ber
A: 

okay, thanks everyone. I'm leaving this here because I appreciate the digest() rather than hexdigest() insight below.

pyrony
Use the binary `digest()` rather than the `hexdigest()`. Since you're base64-ing it anyway you don't need the extra step of hex-encoding to make it readable.
bobince
@pyrony, so **accept** that answer, already -- use the checkmark-shaped icon just below that answer's "number of upvotes" digit on the answer's upper left.
Alex Martelli