Last active
May 11, 2021 07:30
-
-
Save zulzeen/2f701c5aa39e18261089f3cc2584c2dd to your computer and use it in GitHub Desktop.
A random password generator
This file contains 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 math | |
import random | |
import string | |
import argparse | |
def generate_password(nb_of_char, min_special=0): | |
password = '' | |
for i in range(nb_of_char, 0, -1): | |
if min_special >= i: | |
choices = string.punctuation | |
min_special -= 1 | |
else: | |
choices = string.ascii_letters + string.digits + string.punctuation | |
password += random.choice(choices) | |
return password | |
def check_password_strength(password): | |
available_dictionaries = [string.digits, string.ascii_lowercase, string.ascii_uppercase, string.punctuation] | |
dictionary_size = 0 | |
for char in password: | |
for dictionary in available_dictionaries: | |
if char in dictionary: | |
dictionary_size += len(dictionary) | |
available_dictionaries.remove(dictionary) | |
return round(math.log2(dictionary_size ** len(password)), 2) | |
def main(): | |
parser = argparse.ArgumentParser(description="Generate random passwords") | |
parser.add_argument("--password-count", '-n', type=int, | |
help="number of passwords to generate", default=1) | |
parser.add_argument("--length", "-l", type=int, | |
help="Length of the passwords", | |
default=random.randint(4,30)) | |
parser.add_argument("--special-chars", "-s", type=int, | |
help="Minimum number of special characters", | |
default=random.randint(0,30)) | |
parser.add_argument("--show-strength", '-S', action='store_true', | |
help="display the strength of each generated password") | |
args = parser.parse_args() | |
for _ in range(args.password_count): | |
password = generate_password(args.length, args.special_chars) | |
output = password | |
if args.show_strength: | |
output = f"{password} {check_password_strength(password)}" | |
print(output) | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment