Created
November 16, 2016 09:51
-
-
Save olavmrk/4b4e471ece65b13acaa373758fe07b59 to your computer and use it in GitHub Desktop.
Generate SHA-512 password hash
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
import argparse | |
import crypt | |
import getpass | |
import random | |
import string | |
import sys | |
def parse_args(): | |
parser = argparse.ArgumentParser(description='Generate SHA-512 crypt() password hash with configurable number of rounds') | |
parser.add_argument('--rounds', type=int, help='Number of rounds', default=10000) | |
return parser.parse_args() | |
def create_salt(length): | |
rng = random.SystemRandom() | |
alphabet = string.ascii_letters + string.digits | |
return ''.join(random.choice(alphabet) for _ in range(0, length)) | |
def hash_password(rounds, password): | |
salt = create_salt(16) | |
password_base = '$6$rounds={rounds}${salt}$'.format(rounds=rounds, salt=salt) | |
return crypt.crypt(password, password_base) | |
def main(): | |
args = parse_args() | |
password = getpass.getpass() | |
verify_password = getpass.getpass(prompt='Verify password') | |
if password != verify_password: | |
print('Passwords do not patch.', file=sys.stderr) | |
sys.exit(1) | |
pwhash = hash_password(rounds=args.rounds, password=password) | |
print(pwhash) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment