Hack memoryview for 2.6
[rarfile.git] / test / test_hashing.py
blob614e2096b4d1b82e1bb622139a54d2fe4c76a5c6
1 """Hashing tests.
2 """
4 from __future__ import division, print_function
6 import hashlib
8 from binascii import unhexlify
10 from nose.tools import *
12 import rarfile
14 from rarfile import Blake2SP, CRC32Context, NoHashContext, tohex, Rar3Sha1
16 def test_nohash():
17 eq_(NoHashContext('').hexdigest(), None)
18 eq_(NoHashContext('asd').hexdigest(), None)
19 md = NoHashContext()
20 md.update('asd')
21 eq_(md.digest(), None)
23 def test_crc32():
24 eq_(CRC32Context(b'').hexdigest(), '00000000')
25 eq_(CRC32Context(b'Hello').hexdigest(), 'f7d18982')
26 eq_(CRC32Context(b'Bye').hexdigest(), '4f7ad7d4')
28 md = CRC32Context()
29 md.update(b'He')
30 md.update(b'll')
31 md.update(b'o')
32 eq_(md.hexdigest(), 'f7d18982')
34 def xblake2sp(xdata):
35 data = unhexlify(xdata)
36 md = Blake2SP()
37 md.update(data)
38 return md.hexdigest()
40 def xblake2sp_slow(xdata):
41 data = unhexlify(xdata)
42 md = Blake2SP()
43 buf = memoryview(data)
44 pos = 0
45 while pos < len(buf):
46 md.update(buf[pos : pos+3])
47 pos += 3
48 return md.hexdigest()
51 if rarfile._have_blake2:
52 def test_blake2sp():
53 eq_(Blake2SP(b'').hexdigest(), 'dd0e891776933f43c7d032b08a917e25741f8aa9a12c12e1cac8801500f2ca4f')
54 eq_(Blake2SP(b'Hello').hexdigest(), '0d6bae0db99f99183d060f7994bb94b45c6490b2a0a628b8b1346ebea8ec1d66')
56 eq_(xblake2sp(''), 'dd0e891776933f43c7d032b08a917e25741f8aa9a12c12e1cac8801500f2ca4f')
57 eq_(xblake2sp('00'), 'a6b9eecc25227ad788c99d3f236debc8da408849e9a5178978727a81457f7239')
59 long1 = '000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f202122232425262728292a2b2c2d2e2f3031'
60 eq_(xblake2sp(long1), '270affa6426f1a515c9b76dfc27d181fc2fd57d082a3ba2c1eef071533a6dfb7')
62 long2 = long1 * 20
63 eq_(xblake2sp(long2), '24a78d92592d0761a3681f32935225ca55ffb8eb16b55ab9481c89c59a985ff3')
64 eq_(xblake2sp_slow(long2), '24a78d92592d0761a3681f32935225ca55ffb8eb16b55ab9481c89c59a985ff3')
66 def test_hmac_sha256():
67 eq_(tohex(rarfile.hmac_sha256(b'key', b'data')), '5031fe3d989c6d1537a013fa6e739da23463fdaec3b70137d828e36ace221bd0')
69 def test_rar3_sha1():
70 for n in range(0, 200):
71 data = bytearray([i for i in range(n)])
72 h1 = hashlib.sha1(data).hexdigest()
73 h2 = Rar3Sha1(data).hexdigest()
74 eq_(h1, h2)
76 data = bytearray([(i & 255) for i in range(2000)])
77 x1 = hashlib.sha1()
78 x2 = Rar3Sha1()
79 for step in (3, 17, 67, 128, 157):
80 pos = 0
81 while pos < len(data):
82 pos2 = pos + step
83 if pos2 > len(data):
84 pos2 = len(data)
85 x1.update(data[pos:pos2])
86 x2.update(data[pos:pos2])
87 eq_(x1.hexdigest(), x2.hexdigest())
88 pos = pos2
90 def test_rar3_s2k():
91 exp = ('a160cb31cb262e9231c0b6fc984fbb0d', 'aa54a659fb0c359b30f353a6343fb11d')
92 key, iv = rarfile.rar3_s2k(b'password', unhexlify('00FF00'))
93 eq_((tohex(key), tohex(iv)), exp)
94 key, iv = rarfile.rar3_s2k(u'password', unhexlify('00FF00'))
95 eq_((tohex(key), tohex(iv)), exp)
97 exp = ('ffff33ffaf31987c899ccc2f965a8927', 'bdff6873721b247afa4f978448a5aeef')
98 key, iv = rarfile.rar3_s2k(u'p'*28, unhexlify('1122334455667788'))
99 eq_((tohex(key), tohex(iv)), exp)
100 exp = ('306cafde28f1ea78c9427c3ec642c0db', '173ecdf574c0bfe9e7c23bdfd96fa435')
101 key, iv = rarfile.rar3_s2k(u'p'*29, unhexlify('1122334455667788'))
102 eq_((tohex(key), tohex(iv)), exp)
104 if rarfile._have_crypto:
105 def test_pbkdf2_hmac_sha256():
106 eq_(tohex(rarfile.pbkdf2_sha256(b'password', b'salt', 100)),
107 '07e6997180cf7f12904f04100d405d34888fdf62af6d506a0ecc23b196fe99d8')