user.check_password('secret') return?from werkzeug.security import generate_password_hash, check_password_hash class User: def __init__(self, username, password): self.username = username self.password_hash = generate_password_hash(password) def check_password(self, password): return check_password_hash(self.password_hash, password) user = User('alice', 'secret') result = user.check_password('secret')
check_password_hash returns when the password matches.The check_password method returns True if the given password matches the stored hash. Since 'secret' is the correct password, the result is True.
password_hash. Which code snippet correctly implements this?from werkzeug.security import generate_password_hash class User: def __init__(self): self.password_hash = '' # password setter here
Option D uses @property and @password.setter correctly to hash the password when set. Option D is a method but not a setter property. Option D returns self.password which causes recursion. Option D is just a method without decorator.
user.check_password('secret') returns False even though the password is correct.from werkzeug.security import generate_password_hash, check_password_hash class User: def __init__(self, username, password): self.username = username self.password = password def check_password(self, password): return check_password_hash(self.password, password) user = User('bob', 'secret') result = user.check_password('secret')
self.password and what check_password_hash expects.The password is stored as plain text in self.password. The check_password_hash function expects a hashed password as the first argument. Since it receives plain text, it returns False.
user.password_hash after setting password?user.password_hash contain after user.password = 'mypassword'?from werkzeug.security import generate_password_hash class User: def __init__(self): self.password_hash = '' @property def password(self): raise AttributeError('Password is write-only') @password.setter def password(self, password): self.password_hash = generate_password_hash(password) user = User() user.password = 'mypassword' result = user.password_hash
The setter uses generate_password_hash to store a hashed version of the password. This hash string usually starts with 'pbkdf2:sha256:'.
Storing plain text passwords is risky because if attackers get database access, they see all passwords. Hashing passwords protects users by storing only irreversible hashes.