Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save marcinn/7467397 to your computer and use it in GitHub Desktop.
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.
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