blob: 326c0cf2a2d4ebd603eae20a7b25b63100d5f640 [file] [log] [blame]
Guido van Rossum82268e01994-10-08 19:17:34 +00001import string
2
3def MDPrint(str):
4 outstr = ''
5 for i in str:
6 o = ord(i)
7 outstr = outstr \
8 + string.hexdigits[(o >> 4) & 0xF] \
9 + string.hexdigits[o & 0xF]
10 print outstr,
11
12
13from time import time
14
15def makestr(start, end):
16 result = ''
17 for i in range(start, end + 1):
18 result = result + chr(i)
19
20 return result
21
22
23from md5 import md5
24
25def MDTimeTrial():
26 TEST_BLOCK_SIZE = 1000
27 TEST_BLOCKS = 10000
28
29 TEST_BYTES = TEST_BLOCK_SIZE * TEST_BLOCKS
30
31 # initialize test data, need temporary string filler
32
33 filsiz = 1 << 8
34 filler = makestr(0, filsiz-1)
35 data = filler * (TEST_BLOCK_SIZE / filsiz);
36 data = data + filler[:(TEST_BLOCK_SIZE % filsiz)]
37
38 del filsiz, filler
39
40
41 # start timer
42 print 'MD5 time trial. Processing', TEST_BYTES, 'characters...'
43 t1 = time()
44
45 mdContext = md5()
46
47 for i in range(TEST_BLOCKS):
48 mdContext.update(data)
49
50 str = mdContext.digest()
51 t2 = time()
52
53 MDPrint(str)
54 print 'is digest of test input.'
55 print 'Seconds to process test input:', t2 - t1
56 print 'Characters processed per second:', TEST_BYTES / (t2 - t1)
57
58
59def MDString(str):
60 MDPrint(md5(str).digest())
61 print '"' + str + '"'
62
63
64def MDFile(filename):
65 f = open(filename, 'rb');
66 mdContext = md5()
67
68 while 1:
69 data = f.read(1024)
70 if not data:
71 break
72 mdContext.update(data)
73
74 MDPrint(mdContext.digest())
75 print filename
76
77
78import sys
79
80def MDFilter():
81 mdContext = md5()
82
83 while 1:
84 data = sys.stdin.read(16)
85 if not data:
86 break
87 mdContext.update(data)
88
89 MDPrint(mdContext.digest())
90 print
91
92
93def MDTestSuite():
94 print 'MD5 test suite results:'
95 MDString('')
96 MDString('a')
97 MDString('abc')
98 MDString('message digest')
99 MDString(makestr(ord('a'), ord('z')))
100 MDString(makestr(ord('A'), ord('Z')) \
101 + makestr(ord('a'), ord('z')) \
102 + makestr(ord('0'), ord('9')))
103 MDString((makestr(ord('1'), ord('9')) + '0') * 8)
104
105 # Contents of file foo are "abc"
106 MDFile('foo')
107
108
109from sys import argv
110
111# I don't wanna use getopt(), since I want to use the same i/f...
112def main():
113 if len(argv) == 1:
114 MDFilter()
115 for arg in argv[1:]:
116 if arg[:2] == '-s':
117 MDString(arg[2:])
118 elif arg == '-t':
119 MDTimeTrial()
120 elif arg == '-x':
121 MDTestSuite()
122 else:
123 MDFile(arg)
124
125main()