From 41fe65cf0bad7e159723a2ad4b7af29097e702b8 Mon Sep 17 00:00:00 2001 From: adityatahiliani Date: Wed, 23 Oct 2024 17:24:29 +0530 Subject: [PATCH] added password generator file and function --- password_generator.py | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 password_generator.py diff --git a/password_generator.py b/password_generator.py new file mode 100644 index 0000000..973fe3b --- /dev/null +++ b/password_generator.py @@ -0,0 +1,22 @@ +import random +import string + +def generate_password(length=12, use_special_chars=True): + """Generate a random password. + + Args: + length (int): Length of the password. + use_special_chars (bool): Whether to include special characters. + + Returns: + str: Generated password. + """ + if length < 4: + raise ValueError("Password length must be at least 4 characters.") + + characters = string.ascii_letters + string.digits + if use_special_chars: + characters += string.punctuation + + password = ''.join(random.choice(characters) for _ in range(length)) + return password