Password Generator
12
lowercase
uppercase
numbers
symbols
Generate Password
Copy to Clipboard
import string import secrets import math from pyodide.ffi import create_proxy from js import document, alert def get_characters(): chars = '' if document.querySelector('#lowercase').checked: chars += string.ascii_lowercase if document.querySelector('#uppercase').checked: chars += string.ascii_uppercase if document.querySelector('#digits').checked: chars += string.digits if document.querySelector('#special').checked: chars += string.punctuation return chars def create_password(length, characters): return ''.join(secrets.choice(characters) for _ in range(length)) def get_entropy(length, character_number): try: entropy = length * math.log2(character_number) except ValueError: return 0.0 return round(entropy, 2) def get_strength(entropy): if entropy >= 120: return "Excellent" elif entropy >= 70: return "Strong" elif entropy >= 50: return "Good" elif entropy >= 30: return "Weak" else: return "Pathetic" def generate_password(event=None): length = int(document.querySelector('#length').value) chars = get_characters() if not chars: document.querySelector('#password').value = "Please select at least one character type" return password = create_password(length, chars) document.querySelector('#password').value = password entropy = get_entropy(length, len(chars)) document.querySelector('#entropy').textContent = f"Entropy: {entropy} bits" strength = get_strength(entropy) document.querySelector('#strength').textContent = f"Strength: {strength}" def copy_password(event=None): password = document.querySelector('#password').value document.querySelector('#password').select() document.execCommand('copy') alert("Password copied to clipboard!") def update_length_value(event): document.querySelector('#length-value').textContent = event.target.value # Set up event listeners generate_proxy = create_proxy(generate_password) copy_proxy = create_proxy(copy_password) update_length_proxy = create_proxy(update_length_value) document.querySelector('#generate').addEventListener("click", generate_proxy) document.querySelector('#copy').addEventListener("click", copy_proxy) document.querySelector('#length').addEventListener("input", update_length_proxy) # Add event listeners to checkboxes checkboxes = document.querySelectorAll('input[type="checkbox"]') for checkbox in checkboxes: checkbox.addEventListener("change", generate_proxy) # Generate initial password generate_password()