1994-10-08 16:17:34 -03:00
|
|
|
import string
|
2003-04-24 14:26:56 -03:00
|
|
|
import md5
|
|
|
|
from sys import argv
|
1994-10-08 16:17:34 -03:00
|
|
|
|
|
|
|
def MDPrint(str):
|
2003-04-24 14:26:22 -03:00
|
|
|
outstr = ''
|
|
|
|
for i in str:
|
|
|
|
o = ord(i)
|
2004-07-18 02:56:09 -03:00
|
|
|
outstr = (outstr
|
|
|
|
+ string.hexdigits[(o >> 4) & 0xF]
|
2003-04-24 14:27:53 -03:00
|
|
|
+ string.hexdigits[o & 0xF])
|
2003-04-24 14:26:22 -03:00
|
|
|
print outstr,
|
|
|
|
|
1994-10-08 16:17:34 -03:00
|
|
|
|
|
|
|
from time import time
|
|
|
|
|
|
|
|
def makestr(start, end):
|
2003-04-24 14:26:22 -03:00
|
|
|
result = ''
|
|
|
|
for i in range(start, end + 1):
|
|
|
|
result = result + chr(i)
|
|
|
|
|
|
|
|
return result
|
1994-10-08 16:17:34 -03:00
|
|
|
|
|
|
|
|
|
|
|
def MDTimeTrial():
|
2003-04-24 14:26:22 -03:00
|
|
|
TEST_BLOCK_SIZE = 1000
|
|
|
|
TEST_BLOCKS = 10000
|
|
|
|
|
|
|
|
TEST_BYTES = TEST_BLOCK_SIZE * TEST_BLOCKS
|
1994-10-08 16:17:34 -03:00
|
|
|
|
2003-04-24 14:26:22 -03:00
|
|
|
# initialize test data, need temporary string filler
|
1994-10-08 16:17:34 -03:00
|
|
|
|
2003-04-24 14:26:22 -03:00
|
|
|
filsiz = 1 << 8
|
|
|
|
filler = makestr(0, filsiz-1)
|
2008-09-12 22:42:55 -03:00
|
|
|
data = filler * (TEST_BLOCK_SIZE // filsiz)
|
2003-04-24 14:26:22 -03:00
|
|
|
data = data + filler[:(TEST_BLOCK_SIZE % filsiz)]
|
1994-10-08 16:17:34 -03:00
|
|
|
|
2003-04-24 14:26:22 -03:00
|
|
|
del filsiz, filler
|
1994-10-08 16:17:34 -03:00
|
|
|
|
|
|
|
|
2003-04-24 14:26:22 -03:00
|
|
|
# start timer
|
|
|
|
print 'MD5 time trial. Processing', TEST_BYTES, 'characters...'
|
|
|
|
t1 = time()
|
1994-10-08 16:17:34 -03:00
|
|
|
|
2003-04-24 14:26:56 -03:00
|
|
|
mdContext = md5.new()
|
1994-10-08 16:17:34 -03:00
|
|
|
|
2003-04-24 14:26:22 -03:00
|
|
|
for i in range(TEST_BLOCKS):
|
|
|
|
mdContext.update(data)
|
1994-10-08 16:17:34 -03:00
|
|
|
|
2003-04-24 14:26:22 -03:00
|
|
|
str = mdContext.digest()
|
|
|
|
t2 = time()
|
1994-10-08 16:17:34 -03:00
|
|
|
|
2003-04-24 14:26:22 -03:00
|
|
|
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)
|
1994-10-08 16:17:34 -03:00
|
|
|
|
|
|
|
|
|
|
|
def MDString(str):
|
2003-04-24 14:26:56 -03:00
|
|
|
MDPrint(md5.new(str).digest())
|
2003-04-24 14:26:22 -03:00
|
|
|
print '"' + str + '"'
|
1994-10-08 16:17:34 -03:00
|
|
|
|
|
|
|
|
|
|
|
def MDFile(filename):
|
2008-09-12 22:42:55 -03:00
|
|
|
f = open(filename, 'rb')
|
2003-04-24 14:26:56 -03:00
|
|
|
mdContext = md5.new()
|
1994-10-08 16:17:34 -03:00
|
|
|
|
2003-04-24 14:26:22 -03:00
|
|
|
while 1:
|
|
|
|
data = f.read(1024)
|
|
|
|
if not data:
|
|
|
|
break
|
|
|
|
mdContext.update(data)
|
1994-10-08 16:17:34 -03:00
|
|
|
|
2003-04-24 14:26:22 -03:00
|
|
|
MDPrint(mdContext.digest())
|
|
|
|
print filename
|
1994-10-08 16:17:34 -03:00
|
|
|
|
|
|
|
|
|
|
|
import sys
|
|
|
|
|
|
|
|
def MDFilter():
|
2003-04-24 14:26:56 -03:00
|
|
|
mdContext = md5.new()
|
1994-10-08 16:17:34 -03:00
|
|
|
|
2003-04-24 14:26:22 -03:00
|
|
|
while 1:
|
|
|
|
data = sys.stdin.read(16)
|
|
|
|
if not data:
|
|
|
|
break
|
|
|
|
mdContext.update(data)
|
1994-10-08 16:17:34 -03:00
|
|
|
|
2003-04-24 14:26:22 -03:00
|
|
|
MDPrint(mdContext.digest())
|
|
|
|
print
|
1994-10-08 16:17:34 -03:00
|
|
|
|
|
|
|
|
|
|
|
def MDTestSuite():
|
2003-04-24 14:26:22 -03:00
|
|
|
print 'MD5 test suite results:'
|
|
|
|
MDString('')
|
|
|
|
MDString('a')
|
|
|
|
MDString('abc')
|
|
|
|
MDString('message digest')
|
|
|
|
MDString(makestr(ord('a'), ord('z')))
|
2004-07-18 02:56:09 -03:00
|
|
|
MDString(makestr(ord('A'), ord('Z'))
|
|
|
|
+ makestr(ord('a'), ord('z'))
|
2003-04-24 14:26:22 -03:00
|
|
|
+ makestr(ord('0'), ord('9')))
|
|
|
|
MDString((makestr(ord('1'), ord('9')) + '0') * 8)
|
1994-10-08 16:17:34 -03:00
|
|
|
|
2003-04-24 14:26:22 -03:00
|
|
|
# Contents of file foo are "abc"
|
|
|
|
MDFile('foo')
|
1994-10-08 16:17:34 -03:00
|
|
|
|
|
|
|
|
|
|
|
# I don't wanna use getopt(), since I want to use the same i/f...
|
|
|
|
def main():
|
2003-04-24 14:26:22 -03:00
|
|
|
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)
|
1994-10-08 16:17:34 -03:00
|
|
|
|
|
|
|
main()
|