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 | |
Generate random passwords on your local computer. | |
Usage: $ python filename.py [num] | |
Option: num: Password characters number. Default 16. num > 3 (with special chars, > 4) | |
- Type of characters: Number, uppercase, lowercase. At least once. | |
- Option: Set special_characters variable when using symbols or specific characters | |
Output: Genarated password is copied to clipboard (not shown in console) | |
""" | |
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) | |
def hasSymbols(inputString): | |
if special_characters: | |
return any(char in special_characters for char in inputString) | |
else: | |
return 1 | |
def generate_password(char_num): | |
if char_num < char_threshold: | |
print(f"Error: Required char_num >= {char_threshold}") | |
sys.exit() | |
# assert all types are included | |
while 1: | |
x = "".join( | |
random.choices( | |
string.ascii_letters + string.digits + special_characters, k=char_num | |
) | |
) | |
if hasNumbers(x) * hasUppercase(x) * hasLowercase(x) * hasSymbols(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 | |
try: | |
assert char_num >= char_threshold | |
except AssertionError: | |
print( | |
f"Error. Command is like `python filename.py num`. num >= {char_threshold}, instead of {char_num}" | |
) | |
sys.exit() | |
# overwrite char_num if arg is set | |
try: | |
set_num = int(sys.argv[1]) | |
assert set_num >= char_threshold | |
char_num = set_num | |
except (IndexError): | |
pass # sys.argv[1] is not set. | |
except (AssertionError, ValueError): | |
print( | |
f"Error. Command is like `python filename.py 16`. Number >= {char_threshold}" | |
) | |
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__": | |
special_characters = "" | |
# special_characters = " !\"#$%&'()*+,-./:;<=>?@[\]^_`{|}~" | |
if special_characters: | |
char_threshold = 4 | |
else: | |
char_threshold = 3 | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment