Hash function that protects against collisions, not attacks. (Produces a random UUID-size result space)

StackOverflow https://stackoverflow.com/questions/15183297

  •  18-03-2022
  •  | 
  •  

Question

Using SHA1 to hash down larger size strings so that they can be used as a keys in a database.

Trying to produce a UUID-size string from the original string that is random enough and big enough to protect against collisions, but much smaller than the original string.

Not using this for anything security related.

Example:

# Take a very long string, hash it down to a smaller string behind the scenes and use
#     the hashed key as the data base primary key instead
def _get_database_key(very_long_key):
    return hashlib.sha1(very_long_key).digest()

Is SHA1 a good algorithm to be using for this purpose? Or is there something else that is more appropriate?

Was it helpful?

Solution

Python has a uuid library, based on RFC 4122.

The version that uses SHA1 is UUIDv5, so the code would be something like this:

import uuid

uuid.uuid5(uuid.NAMESPACE_OID, 'your string here')
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top