summaryrefslogtreecommitdiffstats
path: root/Demo/md5test/md5driver.py
blob: 7f561aba407e437cf183f91b8ba5c9880501fb3d (plain)
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
from hashlib import md5
import string
from sys import argv

def MDPrint(str):
    outstr = ''
    for o in str:
        outstr = (outstr
                  + string.hexdigits[(o >> 4) & 0xF]
                  + string.hexdigits[o & 0xF])
    print(outstr, end=' ')


from time import time

def makestr(start, end):
    result = ''
    for i in range(start, end + 1):
        result = result + chr(i)

    return result


def MDTimeTrial():
    TEST_BLOCK_SIZE = 1000
    TEST_BLOCKS = 10000

    TEST_BYTES = TEST_BLOCK_SIZE * TEST_BLOCKS

    # initialize test data, need temporary string filler

    filsiz = 1 << 8
    filler = makestr(0, filsiz-1)
    data = filler * (TEST_BLOCK_SIZE // filsiz)
    data = data + filler[:(TEST_BLOCK_SIZE % filsiz)]

    del filsiz, filler


    # start timer
    print('MD5 time trial. Processing', TEST_BYTES, 'characters...')
    t1 = time()

    mdContext = md5()

    for i in range(TEST_BLOCKS):
        mdContext.update(data)

    str = mdContext.digest()
    t2 = time()

    MDPrint(str)
    print('is digest of test input.')
    print('Seconds to process test input:', t2 - t1)
    print('Characters processed per second:', TEST_BYTES / (t2 - t1))


def MDString(str):
    MDPrint(md5(str.encode("utf-8")).digest())
    print('"' + str + '"')


def MDFile(filename):
    f = open(filename, 'rb')
    mdContext = md5()

    while 1:
        data = f.read(1024)
        if not data:
            break
        mdContext.update(data)

    MDPrint(mdContext.digest())
    print(filename)


import sys

def MDFilter():
    mdContext = md5()

    while 1:
        data = sys.stdin.read(16).encode()
        if not data:
            break
        mdContext.update(data)

    MDPrint(mdContext.digest())
    print()


def MDTestSuite():
    print('MD5 test suite results:')
    MDString('')
    MDString('a')
    MDString('abc')
    MDString('message digest')
    MDString(makestr(ord('a'), ord('z')))
    MDString(makestr(ord('A'), ord('Z'))
              + makestr(ord('a'), ord('z'))
              + makestr(ord('0'), ord('9')))
    MDString((makestr(ord('1'), ord('9')) + '0') * 8)

    # Contents of file foo are "abc"
    MDFile('foo')


# I don't wanna use getopt(), since I want to use the same i/f...
def main():
    if len(argv) == 1:
        MDFilter()
    for arg in argv[1:]:
        if arg[:2] == '-s':
            MDString(arg[2:])
        elif arg == '-t':
            MDTimeTrial()
        elif arg == '-x':
            MDTestSuite()
        else:
            MDFile(arg)

main()