13
votes

I need an hash algorithm that outputs an alphanumeric string that is max 20 characters long. For "alphanumeric" I mean [a-zA-Z0-9].

Inputs are UUIDs in canonical form (example 550e8400-e29b-41d4-a716-446655440000)

In alternative is there a way to convert a SHA1 or MD5 hash to a string with these limitations?

Thanks.

EDIT

Doesn't need to be cryptographically secure. Collisions make data inaccurate, but if they happen sporadically I can live with it.

EDIT 2

I don't know if truncating MD5 or SHA1 would make collisions happen too often. Now I'm wondering if it's better to truncate to 20 chars a MD5 value or a SHA1 value.

1
Do you have any desired properties for these hashes? Should they be cryptographically secure, or just good enough for a hash table?templatetypedef
Can't you just print out the SHA1 or MD5 hash string in hex (or base64) and truncate to 20 chars if needed?Dustin Boswell
What is your goal? Is it such that just taking a 20 character substring of a SHA1 hash would not work?Jason LeBrun
I'll try truncating the SHA1 (or MD5, not decided yet) to 20 chars. I can measure collisions, so I can post the results.Giacomo

1 Answers

19
votes

Just clip the characters you don't need from the hash of the GUID. With a good hash function, the unpredictability of any part of the hash is proportional to the part's size. If you want, you can encode it base 32 instead of the standard hex base 16. Bear in mind that this will not significantly improve entropy per character (only by 25%).

For non-cryptographic uses, it does not matter whether you truncate MD5, SHA1 or SHA2. Neither has any glaring deficiencies in entropy.