| 1 | import string
|
|---|
| 2 | import md5
|
|---|
| 3 | from sys import argv
|
|---|
| 4 |
|
|---|
| 5 | def MDPrint(str):
|
|---|
| 6 | outstr = ''
|
|---|
| 7 | for i in str:
|
|---|
| 8 | o = ord(i)
|
|---|
| 9 | outstr = (outstr
|
|---|
| 10 | + string.hexdigits[(o >> 4) & 0xF]
|
|---|
| 11 | + string.hexdigits[o & 0xF])
|
|---|
| 12 | print outstr,
|
|---|
| 13 |
|
|---|
| 14 |
|
|---|
| 15 | from time import time
|
|---|
| 16 |
|
|---|
| 17 | def makestr(start, end):
|
|---|
| 18 | result = ''
|
|---|
| 19 | for i in range(start, end + 1):
|
|---|
| 20 | result = result + chr(i)
|
|---|
| 21 |
|
|---|
| 22 | return result
|
|---|
| 23 |
|
|---|
| 24 |
|
|---|
| 25 | def 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.new()
|
|---|
| 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 |
|
|---|
| 59 | def MDString(str):
|
|---|
| 60 | MDPrint(md5.new(str).digest())
|
|---|
| 61 | print '"' + str + '"'
|
|---|
| 62 |
|
|---|
| 63 |
|
|---|
| 64 | def MDFile(filename):
|
|---|
| 65 | f = open(filename, 'rb')
|
|---|
| 66 | mdContext = md5.new()
|
|---|
| 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 |
|
|---|
| 78 | import sys
|
|---|
| 79 |
|
|---|
| 80 | def MDFilter():
|
|---|
| 81 | mdContext = md5.new()
|
|---|
| 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 |
|
|---|
| 93 | def 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 |
|
|---|
| 109 | # I don't wanna use getopt(), since I want to use the same i/f...
|
|---|
| 110 | def main():
|
|---|
| 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)
|
|---|
| 122 |
|
|---|
| 123 | main()
|
|---|