python sha256 加盐
时间: 2023-09-03 22:14:46 浏览: 261
alipay:Python Alipay(支付宝) SDK with SHA1SHA256 support
在 Python 中使用 SHA256 加盐的方法如下:
```python
import hashlib
import os
def hash_password(password, salt=None):
if salt is None:
salt = os.urandom(16).hex() # 生成随机盐值
password = password.encode('utf-8')
salt = salt.encode('utf-8')
hashed_password = hashlib.sha256(salt + password).hexdigest()
return hashed_password, salt
# 示例用法
password = "mysecretpassword"
hashed_password, salt = hash_password(password)
print("Hashed Password:", hashed_password)
阅读全文