hash unicode string in python

后端 未结 3 2096
难免孤独
难免孤独 2021-02-06 21:07

I try to hash some unicode strings:

hashlib.sha1(s).hexdigest()
UnicodeEncodeError: \'ascii\' codec can\'t encode characters in position 0-81: 
ordinal not in ra         


        
3条回答
  •  情话喂你
    2021-02-06 21:33

    Use encoding format utf-8, Try this easy way,

    >>> import hashlib
    >>> hashlib.sha256(str(random.getrandbits(256)).encode('utf-8')).hexdigest()
    'cd183a211ed2434eac4f31b317c573c50e6c24e3a28b82ddcb0bf8bedf387a9f'
    

提交回复
热议问题