Skip to content

Commit b402970

Browse files
authored
Hash, verify and save password
1 parent 64eb1b2 commit b402970

1 file changed

Lines changed: 41 additions & 0 deletions

File tree

Cryptography/password.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
from zxcvbn import zxcvbn
2+
from getpass import getpass
3+
import bcrypt
4+
5+
def password_strength(password):
6+
result = zxcvbn(password)
7+
score = result['score'] # 'Score' - (0 to 4)
8+
9+
if score==3:
10+
response = "Strong enough passsword: score of 3"
11+
elif score == 4:
12+
response = "Very strong password: score of 4"
13+
else:
14+
feedback = result.get('feedback')
15+
warning = feedback.get('warning')
16+
suggestions = feedback.get('suggestions')
17+
response = f"Weak password: score of {score}\nWarning: {warning}\nSuggestions: {suggestions[0]}"
18+
return response
19+
20+
def hash_password(password):
21+
salt = bcrypt.gensalt()
22+
hashed = bcrypt.hashpw(password.encode(), salt)
23+
return hashed
24+
25+
def verify_password(password_attempt, hashed):
26+
if bcrypt.checkpw(password_attempt.encode(), hashed):
27+
return "Password is correct. Access granted!"
28+
return "Incorrect password. Access denied!"
29+
30+
if __name__ == "__main__":
31+
while True:
32+
password1 = getpass("Enter a password to check strength: ")
33+
print(password_strength(password1))
34+
if password_strength(password1).startswith("Weak"):
35+
print("Choose a stronger password.")
36+
else:
37+
break
38+
hashed_password = hash_password(password1)
39+
print("Hashed password: ", hashed_password)
40+
attempt = getpass("Re-enter the password to verify: ")
41+
print(verify_password(attempt, hashed_password))

0 commit comments

Comments
 (0)