Jak porównać hasło tekstowe z hashowanym za pomocą bcrypt?
Chciałbym użyć bcrypt
do hashowania haseł, a następnie sprawdzenia, czy podane hasło jest poprawne.
Hashowanie haseł jest łatwe:
import bcrypt
password = u'foobar'
password_hashed = bcrypt.hashpw(password, bcrypt.gensalt())
# then store password_hashed in a database
Jak mogę porównać hasło tekstowe z przechowywanym Hashem?
4 answers
Z py-bcrypt, nie musisz przechowywać soli oddzielnie: bcrypt
przechowuje sól w haszu.
>>> import bcrypt
>>> salt = bcrypt.gensalt()
>>> hashed = bcrypt.hashpw('secret', salt)
>>> hashed.find(salt)
0
>>> hashed == bcrypt.hashpw('secret', hashed)
True
>>>
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-10-02 22:30:26
Dokumentacja nie wspomina o przechowywaniu soli, mówi, że wystarczy:
#Initial generation
hashed = bcrypt.hashpw(password, bcrypt.gensalt())
#Store hashed in your db
#Load hashed from the db and check the provided password
if bcrypt.hashpw(password, hashed) == hashed:
print "It matches"
else:
print "It does not match"
Później, powiedzmy, że masz hasło wejściowe użytkownika user_pass
. Możesz też to hashować, a następnie porównać hash z przechowywanym Hashem, a jeśli pasują, to oryginalne hasła również pasują.
Zauważ, że bcrypt automatycznie zapisuje wartość salt jako część zaszyfrowanego hasła, dzięki czemu możesz go użyć również podczas zaszyfrowania przyszłego wejścia.
Pierwszy raz:
import bcrypt
password = u'foobar'
salt = bcrypt.gensalt()
password_hashed = bcrypt.hashpw(password, salt)
# store 'password_hashed' in a database of your choosing
Późniejsze czasy:
import bcrypt
password = something_that_gets_input()
stored_hash = something_that_gets_this_from_the_db()
if bcrypt.hashpw(password, stored_hash) == stored_hash:
# password matches
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-05-01 15:29:50
Nie znam Pythona, ale myślę, że można użyć:
public static boolean checkpw (java.lang.String plaintext,
java.lang.String hashed)
// Check that an unencrypted password matches one that has
// previously been hashed.
if bcrypt.checkpw(plaintext, hashed):
print "It matches"
else:
print "It does not match"
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-06-16 08:56:41