A hash function that protects against collisions, not attacks. (Produces a random UUID result space)

Use SHA1 to hash larger strings so that they can be used as keys in the database.

Trying to create a UUID-sized string from the original string, which is random enough and large enough to protect against collisions, but much smaller than the original string.

Do not use this for safety.

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 for this purpose? Or is there something even more suitable?

+5
source share
1 answer

Python has a uuidlibrary based on RFC 4122 .

The version using SHA1 is UUIDv5, so the code will be something like this:

import uuid

uuid.uuid5(uuid.NAMESPACE_OID, 'your string here')
+4
source

All Articles