Chr*_*row 5 python hash sha1 hashlib
使用SHA1散列较大的字符串,以便它们可以用作数据库中的键.
尝试从原始字符串生成UUID大小的字符串,该字符串足够随机且足够大以防止冲突,但比原始字符串小得多.
不使用此安全相关的任何东西.
# 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()
Run Code Online (Sandbox Code Playgroud)
SHA1是一个很好的算法用于此目的吗?或者还有其他更合适的东西吗?
使用 SHA1 的版本是 UUIDv5,因此代码如下所示:
import uuid
uuid.uuid5(uuid.NAMESPACE_OID, 'your string here')
Run Code Online (Sandbox Code Playgroud)