问题描述
我使用的是 Python 3 我有一个用户名映射到密码的 Auth_INI ConfigParser 文件。 我需要加密密码用密码这样只有密码才能解密它。
> Auth_INI = ConfigParser()
> password = 'password'
> password_encrypted = encrypt(password,'password')
> Auth_INI['username'] = password_encrypted
它所需要的可能只是某种困难的散列/非散列编码。我不需要任何东西 太花哨了。没有钥匙圈,只有纯文本编码/未编码。
使用这种加密方案,我可以轻松地通过 decrypt(password,'guessword') != 'guessword'
验证一个人,除此之外没有其他理由了。
有人可以推荐一个像这样工作的加密/编码库吗?
现在我只是使用 urlsafe_b64encode('password') 并且没有密码来解码密码,所以基本上没有密码加密。请帮忙...
解决方法
从 PyNaCl 文档中,这里有一个简单粗暴的例子:
import nacl.pwhash
# Password used to hash itself
orig_password = b'my password'
# Hashing the password
hashed_data = nacl.pwhash.str(orig_password)
# The result will be True on password match.
res = nacl.pwhash.verify(hashed_data,orig_password)
print(res)
# On mismatch an exception will be raised
wrong_password = b'My password'
res2 = nacl.pwhash.verify(hashed_data,wrong_password)