Skip to content

Commit d2882fa

Browse files
author
Kostas Livieratos
committed
used a more secure method to check for password breach
1 parent f3adcad commit d2882fa

File tree

1 file changed

+7
-4
lines changed

1 file changed

+7
-4
lines changed

django_pwned_validator/validators.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,24 @@
11
import requests
2+
import hashlib
23

34
from django.core.exceptions import ValidationError
45
from django.utils.translation import ugettext as _
56

67
PWNED_ENDPOINT = 'https://api.pwnedpasswords.com/'
7-
PWNED_PASSWORD_CHECK_PATH = 'pwnedpassword/'
8+
PWNED_PASSWORD_CHECK_PATH = 'range/'
89

910

1011
class PwnedPasswordValidator(object):
1112

1213
def _exists_as_pwned(self, password):
13-
url = PWNED_ENDPOINT + PWNED_PASSWORD_CHECK_PATH + password
14+
hash = hashlib.sha1(password.encode("utf8")).hexdigest().upper()
15+
head, rest = hash[:5], hash[5:]
16+
url = PWNED_ENDPOINT + PWNED_PASSWORD_CHECK_PATH + head
1417
req = requests.get(url)
15-
if req.status_code == 200:
18+
if rest in req.content.decode('utf-8'):
1619
# password found in pwned db
1720
return True
18-
elif req.status_code >= 400:
21+
else:
1922
return False
2023

2124
def validate(self, password, *args, **kwargs):

0 commit comments

Comments
 (0)