forked from freeCodeCamp/boilerplate-SHA-1-password-cracker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpassword_cracker.py
30 lines (26 loc) · 1.09 KB
/
password_cracker.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import hashlib
import re
def crack_sha1_hash(hash, use_salts = False):
pattern = re.compile(r'^[a-fA-F0-9]{40}$') # Regular expression pattern for SHA1 hash
if not pattern.match(hash):
return "INVALID HASH"
with open('known-salts.txt', 'r') as salts_fd:
salts = salts_fd.readlines()
with open('top-10000-passwords.txt', 'r') as passwords:
for password in passwords:
password = password.strip()
if use_salts:
for salt in salts:
salt = salt.strip()
if hash_password(password, salt) == hash:
return password
if hash_password(password, salt, False) == hash:
return password
else:
if hash_password(password) == hash:
return password
return "PASSWORD NOT IN DATABASE"
def hash_password(password, salt = None, prepend = True):
if salt is not None:
password = (salt + password) if prepend else (password + salt)
return hashlib.sha1(password.encode()).hexdigest()