Skip to content

Instantly share code, notes, and snippets.

@shimo164
Last active August 11, 2021 00:02
Show Gist options
  • Select an option

  • Save shimo164/c930449c0716d07ddd644c18b3d71692 to your computer and use it in GitHub Desktop.

Select an option

Save shimo164/c930449c0716d07ddd644c18b3d71692 to your computer and use it in GitHub Desktop.

Revisions

  1. shimo164 revised this gist Aug 11, 2021. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion generate_password.py
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,5 @@
    """Password generater
    Create random passwords on your local computer.
    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)
  2. shimo164 revised this gist Aug 11, 2021. 1 changed file with 9 additions and 8 deletions.
    17 changes: 9 additions & 8 deletions generate_password.py
    Original file line number Diff line number Diff line change
    @@ -1,12 +1,13 @@
    """Password generater
    Create random passwords on your local computer.
    Create random passwords
    - Include number, uppercase, lowercase, at least once.
    - Set special_characters variable if you want to use symbols or specific chars
    - Default char number is 16.
    - Set num as arg like $python file.py 24
    - Genarated password is copied to clipboard (not shown in console)
    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
    @@ -25,7 +26,7 @@ def hasLowercase(inputString):
    return any(char.islower() for char in inputString)


    def hasSymbol(inputString):
    def hasSymbols(inputString):
    if special_characters:
    return any(char in special_characters for char in inputString)
    else:
    @@ -44,7 +45,7 @@ def generate_password(char_num):
    string.ascii_letters + string.digits + special_characters, k=char_num
    )
    )
    if hasNumbers(x) * hasUppercase(x) * hasLowercase(x) * hasSymbol(x):
    if hasNumbers(x) * hasUppercase(x) * hasLowercase(x) * hasSymbols(x):
    return x


  3. shimo164 revised this gist Aug 10, 2021. 1 changed file with 39 additions and 14 deletions.
    53 changes: 39 additions & 14 deletions generate_password.py
    Original file line number Diff line number Diff line change
    @@ -2,12 +2,11 @@
    Create random passwords
    - Include number, uppercase, lowercase, at least once.
    - Default num = 16
    - Set special_characters variable if you want to use symbols or specific chars
    - Default char number is 16.
    - Set num as arg like $python file.py 24
    - 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
    @@ -26,16 +25,26 @@ def hasLowercase(inputString):
    return any(char.islower() for char in inputString)


    # require upper, lower, number
    def generate_password(char_num):
    def hasSymbol(inputString):
    if special_characters:
    return any(char in special_characters for char in inputString)
    else:
    return 1

    if char_num < 3:
    print("Erorr: Required char_num >= 3")
    return

    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, k=char_num))
    if hasNumbers(x) * hasUppercase(x) * hasLowercase(x):
    x = "".join(
    random.choices(
    string.ascii_letters + string.digits + special_characters, k=char_num
    )
    )
    if hasNumbers(x) * hasUppercase(x) * hasLowercase(x) * hasSymbol(x):
    return x


    @@ -55,15 +64,25 @@ def send_to_clipboard(text):
    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 >= 3
    assert set_num >= char_threshold
    char_num = set_num
    except (IndexError):
    pass # sys.argv[1] is not set. Use default.
    pass # sys.argv[1] is not set.
    except (AssertionError, ValueError):
    print("Error. Command is like `python filename.py 16`. Number >= 3")
    print(
    f"Error. Command is like `python filename.py 16`. Number >= {char_threshold}"
    )
    sys.exit()

    my_password = generate_password(char_num)
    @@ -72,5 +91,11 @@ def main():


    if __name__ == "__main__":
    special_characters = ""
    # special_characters = " !\"#$%&'()*+,-./:;<=>?@[\]^_`{|}~"

    if special_characters:
    char_threshold = 4
    else:
    char_threshold = 3
    main()
  4. shimo164 created this gist Jul 31, 2021.
    76 changes: 76 additions & 0 deletions generate_password.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,76 @@
    """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()