hash_password.py 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. import argparse
  2. import getpass
  3. from lightrag.api.passwords import hash_password
  4. def build_parser() -> argparse.ArgumentParser:
  5. parser = argparse.ArgumentParser(
  6. description="Generate a bcrypt password value for AUTH_ACCOUNTS."
  7. )
  8. parser.add_argument(
  9. "password",
  10. nargs="?",
  11. help="Password to hash. If omitted, a secure prompt is used.",
  12. )
  13. parser.add_argument(
  14. "--username",
  15. help="Optional username. When provided, output is ready to paste into AUTH_ACCOUNTS.",
  16. )
  17. return parser
  18. def main(argv: list[str] | None = None) -> int:
  19. parser = build_parser()
  20. args = parser.parse_args(argv)
  21. password = args.password or getpass.getpass("Password: ")
  22. if not password:
  23. parser.error("password cannot be empty")
  24. hashed_password = hash_password(password)
  25. if args.username:
  26. print(f"{args.username}:{hashed_password}")
  27. else:
  28. print(hashed_password)
  29. return 0
  30. if __name__ == "__main__":
  31. raise SystemExit(main())