Last active
May 21, 2023 16:35
-
-
Save n05tr0m0/e873471a144a2b58cfa7d3f1bf7ce8fa to your computer and use it in GitHub Desktop.
A simple password generator for CLI with automatic copying of the generated password to clipboard. Don't forget to give the rights to execute.
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
#!/usr/bin/env python3 | |
import argparse | |
import platform | |
import random | |
import subprocess | |
import sys | |
from string import ascii_letters, digits, punctuation | |
def parse_args() -> argparse.Namespace: | |
parser = argparse.ArgumentParser( | |
description="The program generates passwords with different length and complexity", | |
formatter_class=argparse.ArgumentDefaultsHelpFormatter, | |
) | |
parser.add_argument("-l", "--length", type=int, default=16, help="Set password length. Max = 99") | |
parser.add_argument( | |
"--special", | |
action=argparse.BooleanOptionalAction, | |
type=bool, | |
default=True, | |
help="Special characters switch, type no-special for false.", | |
) | |
return parser.parse_args() | |
def create_password(len_pas: int, spec_symbols: bool = True) -> str: | |
"""Make random password when run.""" | |
symbols = ascii_letters + digits | |
if spec_symbols: | |
symbols = symbols + punctuation | |
secure_random = random.SystemRandom() | |
password = "".join(secure_random.choice(symbols) for _ in range(len_pas)) | |
return password | |
def get_beautiful_answer(password: str) -> None: | |
"""Beautiful console output""" | |
if not password: | |
print("Password length not set") | |
sys.exit(0) | |
print("Your newly generated password:") | |
print("-" * len(password)) | |
print(password) | |
print("-" * len(password)) | |
print("Password copied to the clipboard.") | |
def get_clipboard() -> str: | |
clipboard_types = { | |
"Darwin": "pbcopy", | |
"Windows": "clip", | |
"Linux": "xsel", | |
} | |
os_type = platform.system() | |
return clipboard_types.get(os_type) | |
def main() -> None: | |
args = parse_args() | |
if args.length > 99: | |
args.length = 99 | |
new_password = create_password(args.length, spec_symbols=args.special) | |
clipboard = get_clipboard() | |
subprocess.run(clipboard, text=True, input=new_password) | |
get_beautiful_answer(new_password) | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment