mirror of
https://github.com/python/cpython.git
synced 2024-12-25 01:34:48 +08:00
3fdcccb82f
had yet-another Python implementation of a binary-data-to-hex-digit encoder!
31 lines
740 B
Python
31 lines
740 B
Python
# Testing md5 module
|
|
|
|
import string
|
|
from md5 import md5
|
|
|
|
def hexstr(s):
|
|
h = string.hexdigits
|
|
r = ''
|
|
for c in s:
|
|
i = ord(c)
|
|
r = r + h[(i >> 4) & 0xF] + h[i & 0xF]
|
|
return r
|
|
|
|
def md5test(s):
|
|
return 'MD5 ("' + s + '") = ' + hexstr(md5(s).digest())
|
|
|
|
print 'MD5 test suite:'
|
|
print md5test('')
|
|
print md5test('a')
|
|
print md5test('abc')
|
|
print md5test('message digest')
|
|
print md5test('abcdefghijklmnopqrstuvwxyz')
|
|
print md5test('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789')
|
|
print md5test('12345678901234567890123456789012345678901234567890123456789012345678901234567890')
|
|
|
|
# hexdigest is new with Python 2.0
|
|
m = md5('testing the hexdigest method')
|
|
h = m.hexdigest()
|
|
if hexstr(m.digest()) <> h:
|
|
print 'hexdigest() failed'
|