Last active
September 3, 2016 02:30
-
-
Save nikcub/b70e78f0830338aa4fbcb6c1252b1d80 to your computer and use it in GitHub Desktop.
OS X Command Line Password Generator
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
#!/usr/bin/env python | |
import sys | |
import os | |
import string | |
import random | |
DICT_FILE = '/usr/share/dict/words' | |
DEFAULT_LENGTH = 24 | |
# CHARACTER_SET = string.letters + string.letters + string.letters + string.punctuation | |
# CHARACTER_SET = string.letters + string.letters + string.digits | |
CHARACTER_SET = string.lowercase + string.digits | |
def passgen(size=DEFAULT_LENGTH, chars=CHARACTER_SET): | |
return ''.join(random.choice(chars) for _ in range(size)) | |
def shescape(s): | |
return s.replace("'", "'\\''").replace('"', '\"') | |
def passgen_words(length=4): | |
if not os.path.isfile(DICT_FILE): | |
sys.stderr.write("Dict file {} not found".format(DICT_FILE)) | |
sys.exit(1) | |
return ' '.join(random_line(DICT_FILE) for _ in range(length)) | |
def random_line(file_path): | |
fp = open(file_path, 'r') | |
line = next(fp) | |
for num, aline in enumerate(fp): | |
if random.randrange(num + 2): continue | |
line = aline.strip('\n').lower() | |
return line | |
def clipboard_copy(passwd_string): | |
cmd = 'echo \'%s\' | tr -d "\n" | tr -d "\n" | pbcopy' % shescape(passwd_string) | |
os.system(cmd) | |
def run(args): | |
size = 12 | |
if len(sys.argv) > 1: | |
arg = sys.argv[1] | |
if arg == 'w' or arg == 'word' or arg == 'words': | |
generated_pass = passgen_words(4) | |
else: | |
size = int(arg) | |
generated_pass = passgen(size) | |
else: | |
generated_pass = passgen() | |
print generated_pass | |
clipboard_copy(generated_pass) | |
if __name__ == '__main__': | |
sys.exit(run(sys.argv)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment