Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hash algorithm with alphanumeric output of 20 characters max

Tags:

hash

md5

sha1

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.

like image 766
Giacomo Avatar asked Jan 28 '11 08:01

Giacomo


1 Answers

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.

like image 110
phihag Avatar answered Sep 28 '22 10:09

phihag