2017-12-18 206 views
0

我正在开发一个使用python和flask的webapp。它有一个用户系统,当然还有一个注册表。我正在使用,加密要注册的用户的密码,passlib.hash.sha256。以下是我在做什么:sha256_crypt.encrypt总是返回另一个散列

from passlib.hash import sha256_crypt as sha256 
[...] 
if request.method == "POST" and form.validate(): 
    username = request.form['username'] 
    password = request.form['password'] 
    confirm_password = request.form['confirm_password'] 
    email = request.form['email'] 

    password = sha256.encrypt(password) #Encryption. 



    c, conn = connection('accounts') #Connection to the database 


    x = c.execute("SELECT * FROM accounts WHERE username = '%s' OR email = '%s'" %(thwart(username), thwart(email))) 

    if x: 
     flash("We are very sorry, but this Username/Email-address is already taken. Please try again") 
    else: 
     c.execute('INSERT INTO accounts VALUES ("%s", "%s", "%s")' %(thwart(username), thwart(password), thwart(email))) 
     conn.commit() 
     flash('Succesfully Registered!') 

在数据库中,即使输入了相同的密码,散列总是变化。有人知道为什么吗?我究竟做错了什么?

+0

你已经发现了这个概念盐https://en.wikipedia.org/wiki/Salt_(cryptography)。你确定你有足够的资格来处理认证吗? –

+0

你是什么意思“合格” – MisterMM23

+0

我明白了。但是我没有编写任何可以添加随机数据的程序。这是python的sha256的新功能吗? – MisterMM23

回答

1

Fristly,请注意自1.7版本sha256_crypt.encrypt(..)被弃用,而是改名为sha256_crypt.hash(..)所以你必须

hash = sha256_crypt.hash("password") 

创建哈希值。作为哈希包括随机盐,可以不重新计算哈希和比较,而不是你应该查找表中的散列值,然后在使用它sha256_crypt.verify(),如:

sha256_crypt.verify("password", hash) 
+0

是_hash_表示从数据库或内置类型'hash'中提取的散列? – MisterMM23

+0

声明:'hash = sha256_crypt.hash(“password”)'我直接从文档中取出。第一个'hash'是由'hash(..)'方法调用完成的计算的结果,并且是数据库中需要保留的内容。 –

+0

好的。有效!非常感谢你! – MisterMM23

-1

尝试pycrypto .sha256相反,passlib似乎不是您的要求计算未消隐哈希的正确解决方案。

相关问题