Skip to content

Instantly share code, notes, and snippets.

@bbengfort

bbengfort/pwgen.py

Last active Mar 30, 2016
Embed
What would you like to do?
Unique, reproducible per-domain password generation.
#!/usr/bin/env python
## Imports
import base64
import hashlib
import argparse
from getpass import getpass
## Module Constants
VERSION = "pwgen.py 1.0"
DESCRIPTION = "Generates unique, long, reproducible passwords."
EPILOG = "See http://bit.ly/1ROIahb for more information."
def get_password(attempts=0):
password = getpass("Enter master passphrase: ").strip()
confirm = getpass("Enter same passphrase again: ").strip()
if password != confirm:
if attempts < 3:
print "Passwords do not match, try again."
return get_password(attempts+1)
else:
raise Exception("Password attempt maximum, stretch fingers and try again!")
if not password:
raise Exception("You must supply a base password for the generator!")
return password
def main(args):
"""
Use raw input to collect information from the command line.
"""
# Create the base password string
password = "{}{}{}".format(args.domain[0], args.salt, get_password())
# Hash and encode the base password
password = base64.b64encode(hashlib.sha256(password).digest())
return password
if __name__ == "__main__":
parser = argparse.ArgumentParser(
version=VERSION, description=DESCRIPTION, epilog=EPILOG,
)
# Create the arguments
parser.add_argument(
'-s', '--salt', default='', type=str,
help='salt or increment to add to password',
)
parser.add_argument(
'domain', nargs=1,
help='the domain of the site to generate a password for',
)
# Parse the arguments
args = parser.parse_args()
# Execute the script
try:
result = main(args)
if result:
parser.exit(result)
parser.exit()
except Exception as e:
parser.error(str(e))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment