Skip to content

Instantly share code, notes, and snippets.

@bbengfort
Last active March 30, 2016 15:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bbengfort/02fa5be3413d494e282bf3ddb0e3f3a5 to your computer and use it in GitHub Desktop.
Save bbengfort/02fa5be3413d494e282bf3ddb0e3f3a5 to your computer and use it in GitHub Desktop.
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