#!/usr/bin/env python3 """ Password Hash Generator for streamlit-authenticator Usage: python generate_password_hash.py This will prompt you for a password and generate the bcrypt hash. """ import streamlit_authenticator as stauth def generate_hash(): print("=" * 50) print("Password Hash Generator") print("=" * 50) print() # Get password from user password = input("Enter password to hash: ").strip() if not password: print("Password cannot be empty!") return # Generate hash print("\nGenerating hash...") hashed_passwords = stauth.Hasher([password]).generate() hash_value = hashed_passwords[0] print("\nHash generated successfully!") print("=" * 50) print(f"\nYour hashed password:\n{hash_value}") print("=" * 50) # Show example usage print("\nAdd to config.yaml:") print("-" * 50) print(f""" credentials: usernames: username_here: email: user@example.com name: User Name password: {hash_value} """) print("\nOr add to secrets.toml:") print("-" * 50) print(f""" [auth.credentials.usernames.username_here] email = "user@example.com" name = "User Name" password = "{hash_value}" """) print("\nDone! Copy the hash above to your config file.") def generate_multiple(): print("=" * 50) print("Multiple User Password Hash Generator") print("=" * 50) print() users = {} while True: username = input("\nEnter username (or press Enter to finish): ").strip() if not username: break password = input(f"Enter password for {username}: ").strip() if not password: print("Password cannot be empty! Skipping user.") continue email = input(f"Enter email for {username}: ").strip() name = input(f"Enter full name for {username}: ").strip() users[username] = { 'password': password, 'email': email or f"{username}@example.com", 'name': name or username.title() } if not users: print("\n No users to process!") return # Generate all hashes print("\nGenerating hashes...") passwords = [data['password'] for data in users.values()] hashed_passwords = stauth.Hasher(passwords).generate() # Update users with hashed passwords for i, username in enumerate(users.keys()): users[username]['hashed'] = hashed_passwords[i] # Display results print("\nHashes generated successfully!") print("=" * 50) print("\nconfig.yaml format:") print("-" * 50) print("credentials:") print(" usernames:") for username, data in users.items(): print(f" {username}:") print(f" email: {data['email']}") print(f" name: {data['name']}") print(f" password: {data['hashed']}") print("\nsecrets.toml format:") print("-" * 50) for username, data in users.items(): print(f"[auth.credentials.usernames.{username}]") print(f'email = "{data["email"]}"') print(f'name = "{data["name"]}"') print(f'password = "{data["hashed"]}"') print() print("Done! Copy the configuration above to your config file.") if __name__ == "__main__": print("\nChoose an option:") print("1. Generate single password hash") print("2. Generate multiple user hashes") choice = input("\nEnter choice (1 or 2): ").strip() if choice == "1": generate_hash() elif choice == "2": generate_multiple() else: print("Invalid choice!")