hash_password 914 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. #!/usr/bin/env python
  2. import argparse
  3. import bcrypt
  4. import getpass
  5. bcrypt_rounds=12
  6. def prompt_for_pass():
  7. password = getpass.getpass("Password: ")
  8. if not password:
  9. raise Exception("Password cannot be blank.")
  10. confirm_password = getpass.getpass("Confirm password: ")
  11. if password != confirm_password:
  12. raise Exception("Passwords do not match.")
  13. return password
  14. if __name__ == "__main__":
  15. parser = argparse.ArgumentParser(
  16. description="Calculate the hash of a new password, so that passwords"
  17. " can be reset")
  18. parser.add_argument(
  19. "-p", "--password",
  20. default=None,
  21. help="New password for user. Will prompt if omitted.",
  22. )
  23. args = parser.parse_args()
  24. password = args.password
  25. if not password:
  26. password = prompt_for_pass()
  27. print bcrypt.hashpw(password, bcrypt.gensalt(bcrypt_rounds))