File tree 1 file changed +7
-4
lines changed 1 file changed +7
-4
lines changed Original file line number Diff line number Diff line change 1
1
import requests
2
+ import hashlib
2
3
3
4
from django .core .exceptions import ValidationError
4
5
from django .utils .translation import ugettext as _
5
6
6
7
PWNED_ENDPOINT = 'https://api.pwnedpasswords.com/'
7
- PWNED_PASSWORD_CHECK_PATH = 'pwnedpassword /'
8
+ PWNED_PASSWORD_CHECK_PATH = 'range /'
8
9
9
10
10
11
class PwnedPasswordValidator (object ):
11
12
12
13
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
14
17
req = requests .get (url )
15
- if req . status_code == 200 :
18
+ if rest in req . content . decode ( 'utf-8' ) :
16
19
# password found in pwned db
17
20
return True
18
- elif req . status_code >= 400 :
21
+ else :
19
22
return False
20
23
21
24
def validate (self , password , * args , ** kwargs ):
You can’t perform that action at this time.
0 commit comments