Skip to content

Instantly share code, notes, and snippets.

@rjzak
Created December 30, 2016 14:51
Show Gist options
  • Select an option

  • Save rjzak/1619a7def053515f847ec98381fadaed to your computer and use it in GitHub Desktop.

Select an option

Save rjzak/1619a7def053515f847ec98381fadaed to your computer and use it in GitHub Desktop.

Revisions

  1. rjzak created this gist Dec 30, 2016.
    34 changes: 34 additions & 0 deletions gen_password.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,34 @@
    #!/usr/bin/python3

    import os
    import sys
    import glob
    import codecs
    import random

    cleanup = lambda x: x.split("/")[0] if "/" in x else x # Some hunspell entries have slashes in the second to last character.

    num = 4
    if len(sys.argv) > 1:
    try:
    num = int(sys.argv[1])
    except:
    pass

    words = list()
    dicts = list()

    if os.path.exists("/usr/share/hunspell"):
    dicts += glob.glob("/usr/share/hunspell/*.dic")

    if os.path.exists("/usr/share/dict"):
    dicts += glob.glob("/usr/share/dict/*-*")

    for dictfile in dicts:
    with open(dictfile, encoding="utf-8", errors="ignore") as f:
    [words.append(cleanup(word.strip())) for word in f]

    words = list(set(words))
    password = ' '.join(random.choice(words) for i in range(num))
    print(password)
    print("Generated from %d dictionary files." % len(dicts))