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