2016-11-06 16:04:25 +00:00
|
|
|
from common import *
|
2016-04-12 16:09:23 +00:00
|
|
|
|
2016-04-15 19:13:27 +00:00
|
|
|
from trezor.crypto import hashlib
|
2016-04-12 16:09:23 +00:00
|
|
|
|
|
|
|
class TestCryptoSha256(unittest.TestCase):
|
|
|
|
|
|
|
|
# vectors from http://www.di-mgt.com.au/sha_testvectors.html
|
2016-04-13 18:13:22 +00:00
|
|
|
|
|
|
|
def test_digest(self):
|
2016-04-26 23:08:39 +00:00
|
|
|
self.assertEqual(hashlib.sha256(b'').digest(), unhexlify('e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855'))
|
|
|
|
self.assertEqual(hashlib.sha256(b'abc').digest(), unhexlify('ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad'))
|
|
|
|
self.assertEqual(hashlib.sha256(b'abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq').digest(), unhexlify('248d6a61d20638b8e5c026930c3e6039a33ce45964ff2167f6ecedd419db06c1'))
|
|
|
|
self.assertEqual(hashlib.sha256(b'abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu').digest(), unhexlify('cf5b16a778af8380036ce59e7b0492370b249b11e8f07a51afac45037afee9d1'))
|
2016-04-13 18:13:22 +00:00
|
|
|
|
|
|
|
def test_update(self):
|
2016-04-15 19:13:27 +00:00
|
|
|
x = hashlib.sha256()
|
2016-04-26 23:08:39 +00:00
|
|
|
self.assertEqual(x.digest(), unhexlify('e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855'))
|
2016-04-13 18:13:22 +00:00
|
|
|
|
2016-04-15 19:13:27 +00:00
|
|
|
x = hashlib.sha256()
|
2016-04-13 18:13:22 +00:00
|
|
|
x.update(b'abc')
|
2016-04-26 23:08:39 +00:00
|
|
|
self.assertEqual(x.digest(), unhexlify('ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad'))
|
2016-04-13 18:13:22 +00:00
|
|
|
|
2016-04-15 19:13:27 +00:00
|
|
|
x = hashlib.sha256()
|
2016-04-13 18:13:22 +00:00
|
|
|
x.update(b'abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq')
|
2016-04-26 23:08:39 +00:00
|
|
|
self.assertEqual(x.digest(), unhexlify('248d6a61d20638b8e5c026930c3e6039a33ce45964ff2167f6ecedd419db06c1'))
|
2016-04-13 18:13:22 +00:00
|
|
|
|
2016-04-15 19:13:27 +00:00
|
|
|
x = hashlib.sha256()
|
2016-04-13 18:13:22 +00:00
|
|
|
x.update(b'abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu')
|
2016-04-26 23:08:39 +00:00
|
|
|
self.assertEqual(x.digest(), unhexlify('cf5b16a778af8380036ce59e7b0492370b249b11e8f07a51afac45037afee9d1'))
|
2016-04-13 18:13:22 +00:00
|
|
|
|
2016-04-15 19:13:27 +00:00
|
|
|
x = hashlib.sha256()
|
2016-04-13 18:13:22 +00:00
|
|
|
for i in range(1000000):
|
|
|
|
x.update(b'a')
|
2016-04-26 23:08:39 +00:00
|
|
|
self.assertEqual(x.digest(), unhexlify('cdc76e5c9914fb9281a1c7e284d73e67f1809a48a497200e046d39ccc7112cd0'))
|
2016-04-13 18:13:22 +00:00
|
|
|
|
|
|
|
'''
|
2016-04-15 19:13:27 +00:00
|
|
|
x = hashlib.sha256()
|
2016-04-13 18:13:22 +00:00
|
|
|
for i in range(16777216):
|
|
|
|
x.update(b'abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmno')
|
2016-04-26 23:08:39 +00:00
|
|
|
self.assertEqual(x.digest(), unhexlify('50e72a0e26442fe2552dc3938ac58658228c0cbfb1d2ca872ae435266fcd055e'))
|
2016-04-13 18:13:22 +00:00
|
|
|
'''
|
2016-04-12 16:09:23 +00:00
|
|
|
|
2016-04-14 09:17:18 +00:00
|
|
|
def test_digest_multi(self):
|
2016-04-15 19:13:27 +00:00
|
|
|
x = hashlib.sha256()
|
2016-04-14 09:17:18 +00:00
|
|
|
d0 = x.digest()
|
|
|
|
d1 = x.digest()
|
|
|
|
d2 = x.digest()
|
|
|
|
self.assertEqual(d0, d1)
|
|
|
|
self.assertEqual(d0, d2)
|
|
|
|
|
2016-04-12 16:09:23 +00:00
|
|
|
if __name__ == '__main__':
|
|
|
|
unittest.main()
|