1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#!/usr/bin/env python3
 
# SY301 Fall 2016
# Project 2 Shadow file generator program
# (you should not need to modify this program at all!)
 
import hashlib
import string
import sys
import random
 
def usage():
    print('Must run with four arguments, one for the file from which to read')
    print('passwords and counts, one for the number of elements in the table,')
    print('one with the filename for the hashed table, and one for the filename')
    print('for the plaintext solution.')
    print('Ex: python3 shadowgen.py shortrockyouwithcount.txt 5000 outHashes.txt outAnswers.txt')
 
if __name__ == '__main__':
    if len(sys.argv) < 5:
        usage()
        exit(1)
 
    inFile = sys.argv[1]
    try:
        numChoices = int(sys.argv[2])
    except ValueError:
        usage()
        exit(1)
    outFile = sys.argv[3]
    outAns = sys.argv[4]
 
    exclude = set(string.punctuation)
    usernames = set()
 
    with open('american-english.txt', 'r') as uns:
        for word in uns:
            un = ''.join(ch for ch in word.rstrip() if ch not in exclude)
            usernames.add(un.lower())
 
    passwords = []
    with open(inFile, 'r') as pws:
        for line in pws:
            lineArr = line.rstrip().split()
            if len(lineArr) > 1:
                for i in range(int(lineArr[0])):
                    passwords.append(' '.join(lineArr[1:]))
 
    usernames = random.sample(usernames,numChoices)
    with open(outFile, 'w') as hashes, open(outAns, 'w') as answers:
        for un in usernames:
            m = hashlib.md5()
            pw = random.choice(passwords)
            answers.write(un+'\t'+pw+'\n')
            pw = pw.encode()
            m.update(pw)
            hashes.write(un+'\t'+m.hexdigest()+'\n')