2017-12-18 2 views
0

파이썬과 플라스크를 사용하여 webapp를 개발하고 있습니다. 그것에는 사용자 시스템이 있습니다. 물론 등록 양식이 있습니다. passwib.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!') 

가, 해시는 항상 동일한 암호가 inputed 된 경우에도, 다양한있다. 왜 아무도 알지 못해? 내가 도대체 ​​뭘 잘못하고있는 겁니까?

+0

소금 https://en.wikipedia.org/wiki/Salt_(cryptography). 인증을 처리 할 정도로 자격이 있다고 확신합니까? –

+0

"자격이 있음"이 의미하는 바는 – MisterMM23

+0

입니다. 그러나 무작위 데이터를 추가하는 프로그램은 작성하지 않았습니다. 이건 파이썬의 sha256에 새로운가요? – MisterMM23

답변

1

Fristly, 그래서 당신이 해시를 만들기위한

hash = sha256_crypt.hash("password") 

sha256_crypt.encrypt(..) 버전 1.7 이후 사용되지 않으며 대신 sha256_crypt.hash(..)로 이름이 변경되는 점에 유의하시기 바랍니다. 해시는 임의의 소금을 포함, 당신은 해시를 다시 계산하고 비교, 대신 테이블에 해시를 조회해야하고, 다음에 사용할 수 없습니다 sha256_crypt.verify() 같은 : 당신은 개념에 대해 알아 발견

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

은 데이터베이스 또는 내장 유형'hash'에서 추출 된 해시를 나타내는 _hash입니다. – MisterMM23

+0

성명 :'hash = sha256_crypt.hash ("password")'나는 문서에서 직접 취했다. 첫 번째'hash '는'hash (..)'methode 호출에 의해 수행 된 계산의 결과이며, 데이터베이스에 저장되어야하는 것이다. –

+0

좋아. 그것은 효과가있다! 고마워요! – MisterMM23

-1

대신 pycrypto .sha256을 시도해보십시오. passlib은 무염 해시를 계산하기위한 올바른 솔루션이 아닙니다.

관련 문제