Chr*_*row 75 python authentication passwords hash salt
这段代码应该用盐哈希密码.salt和哈希密码正在保存在数据库中.密码本身不是.
考虑到操作的敏感性,我想确保一切都是犹太洁食.
注意:我习惯使用url安全版的b64encode.
import hashlib
import base64
import uuid
password = 'test_password'
salt = base64.urlsafe_b64encode(uuid.uuid4().bytes)
t_sha = hashlib.sha512()
t_sha.update(password+salt)
hashed_password = base64.urlsafe_b64encode(t_sha.digest())
Run Code Online (Sandbox Code Playgroud)
Chr*_*row 53
根据这个问题的其他答案,我使用bcrypt实现了一种新方法.
如果我理解正确,使用的说法bcrypt在SHA512是bcrypt被设计成缓慢.bcrypt还可以选择在第一次生成散列密码时调整您希望的速度:
# The '12' is the number that dictates the 'slowness'
bcrypt.hashpw(password, bcrypt.gensalt( 12 ))
Run Code Online (Sandbox Code Playgroud)
慢是可取的,因为如果恶意方获取包含散列密码的表,那么对它们进行解密则要困难得多.
def get_hashed_password(plain_text_password):
# Hash a password for the first time
# (Using bcrypt, the salt is saved into the hash itself)
return bcrypt.hashpw(plain_text_password, bcrypt.gensalt())
def check_password(plain_text_password, hashed_password):
# Check hashed password. Using bcrypt, the salt is saved into the hash itself
return bcrypt.checkpw(plain_text_password, hashed_password)
Run Code Online (Sandbox Code Playgroud)
我可以使用以下方法在Linux系统中轻松安装库:
pip install py-bcrypt
Run Code Online (Sandbox Code Playgroud)
但是,我在Windows系统上安装它时遇到了更多麻烦.它似乎需要一个补丁.看到这个Stackoverflow问题:在win 7 64bit python上安装py-bcrypt
M.D*_*.D. 47
聪明的事情不是自己编写加密,而是使用像passlib这样的东西:https://bitbucket.org/ecollins/passlib/wiki/Home
以安全的方式编写加密代码很容易.令人讨厌的是,使用非加密代码,当程序崩溃时,它会在它无法正常工作时立即注意到它.使用加密代码时,您通常只能在迟到之后发现并且您的数据已被泄露.因此,我认为最好使用由其他人知道的关于该主题并且基于战斗测试协议的其他人编写的包.
passlib还有一些很好的功能,使它易于使用,并且如果旧协议被破坏,也很容易升级到更新的密码散列协议.
此外,只有一轮sha512更容易受到字典攻击.sha512设计得很快,这在尝试安全存储密码时实际上是一件坏事.其他人一直在思考所有这些问题,所以你最好利用这个.
Tay*_*mon 38
编辑:这个答案是错误的.不要使用加密哈希来存储密码.使用密码哈希.
看起来很好我.但是,我很确定你实际上并不需要base64.你可以这样做:
import hashlib, uuid
salt = uuid.uuid4().hex
hashed_password = hashlib.sha512(password + salt).hexdigest()
Run Code Online (Sandbox Code Playgroud)
如果它不会造成困难,您可以通过将salt和哈希密码存储为原始字节而不是十六进制字符串来在数据库中获得稍高效的存储.要做到这一点,更换hex用bytes和hexdigest用digest.
Mar*_*ery 21
从 Python 3.4 开始,hashlib标准库中的模块包含“专为安全密码散列设计”的密钥派生函数。
因此,使用其中之一,例如hashlib.pbkdf2_hmac,使用生成的盐os.urandom:
from typing import Tuple
import os
import hashlib
import hmac
def hash_new_password(password: str) -> Tuple[bytes, bytes]:
"""
Hash the provided password with a randomly-generated salt and return the
salt and hash to store in the database.
"""
salt = os.urandom(16)
pw_hash = hashlib.pbkdf2_hmac('sha256', password.encode(), salt, 100000)
return salt, pw_hash
def is_correct_password(salt: bytes, pw_hash: bytes, password: str) -> bool:
"""
Given a previously-stored salt and hash, and a password provided by a user
trying to log in, check whether the password is correct.
"""
return hmac.compare_digest(
pw_hash,
hashlib.pbkdf2_hmac('sha256', password.encode(), salt, 100000)
)
# Example usage:
salt, pw_hash = hash_new_password('correct horse battery staple')
assert is_correct_password(salt, pw_hash, 'correct horse battery staple')
assert not is_correct_password(salt, pw_hash, 'Tr0ub4dor&3')
assert not is_correct_password(salt, pw_hash, 'rosebud')
Run Code Online (Sandbox Code Playgroud)
注意:
os.urandom 始终使用加密安全的随机源hmac.compare_digest中使用的is_correct_password, 基本上只是==字符串的运算符,但没有短路的能力,这使得它不受计时攻击的影响。这可能并没有真正提供任何额外的安全价值,但它也没有伤害,所以我继续使用它。有关什么是好的密码散列的理论以及适用于散列密码的其他函数列表,请参阅https://security.stackexchange.com/q/211/29805。
小智 19
为了在Python 3中工作,您需要UTF-8编码,例如:
hashed_password = hashlib.sha512(password.encode('utf-8') + salt.encode('utf-8')).hexdigest()
Run Code Online (Sandbox Code Playgroud)
否则你会得到:
回溯(最近一次调用最后一次):
文件"",第1行,在
hashed_password = hashlib.sha512(密码+盐).hexdigest()
TypeError:必须在散列之前对Unicode对象进行编码
Ter*_*way 10
如果您需要使用现有系统存储的哈希值,则passlib似乎很有用.如果您可以控制格式,请使用像bcrypt或scrypt这样的现代哈希.这时,bcrypt似乎更容易从python中使用.
passlib支持bcrypt,它建议安装py-bcrypt作为后端:http://pythonhosted.org/passlib/lib/passlib.hash.bcrypt.html
如果您不想安装passlib,也可以直接使用py-bcrypt.自述文件包含基本用法示例.
另请参见:如何使用scrypt在Python中生成密码和salt的哈希值
我不想复活旧线程,但是......任何想要使用现代最新安全解决方案的人都会使用argon2.
https://pypi.python.org/pypi/argon2_cffi
它赢得了密码哈希竞争.(https://password-hashing.net/)它比bcrypt更容易使用,并且比bcrypt更安全.
| 归档时间: |
|
| 查看次数: |
99406 次 |
| 最近记录: |