Created
August 15, 2018 22:53
-
-
Save rwev/2b73c009de5edb46dae268ea122bce17 to your computer and use it in GitHub Desktop.
Python script for generating (password) strings of specified length and character composition.
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
""" | |
PACC.PY: Passwords, Accidentally | |
Python script for generating (password) strings of specified length and character composition. | |
Author: rwev (https://github.com/rwev) | |
Built-in dependencies only. | |
See usage: | |
>python pacc.py --help | |
To make a portable executable of the program to be used throughout your development system, run: | |
>pip install pyinstaller | |
>pyinstaller pacc.py -F | |
and place the generated /dist/resurgence.exe in your system path. | |
""" | |
from __future__ import print_function | |
import re | |
import os, sys, subprocess | |
import time | |
from optparse import OptionParser | |
import string | |
import random | |
parser = OptionParser() | |
parser.add_option( | |
'-l', '--length', | |
dest = 'string_length', | |
type = 'int', | |
default = 12, | |
help = "Desired length of string. (Defaults to 12)" | |
) | |
parser.add_option( | |
'-s', '--specials', | |
action = 'store_true', | |
dest = 'use_special_characters', | |
default = False, | |
help = 'Include special characters. (Defaults to False)' | |
) | |
(options, args) = parser.parse_args(sys.argv) | |
char_choice_set = string.ascii_letters + string.digits | |
if (options.use_special_characters): | |
char_choice_set += string.punctuation | |
def chooseRandomCharacters(choice_set_str, num_chars_int): | |
'''Takes num_chars random characters from choice_set with the possibility of repetition. | |
''' | |
return ''.join(random.choice(choice_set_str) for _ in range(num_chars_int)) | |
def doStringsShareCharacters(string_1, string_2): | |
for c in string_1: | |
if c in string_2: | |
return True | |
return False | |
requirements_fulfilled = False | |
while not requirements_fulfilled: | |
random_string = chooseRandomCharacters(char_choice_set, options.string_length) | |
requirements_fulfilled = True | |
if not doStringsShareCharacters(random_string, string.lowercase): | |
requirements_fulfilled = False | |
if not doStringsShareCharacters(random_string, string.uppercase): | |
requirements_fulfilled = False | |
if not doStringsShareCharacters(random_string, string.digits): | |
requirements_fulfilled = False | |
if options.use_special_characters and not doStringsShareCharacters(random_string, string.punctuation): | |
requirements_fulfilled = False | |
print(random_string) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment