72 lines
3.4 KiB
Python
Executable File
72 lines
3.4 KiB
Python
Executable File
#!/usr/bin/env python3
|
|
# passgen - generate passwords (length, symbols, word mode)
|
|
# Usage: passgen [--length N] [--count N] [--no-symbols] [--no-digits] [--words N] [--seed N]
|
|
# Default: 16 chars, letters + digits + symbols, from the secrets module.
|
|
# --words builds a memorable passphrase from a built-in word list.
|
|
import argparse, random, secrets, string, sys
|
|
|
|
WORDS = ("apple banana cherry dragon eagle falcon garden harbor island jungle "
|
|
"kitten lemon mango nickel ocean panda quartz river silver tiger "
|
|
"umbrella violet walnut yellow zebra anchor bridge castle desert "
|
|
"ember forest glacier horizon ivory jade kettle lantern meadow "
|
|
"nebula onyx pebble quill rocket saddle temple urgent velvet "
|
|
"window xenon yarn zephyr copper diamond emerald frost granite").split()
|
|
|
|
def gen_chars(length, symbols=True, digits=True, rng=None):
|
|
pool = string.ascii_letters
|
|
if digits:
|
|
pool += string.digits
|
|
if symbols:
|
|
pool += "!@#$%^&*()-_=+[]{};:,.<>?"
|
|
pick = (rng.choice if rng else secrets.choice)
|
|
while True:
|
|
pw = "".join(pick(pool) for _ in range(length))
|
|
if digits and not any(c.isdigit() for c in pw):
|
|
continue
|
|
if symbols and not any(c in "!@#$%^&*()-_=+[]{};:,.<>?" for c in pw):
|
|
continue
|
|
return pw
|
|
|
|
def gen_words(n, sep="-", rng=None):
|
|
pick = (rng.choice if rng else secrets.choice)
|
|
return sep.join(pick(WORDS) for _ in range(n))
|
|
|
|
def selftest():
|
|
rng = random.Random(42)
|
|
pw = gen_chars(16, rng=rng)
|
|
assert len(pw) == 16 and any(c.isdigit() for c in pw) and any(not c.isalnum() for c in pw)
|
|
pw2 = gen_chars(12, symbols=False, digits=False, rng=rng)
|
|
assert len(pw2) == 12 and pw2.isalpha()
|
|
ph = gen_words(4, rng=rng)
|
|
assert len(ph.split("-")) == 4 and all(w in WORDS for w in ph.split("-"))
|
|
assert gen_chars(16, rng=random.Random(1)) == gen_chars(16, rng=random.Random(1))
|
|
print("passgen selftest: ok (charset rules, word mode, seeded repeatability)")
|
|
return 0
|
|
|
|
def main():
|
|
p = argparse.ArgumentParser(prog="passgen",
|
|
description="Generate passwords (letters/digits/symbols) or memorable word passphrases.")
|
|
p.add_argument("--length", type=int, default=16, help="password length (default 16)")
|
|
p.add_argument("--count", type=int, default=1, help="how many to generate (default 1)")
|
|
p.add_argument("--no-symbols", action="store_true", help="letters and digits only")
|
|
p.add_argument("--no-digits", action="store_true", help="no digits")
|
|
p.add_argument("--words", type=int, metavar="N", help="word mode: passphrase of N words")
|
|
p.add_argument("--sep", default="-", help="word separator in word mode (default -)")
|
|
p.add_argument("--seed", type=int, help="seed for reproducible output (testing only)")
|
|
p.add_argument("--selftest", action="store_true", help="run built-in checks and exit")
|
|
a = p.parse_args()
|
|
if a.selftest:
|
|
return selftest()
|
|
if a.length < 4 or a.count < 1 or (a.words is not None and a.words < 1):
|
|
print("passgen: --length >= 4, --count >= 1, --words >= 1", file=sys.stderr); return 2
|
|
rng = random.Random(a.seed) if a.seed is not None else None
|
|
for _ in range(a.count):
|
|
if a.words:
|
|
print(gen_words(a.words, a.sep, rng))
|
|
else:
|
|
print(gen_chars(a.length, not a.no_symbols, not a.no_digits, rng))
|
|
return 0
|
|
|
|
if __name__ == "__main__":
|
|
sys.exit(main())
|