Hash algorithm with alphanumeric output of 20 char

2019-04-05 06:11发布

问题:

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:

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.



标签: hash md5 sha1