Skip to content

Instantly share code, notes, and snippets.

@ffleming
Last active August 24, 2019 19:44
Show Gist options
  • Save ffleming/0419be34dae22cb37a76a9b098615698 to your computer and use it in GitHub Desktop.
Save ffleming/0419be34dae22cb37a76a9b098615698 to your computer and use it in GitHub Desktop.
Bcrypt CLI
#!/usr/bin/env python3
# The bcrypt import will break if you call this file `bcrypt` or `bcrypt.py`.
import argparse
import bcrypt
import uuid
parser = argparse.ArgumentParser(description='Use bcrypt to hash a password')
parser.add_argument('password', metavar='PASS', type=str, action='store',
default='', nargs='?',
help='The password to hash (default: randomize)')
parser.add_argument('-c', '--cost', type=int, action='store',
help='The cost factor', default=10)
parser.add_argument('-v', '--verbose', action='store_true',
help='Verbose output', default=False)
opts = parser.parse_args()
password = opts.password
verbose = opts.verbose
cost = opts.cost
if password == '':
password = uuid.uuid4().hex
print("Generated password " + password)
bcrypt_hash = bcrypt.hashpw(bytes(password, 'us-ascii'), bcrypt.gensalt(opts.cost))
if verbose:
print("Password hashed with cost " + str(cost))
print(bcrypt_hash.decode('us-ascii'))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment