Created
February 9, 2025 12:54
-
-
Save VRamazing/49a8424c63a44d273e62c49de85c891b to your computer and use it in GitHub Desktop.
Password generator
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import re | |
import secrets | |
import string | |
def generate_password(length=16, nums=1, special_chars=1, uppercase=1, lowercase=1): | |
# Define the possible characters for the password | |
letters = string.ascii_letters | |
digits = string.digits | |
symbols = string.punctuation | |
# Combine all characters | |
all_characters = letters + digits + symbols | |
while True: | |
password = '' | |
# Generate password | |
for _ in range(length): | |
password += secrets.choice(all_characters) | |
constraints = [ | |
(nums, r'\d'), | |
(special_chars, fr'[{symbols}]'), | |
(uppercase, r'[A-Z]'), | |
(lowercase, r'[a-z]') | |
] | |
# Check constraints | |
# Returns generator instead of list for optimizations | |
if all( | |
constraint <= len(re.findall(pattern, password)) | |
for constraint, pattern in constraints | |
): | |
break | |
return password | |
if __name__ == '__main__': | |
new_password = generate_password() | |
print('Generated password:', new_password) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment