blob: 594f5dd8850c35213ffd499681f7a2c5f1ca8447 [file] [log] [blame]
Gregory P. Smithf21a5f72005-08-21 18:45:59 +00001# Test hashlib module
2#
3# $Id$
4#
Gregory P. Smith3f61d612009-05-04 00:45:33 +00005# Copyright (C) 2005-2009 Gregory P. Smith (greg@krypto.org)
Gregory P. Smithf21a5f72005-08-21 18:45:59 +00006# Licensed to PSF under a Contributor Agreement.
7#
8
9import hashlib
Gregory P. Smith3f61d612009-05-04 00:45:33 +000010from io import StringIO
11try:
12 import threading
13except ImportError:
14 threading = None
Gregory P. Smithf21a5f72005-08-21 18:45:59 +000015import unittest
Benjamin Petersonee8712c2008-05-20 21:35:26 +000016from test import support
Benjamin Peterson78cb4912008-09-24 22:53:33 +000017from test.support import _4G, precisionbigmemtest
Gregory P. Smithf21a5f72005-08-21 18:45:59 +000018
19def hexstr(s):
Guido van Rossum5ed033b2007-07-09 14:29:40 +000020 assert isinstance(s, bytes), repr(s)
Guido van Rossum558ca842007-07-10 20:31:05 +000021 h = "0123456789abcdef"
22 r = ''
Guido van Rossum5ed033b2007-07-09 14:29:40 +000023 for i in s:
Guido van Rossum558ca842007-07-10 20:31:05 +000024 r += h[(i >> 4) & 0xF] + h[i & 0xF]
Gregory P. Smithf21a5f72005-08-21 18:45:59 +000025 return r
26
27
28class HashLibTestCase(unittest.TestCase):
29 supported_hash_names = ( 'md5', 'MD5', 'sha1', 'SHA1',
30 'sha224', 'SHA224', 'sha256', 'SHA256',
31 'sha384', 'SHA384', 'sha512', 'SHA512' )
32
33 def test_unknown_hash(self):
34 try:
35 hashlib.new('spam spam spam spam spam')
36 except ValueError:
37 pass
38 else:
39 self.assert_(0 == "hashlib didn't reject bogus hash name")
40
41 def test_hexdigest(self):
42 for name in self.supported_hash_names:
43 h = hashlib.new(name)
Guido van Rossum5ed033b2007-07-09 14:29:40 +000044 assert isinstance(h.digest(), bytes), name
45 self.assertEqual(hexstr(h.digest()), h.hexdigest())
Gregory P. Smithf21a5f72005-08-21 18:45:59 +000046
47
48 def test_large_update(self):
Guido van Rossume22905a2007-08-27 23:09:25 +000049 aas = b'a' * 128
50 bees = b'b' * 127
51 cees = b'c' * 126
Gregory P. Smithf21a5f72005-08-21 18:45:59 +000052
53 for name in self.supported_hash_names:
54 m1 = hashlib.new(name)
55 m1.update(aas)
56 m1.update(bees)
57 m1.update(cees)
58
59 m2 = hashlib.new(name)
60 m2.update(aas + bees + cees)
61 self.assertEqual(m1.digest(), m2.digest())
62
Gregory P. Smithf21a5f72005-08-21 18:45:59 +000063 def check(self, name, data, digest):
64 # test the direct constructors
65 computed = getattr(hashlib, name)(data).hexdigest()
Guido van Rossum558ca842007-07-10 20:31:05 +000066 self.assertEqual(computed, digest)
Gregory P. Smithf21a5f72005-08-21 18:45:59 +000067 # test the general new() interface
68 computed = hashlib.new(name, data).hexdigest()
Guido van Rossum558ca842007-07-10 20:31:05 +000069 self.assertEqual(computed, digest)
Gregory P. Smithf21a5f72005-08-21 18:45:59 +000070
Gregory P. Smith365a1862009-02-12 07:35:29 +000071 def check_no_unicode(self, algorithm_name):
72 # Unicode objects are not allowed as input.
73 self.assertRaises(TypeError, getattr(hashlib, algorithm_name), 'spam')
74 self.assertRaises(TypeError, hashlib.new, algorithm_name, 'spam')
75
76 def test_no_unicode(self):
77 self.check_no_unicode('md5')
78 self.check_no_unicode('sha1')
79 self.check_no_unicode('sha224')
80 self.check_no_unicode('sha256')
81 self.check_no_unicode('sha384')
82 self.check_no_unicode('sha512')
Gregory P. Smithf21a5f72005-08-21 18:45:59 +000083
84 def test_case_md5_0(self):
Guido van Rossum558ca842007-07-10 20:31:05 +000085 self.check('md5', b'', 'd41d8cd98f00b204e9800998ecf8427e')
Gregory P. Smithf21a5f72005-08-21 18:45:59 +000086
87 def test_case_md5_1(self):
Guido van Rossum558ca842007-07-10 20:31:05 +000088 self.check('md5', b'abc', '900150983cd24fb0d6963f7d28e17f72')
Gregory P. Smithf21a5f72005-08-21 18:45:59 +000089
90 def test_case_md5_2(self):
Guido van Rossum5ed033b2007-07-09 14:29:40 +000091 self.check('md5',
92 b'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789',
Guido van Rossum558ca842007-07-10 20:31:05 +000093 'd174ab98d277d9f5a5611c2c9f419d9f')
Gregory P. Smithf21a5f72005-08-21 18:45:59 +000094
Benjamin Peterson78cb4912008-09-24 22:53:33 +000095 @precisionbigmemtest(size=_4G + 5, memuse=1)
96 def test_case_md5_huge(self, size):
97 if size == _4G + 5:
98 try:
99 self.check('md5', 'A'*size, 'c9af2dff37468ce5dfee8f2cfc0a9c6d')
100 except OverflowError:
101 pass # 32-bit arch
102
103 @precisionbigmemtest(size=_4G - 1, memuse=1)
104 def test_case_md5_uintmax(self, size):
105 if size == _4G - 1:
106 try:
107 self.check('md5', 'A'*size, '28138d306ff1b8281f1a9067e1a1a2b3')
108 except OverflowError:
109 pass # 32-bit arch
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000110
111 # use the three examples from Federal Information Processing Standards
112 # Publication 180-1, Secure Hash Standard, 1995 April 17
113 # http://www.itl.nist.gov/div897/pubs/fip180-1.htm
114
115 def test_case_sha1_0(self):
Guido van Rossum5ed033b2007-07-09 14:29:40 +0000116 self.check('sha1', b"",
Guido van Rossum558ca842007-07-10 20:31:05 +0000117 "da39a3ee5e6b4b0d3255bfef95601890afd80709")
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000118
119 def test_case_sha1_1(self):
Guido van Rossum5ed033b2007-07-09 14:29:40 +0000120 self.check('sha1', b"abc",
Guido van Rossum558ca842007-07-10 20:31:05 +0000121 "a9993e364706816aba3e25717850c26c9cd0d89d")
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000122
123 def test_case_sha1_2(self):
Guido van Rossum5ed033b2007-07-09 14:29:40 +0000124 self.check('sha1',
125 b"abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq",
Guido van Rossum558ca842007-07-10 20:31:05 +0000126 "84983e441c3bd26ebaae4aa1f95129e5e54670f1")
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000127
128 def test_case_sha1_3(self):
Guido van Rossum5ed033b2007-07-09 14:29:40 +0000129 self.check('sha1', b"a" * 1000000,
Guido van Rossum558ca842007-07-10 20:31:05 +0000130 "34aa973cd4c4daa4f61eeb2bdbad27316534016f")
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000131
132
133 # use the examples from Federal Information Processing Standards
134 # Publication 180-2, Secure Hash Standard, 2002 August 1
135 # http://csrc.nist.gov/publications/fips/fips180-2/fips180-2.pdf
136
137 def test_case_sha224_0(self):
Guido van Rossume22905a2007-08-27 23:09:25 +0000138 self.check('sha224', b"",
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000139 "d14a028c2a3a2bc9476102bb288234c415a2b01f828ea62ac5b3e42f")
140
141 def test_case_sha224_1(self):
Guido van Rossume22905a2007-08-27 23:09:25 +0000142 self.check('sha224', b"abc",
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000143 "23097d223405d8228642a477bda255b32aadbce4bda0b3f7e36c9da7")
144
145 def test_case_sha224_2(self):
146 self.check('sha224',
Guido van Rossume22905a2007-08-27 23:09:25 +0000147 b"abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq",
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000148 "75388b16512776cc5dba5da1fd890150b0c6455cb4f58b1952522525")
149
150 def test_case_sha224_3(self):
Guido van Rossume22905a2007-08-27 23:09:25 +0000151 self.check('sha224', b"a" * 1000000,
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000152 "20794655980c91d8bbb4c1ea97618a4bf03f42581948b2ee4ee7ad67")
153
154
155 def test_case_sha256_0(self):
Guido van Rossume22905a2007-08-27 23:09:25 +0000156 self.check('sha256', b"",
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000157 "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855")
158
159 def test_case_sha256_1(self):
Guido van Rossume22905a2007-08-27 23:09:25 +0000160 self.check('sha256', b"abc",
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000161 "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad")
162
163 def test_case_sha256_2(self):
164 self.check('sha256',
Guido van Rossume22905a2007-08-27 23:09:25 +0000165 b"abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq",
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000166 "248d6a61d20638b8e5c026930c3e6039a33ce45964ff2167f6ecedd419db06c1")
167
168 def test_case_sha256_3(self):
Guido van Rossume22905a2007-08-27 23:09:25 +0000169 self.check('sha256', b"a" * 1000000,
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000170 "cdc76e5c9914fb9281a1c7e284d73e67f1809a48a497200e046d39ccc7112cd0")
171
172
173 def test_case_sha384_0(self):
Guido van Rossume22905a2007-08-27 23:09:25 +0000174 self.check('sha384', b"",
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000175 "38b060a751ac96384cd9327eb1b1e36a21fdb71114be07434c0cc7bf63f6e1da"+
176 "274edebfe76f65fbd51ad2f14898b95b")
177
178 def test_case_sha384_1(self):
Guido van Rossume22905a2007-08-27 23:09:25 +0000179 self.check('sha384', b"abc",
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000180 "cb00753f45a35e8bb5a03d699ac65007272c32ab0eded1631a8b605a43ff5bed"+
181 "8086072ba1e7cc2358baeca134c825a7")
182
183 def test_case_sha384_2(self):
184 self.check('sha384',
Guido van Rossume22905a2007-08-27 23:09:25 +0000185 b"abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmn"+
186 b"hijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu",
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000187 "09330c33f71147e83d192fc782cd1b4753111b173b3b05d22fa08086e3b0f712"+
188 "fcc7c71a557e2db966c3e9fa91746039")
189
190 def test_case_sha384_3(self):
Guido van Rossume22905a2007-08-27 23:09:25 +0000191 self.check('sha384', b"a" * 1000000,
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000192 "9d0e1809716474cb086e834e310a4a1ced149e9c00f248527972cec5704c2a5b"+
193 "07b8b3dc38ecc4ebae97ddd87f3d8985")
194
195
196 def test_case_sha512_0(self):
Guido van Rossume22905a2007-08-27 23:09:25 +0000197 self.check('sha512', b"",
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000198 "cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce"+
199 "47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e")
200
201 def test_case_sha512_1(self):
Guido van Rossume22905a2007-08-27 23:09:25 +0000202 self.check('sha512', b"abc",
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000203 "ddaf35a193617abacc417349ae20413112e6fa4e89a97ea20a9eeee64b55d39a"+
204 "2192992a274fc1a836ba3c23a3feebbd454d4423643ce80e2a9ac94fa54ca49f")
205
206 def test_case_sha512_2(self):
207 self.check('sha512',
Guido van Rossume22905a2007-08-27 23:09:25 +0000208 b"abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmn"+
209 b"hijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu",
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000210 "8e959b75dae313da8cf4f72814fc143f8f7779c6eb9f7fa17299aeadb6889018"+
211 "501d289e4900f7e4331b99dec4b5433ac7d329eeb6dd26545e96e55b874be909")
212
213 def test_case_sha512_3(self):
Guido van Rossume22905a2007-08-27 23:09:25 +0000214 self.check('sha512', b"a" * 1000000,
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000215 "e718483d0ce769644e2e42c7bc15b4638e1f98b13b2044285632a803afa973eb"+
216 "de0ff244877ea60a4cb0432ce577c31beb009c5c2c49aa2e4eadb217ad8cc09b")
217
Antoine Pitroubcd5cbe2009-01-08 21:17:16 +0000218 def test_gil(self):
219 # Check things work fine with an input larger than the size required
220 # for multithreaded operation (which is hardwired to 2048).
221 gil_minsize = 2048
222
223 m = hashlib.md5()
224 m.update(b'1')
225 m.update(b'#' * gil_minsize)
226 m.update(b'1')
227 self.assertEquals(m.hexdigest(), 'cb1e1a2cbc80be75e19935d621fb9b21')
228
229 m = hashlib.md5(b'x' * gil_minsize)
230 self.assertEquals(m.hexdigest(), 'cfb767f225d58469c5de3632a8803958')
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000231
Gregory P. Smith3f61d612009-05-04 00:45:33 +0000232 def test_threaded_hashing(self):
233 if not threading:
234 raise unittest.SkipTest('No threading module.')
235
236 # Updating the same hash object from several threads at once
237 # using data chunk sizes containing the same byte sequences.
238 #
239 # If the internal locks are working to prevent multiple
240 # updates on the same object from running at once, the resulting
241 # hash will be the same as doing it single threaded upfront.
242 hasher = hashlib.sha1()
243 num_threads = 5
244 smallest_data = b'swineflu'
245 data = smallest_data*200000
246 expected_hash = hashlib.sha1(data*num_threads).hexdigest()
247
248 def hash_in_chunks(chunk_size, event):
249 index = 0
250 while index < len(data):
251 hasher.update(data[index:index+chunk_size])
252 index += chunk_size
253 event.set()
254
255 events = []
256 for threadnum in range(num_threads):
257 chunk_size = len(data) // (10**threadnum)
258 assert chunk_size > 0
259 assert chunk_size % len(smallest_data) == 0
260 event = threading.Event()
261 events.append(event)
262 threading.Thread(target=hash_in_chunks,
263 args=(chunk_size, event)).start()
264
265 for event in events:
266 event.wait()
267
268 self.assertEqual(expected_hash, hasher.hexdigest())
269
270
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000271def test_main():
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000272 support.run_unittest(HashLibTestCase)
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000273
274
275if __name__ == "__main__":
276 test_main()