Created
August 8, 2017 17:17
-
-
Save cjbarker/c2768548a321a433bb2491ffd2b7cbf1 to your computer and use it in GitHub Desktop.
Python Script that converts passwords in file into hash digest of space/tab delimited STDOUT or GZip compressed file
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 python | |
# -*- coding: utf-8 -*- | |
# ====================================================================================== | |
# title :pswd-conver.py | |
# description :This will create a header for a python script. | |
# author :CJ Barker | |
# date :20170806 | |
# version :0.1 | |
# usage :python pswd-convert.py <password-file> [-c|--compress] [-h|--help] | |
# notes :Password file is flat file that is space/tab delimited of | |
# password [frequency-count]. Outputs to STDOUT or compress file of | |
# tab deleimated results of <password> <count> <sha1> <sha256> <sha512> | |
# All SHAs are hashdigest. Exit code of 0 if successful; | |
# otherwise, non-zero denotes failure | |
# python_version :2.7.10 | |
# ====================================================================================== | |
"""Converts flat file of password and optional frequency counts to SHA1-512 hash digests. | |
Usage: python pswd-convert.py <password-file> [-c|--compress] [-h|--help] | |
Password file is flat file that is space/tab deliminated of password [frequency-count] | |
Outputs to STDOUT tab deleimated results of <password> <count> <sha1> <sha256> <sha512> | |
All SHAs are hashdigest | |
Exit code of 0 if successful; otherwise, non-zero denotes failure | |
""" | |
import argparse | |
import hashlib | |
import gzip | |
import shutil | |
import os | |
import sys | |
# globals | |
FILE_TO_READ = None | |
PSWD_FILENAME = "passwords-db.txt" | |
PSWD_FILE = None | |
class Password: | |
def __init__(self, **kwargs): | |
self.plain_text = kwargs.get('plain_text') | |
self.count = kwargs.get('count', 1) | |
self.md5 = kwargs.get('md5') | |
self.sha1 = kwargs.get('sha1') | |
self.sha256 = kwargs.get('sha256') | |
self.sha512 = kwargs.get('sha512') | |
self.hash() | |
def __iter__(self): | |
yield 'plain_text', self.plain_text | |
yield 'count', self.count | |
yield 'md5', self.md5 | |
yield 'sha1', self.sha1 | |
yield 'sha256', self.sha256 | |
yield 'sha512', self.sha512 | |
def __str__(self): | |
return "{pt}\t{ct}\t{md}\t{s1}\t{s2}".format(pt=self.plain_text, ct=self.count, md=self.md5, s1=self.sha1, s2=self.sha256) | |
def pretty_print(self): | |
return """PlainText\t{pt} | |
Count\t{ct} | |
MD5\t\t{md} | |
sha1\t{s1} | |
sha256\t{s2} | |
sha512\t{s5} | |
""".format(pt=self.plain_text, ct=self.count, md=self.md5, s1=self.sha1, s2=self.sha256, s5=self.sha512) | |
def hash(self, base64=False): | |
"""Will hash all passwords if not already set and load as hexdigest or base64""" | |
if self.md5 is None: | |
self.md5 = hashlib.md5(self.plain_text).hexdigest() | |
if self.sha1 is None: | |
self.sha1 = hashlib.sha1(self.plain_text).hexdigest() | |
if self.sha256 is None: | |
self.sha256 = hashlib.sha256(self.plain_text).hexdigest() | |
if self.sha512 is None: | |
self.sha512 = hashlib.sha512(self.plain_text).hexdigest() | |
def insert_db(obj=None): | |
global PSWD_FILE | |
if obj is None: | |
return False | |
elif isinstance(obj, Password): | |
data = str(obj) + "\n" | |
data = data.encode('utf-8') | |
if PSWD_FILE: | |
PSWD_FILE.write(data) | |
else: | |
sys.stdout.write(data) | |
else: | |
return False | |
def valid_file(filename=None): | |
if filename is None or filename.strip() == "": | |
return False | |
else: | |
return (os.path.isfile(filename) and os.access(filename, os.R_OK)) # verify readable too | |
def main(): | |
parser = argparse.ArgumentParser(description='Password converter') | |
parser.add_argument('password_file', type=str, help='File path of password file to read and convert.') | |
parser.add_argument('-c', '--compress', action='store_true', help='Send output to GZip compressed file instead of STDOUT.') | |
args = parser.parse_args() | |
password_file = args.password_file | |
compress = args.compress | |
if not valid_file(password_file): | |
print "Password file does not exist or is unreadble: " + password_file | |
sys.exit(1) | |
global PSWD_FILE | |
if compress: | |
PSWD_FILE = open(PSWD_FILENAME, "w") | |
print 'Reading passwords from ' + password_file + ' and converting to ' + PSWD_FILENAME | |
try: | |
with open (password_file, "r") as myfile: | |
for line in myfile: | |
words = line.strip().split() | |
if len(words) >= 2: | |
p = Password(plain_text=words[0], count=int(words[1])) | |
else: | |
p = Password(plain_text=words[0]) | |
insert_db(p) | |
finally: | |
if compress: PSWD_FILE.close() | |
if compress: | |
print 'Compressing file...' | |
with open(PSWD_FILENAME, 'rb') as f_in, gzip.open(PSWD_FILENAME + '.gz', 'wb') as f_out: | |
shutil.copyfileobj(f_in, f_out) | |
print 'Results available: ' + PSWD_FILENAME +'.gz' | |
os.remove(PSWD_FILENAME) | |
sys.exit(0) | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment