如何在 Python 中对字符串进行哈希处理

cop*_*ode 0 python

我只想在 Python 3 中对字符串(密码)进行哈希处理。我该怎么做?这样做的简单方法是什么?您能否提供一个编码示例或推荐一个可以使用的模块。

urw*_*iii 5

您可以使用 Hashlib 在 Python 3 中对值进行哈希处理:

import hashlib
h = hashlib.new('sha256')#sha256 can be replaced with diffrent algorithms
h.update('Hello World'.encode()) #give a encoded string. Makes the String to the Hash 
print(h.hexdigest())#Prints the Hash
Run Code Online (Sandbox Code Playgroud)

  • hashlib 是核心 python 的一部分 - 不是吗?(为什么要 pip 安装?) (2认同)