@ wrote... (1 year, 10 months ago)

There are dozens of similar programs on the the ol' internetz and here's mine.

I'm not a cryptographer.

#!/usr/bin/python
# -*- coding: utf-8 -*-

import argparse
import string

import random as _random
random = _random.SystemRandom()

def generate_password(nchars, no_visual):
    punctuation = set(string.punctuation) - set("'`\"") # quotes and backtick
    digits = set(string.digits)
    letters = set(string.ascii_letters) # includes lower and upper case

    if no_visual:
        digits -= set("10") # visually similar to lO and very common in passwords

    characters = punctuation | digits | letters
    groups = [punctuation, digits, letters]
    random.shuffle(groups)

    password = []

    # make sure we have at least one char from each group
    # 1 or 2 chars from each group to break pattern
    for group in groups:
        password += random.sample(group, random.randint(1,2))

    # sample is pretty picky about sample size. 0 < n < 94
    while len(password) < nchars:
        password += random.sample(characters, 1)

    return "".join(password)

if __name__ == '__main__':

    parser = argparse.ArgumentParser(description='generate a password')
    parser.add_argument('num_chars', metavar='N', type=int, nargs='?', default=16,
                    help='number of characters in password')
    parser.add_argument('--no-visual', action='store_true', default=False,
                    help="don't use visually similar characters (01)")

    args = parser.parse_args()

    print generate_password( args.num_chars, args.no_visual )
$ mk_passwd
8(*w4+b/zlt3
$ mk_passwd --no-visual 20
85he<<?;Q;IeB_u@4%sY
Category: tech, Tags: python
Comments: 0
Click here to add a comment