Last active
December 28, 2015 07:49
-
-
Save marcinn/7467397 to your computer and use it in GitHub Desktop.
Veryfing user`s password outside Django env. I'm using it with Pyramid.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django.contrib.auth import hashers | |
HASHERS = dict(map(lambda x: (x.algorithm, x), [ | |
hashers.SHA1PasswordHasher, | |
hashers.MD5PasswordHasher, | |
hashers.UnsaltedMD5PasswordHasher, | |
hashers.CryptPasswordHasher, | |
hashers.BCryptPasswordHasher, | |
hashers.PBKDF2SHA1PasswordHasher, | |
hashers.PBKDF2PasswordHasher])) | |
def check_password(password, encoded): | |
password = str(password) | |
encoded = str(encoded) | |
if len(encoded) == 32 and '$' not in encoded: | |
hasher_cls = HASHERS.get('unsalted_md5') | |
else: | |
algorithm = encoded.split('$', 1)[0] | |
hasher_cls = HASHERS.get(algorithm) | |
hasher = hasher_cls() | |
return hasher.verify(password, encoded) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment