Skip to content

Instantly share code, notes, and snippets.

@msuksong
Created April 5, 2020 10:01
Show Gist options
  • Select an option

  • Save msuksong/13d36df958b7cecc61ab441299cf3e4c to your computer and use it in GitHub Desktop.

Select an option

Save msuksong/13d36df958b7cecc61ab441299cf3e4c to your computer and use it in GitHub Desktop.
simple password tools - random password, password hashing with various algorithms, verify password hash
#!/usr/bin/env python
# coding=utf-8
import argparse
import getpass
import os
import random
import string
from passlib.hash import (bcrypt, des_crypt, md5_crypt, sha1_crypt,
sha256_crypt, sha512_crypt)
def random_password():
'''
generate random password which satisfies minimum 2 digits, 2 special chars and 20 chars long.
'''
random.seed = (os.urandom(16))
alphabets = ''.join(random.choice(string.ascii_letters) for _ in range(16))
digits = ''.join(random.choice(string.digits) for _ in range(2))
special_chars = ''.join(random.choice('!@#$%^&*(),.') for _ in range(2))
passwd_char_list = list(alphabets+digits+special_chars)
for _ in range(1024):
random.shuffle(passwd_char_list)
return ''.join(passwd_char_list)
def des_password(password):
return des_crypt.hash(password)
def bcrypt_password(password):
return bcrypt.using(rounds=10).hash(password)
def md5_password(password):
return md5_crypt.hash(password, salt_size=4)
def sha1_password(password):
return sha1_crypt.hash(password, rounds=random.randrange(20000, 30000))
def sha256_password(password):
return sha256_crypt.hash(password, rounds=5000)
def sha512_password(password):
return sha512_crypt.hash(password, rounds=5000)
def encrypt(alg):
crypt_funcs = {
'des': des_password,
'bcrypt': bcrypt_password,
'md5': md5_password,
'sha1': sha1_password,
'sha256': sha256_password,
'sha512': sha512_password
}
password = getpass.getpass("Password:")
encrypted = crypt_funcs[alg](password)
return encrypted
def verify_password(encrypted):
password = getpass.getpass("Password:")
try:
return ('DES', des_crypt.verify(password, encrypted))
except:
pass
try:
return ('BCRYPT', bcrypt.verify(password, encrypted))
except:
pass
try:
return ('MD5', md5_crypt.verify(password, encrypted))
except:
pass
try:
return ('SHA1', sha1_crypt.verify(password, encrypted))
except:
pass
try:
return ('SHA256', sha256_crypt.verify(password, encrypted))
except:
pass
try:
return ('SHA512', sha512_crypt.verify(password, encrypted))
except:
pass
print("Not a valid encrypted hash value or unsupported algorithm.")
return False
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='password utility')
group = parser.add_mutually_exclusive_group()
group.add_argument("-a", "--algorithm", choices=['des', 'bcrypt', 'md5', 'sha1', 'sha256', 'sha512'],
help='encryption algorithm')
group.add_argument("-v", "--verify", nargs=1,
metavar='hash', help='Verify encrypted hash value.')
group.add_argument("-g", "--generate", action='store_true',
help='Generate random password.')
args = parser.parse_args()
if args.algorithm:
print(encrypt(args.algorithm))
elif args.verify:
print(verify_password(args.verify[0]))
elif args.generate:
print(random_password() + '\t(Note: this is not an encrypted hash.)')
else:
parser.print_help()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment