Hash function that produces short hashes?

后端 未结 10 1715
走了就别回头了
走了就别回头了 2020-12-07 23:58

Is there a way of encryption that can take a string of any length and produce a sub-10-character hash? I want to produce reasonably unique ID\'s but based on message content

10条回答
  •  忘掉有多难
    2020-12-08 00:28

    You can use any commonly available hash algorithm (eg. SHA-1), which will give you a slightly longer result than what you need. Simply truncate the result to the desired length, which may be good enough.

    For example, in Python:

    >>> import hashlib
    >>> hash = hashlib.sha1("my message".encode("UTF-8")).hexdigest()
    >>> hash
    '104ab42f1193c336aa2cf08a2c946d5c6fd0fcdb'
    >>> hash[:10]
    '104ab42f11'
    

提交回复
热议问题