Hash function that produces short hashes?

后端 未结 10 1687
走了就别回头了
走了就别回头了 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:21

    You can use the hashlib library for Python. The shake_128 and shake_256 algorithms provide variable length hashes. Here's some working code (Python3):

    import hashlib
    >>> my_string = 'hello shake'
    >>> hashlib.shake_256(my_string.encode()).hexdigest(5)
    '34177f6a0a'
    

    Notice that with a length parameter x (5 in example) the function returns a hash value of length 2x.

提交回复
热议问题