Last active
August 11, 2021 00:02
-
-
Save shimo164/c930449c0716d07ddd644c18b3d71692 to your computer and use it in GitHub Desktop.
Python script for generating password on your local machine.
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
| """Password generater | |
| Create random passwords | |
| - Include number, uppercase, lowercase, at least once. | |
| - Default num = 16 | |
| - Genarated password is copied to clipboard (not shown in console) | |
| Usage: $python generate_password.py [num] | |
| [num] is option for setting a number of chars. num >= 3. | |
| """ | |
| import random | |
| import string | |
| import sys | |
| def hasNumbers(inputString): | |
| return any(char.isdigit() for char in inputString) | |
| def hasUppercase(inputString): | |
| return any(char.isupper() for char in inputString) | |
| def hasLowercase(inputString): | |
| return any(char.islower() for char in inputString) | |
| # require upper, lower, number | |
| def generate_password(char_num): | |
| if char_num < 3: | |
| print("Erorr: Required char_num >= 3") | |
| return | |
| while 1: | |
| x = "".join(random.choices(string.ascii_letters + string.digits, k=char_num)) | |
| if hasNumbers(x) * hasUppercase(x) * hasLowercase(x): | |
| return x | |
| def send_to_clipboard(text): | |
| try: | |
| from Tkinter import Tk | |
| except ImportError: | |
| from tkinter import Tk | |
| r = Tk() | |
| r.withdraw() | |
| r.clipboard_clear() | |
| r.clipboard_append(text) | |
| r.update() # now it stays on the clipboard after the window is closed | |
| r.destroy() | |
| def main(): | |
| char_num = 16 # default | |
| # overwrite char_num if arg is set | |
| try: | |
| set_num = int(sys.argv[1]) | |
| assert set_num >= 3 | |
| char_num = set_num | |
| except (IndexError): | |
| pass # sys.argv[1] is not set. Use default. | |
| except (AssertionError, ValueError): | |
| print("Error. Command is like `python filename.py 16`. Number >= 3") | |
| sys.exit() | |
| my_password = generate_password(char_num) | |
| send_to_clipboard(my_password) | |
| print(f"Generated {char_num} char password was sent to clipboard!") | |
| if __name__ == "__main__": | |
| main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment