Нет описания

123456789101112131415161718192021222324252627282930
  1. # -*- encoding: utf-8 -*-
  2. """
  3. License: MIT
  4. Copyright (c) 2019 - present AppSeed.us
  5. """
  6. import hashlib, binascii, os
  7. # Inspiration -> https://www.vitoshacademy.com/hashing-passwords-in-python/
  8. def hash_pass( password ):
  9. """Hash a password for storing."""
  10. salt = hashlib.sha256(os.urandom(60)).hexdigest().encode('ascii')
  11. pwdhash = hashlib.pbkdf2_hmac('sha512', password.encode('utf-8'),
  12. salt, 100000)
  13. pwdhash = binascii.hexlify(pwdhash)
  14. return (salt + pwdhash) # return bytes
  15. def verify_pass(provided_password, stored_password):
  16. """Verify a stored password against one provided by user"""
  17. stored_password = stored_password.decode('ascii')
  18. salt = stored_password[:64]
  19. stored_password = stored_password[64:]
  20. pwdhash = hashlib.pbkdf2_hmac('sha512',
  21. provided_password.encode('utf-8'),
  22. salt.encode('ascii'),
  23. 100000)
  24. pwdhash = binascii.hexlify(pwdhash).decode('ascii')
  25. return pwdhash == stored_password