-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
42 lines (34 loc) · 1.33 KB
/
app.py
File metadata and controls
42 lines (34 loc) · 1.33 KB
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
31
32
33
34
35
36
37
38
39
40
41
42
from flask import Flask, render_template, request
import requests
import hashlib
app = Flask(__name__)
def request_api_data(query_char):
url = 'https://api.pwnedpasswords.com/range/' + query_char
res = requests.get(url)
if res.status_code != 200:
raise RuntimeError(f'Error fetching: {res.status_code}, check the API and try again')
return res
def get_password_leaks_count(hashes, hash_to_check):
hashes = (line.split(':') for line in hashes.text.splitlines())
for h, count in hashes:
if h == hash_to_check:
return count
return 0
def pwned_api_check(password):
sha1password = hashlib.sha1(password.encode('utf-8')).hexdigest().upper()
first5_char, tail = sha1password[:5], sha1password[5:]
response = request_api_data(first5_char)
return get_password_leaks_count(response, tail)
@app.route('/', methods=['GET', 'POST'])
def check_password():
result = None
if request.method == 'POST':
password = request.form['password']
count = pwned_api_check(password)
if count:
result = f'{password} was found {count} times... you should probably change your password!'
else:
result = f'{password} was NOT found. Carry on!'
return render_template('index.html', result=result)
if __name__ == '__main__':
app.run(debug=True)