5 from binascii
import hexlify
, unhexlify
8 from rarfile
import Blake2SP
, CRC32Context
, NoHashContext
, Rar3Sha1
12 """Return hex string."""
13 return hexlify(data
).decode("ascii")
17 assert NoHashContext("").hexdigest() is None
18 assert NoHashContext("asd").hexdigest() is None
21 assert md
.digest() is None
25 assert CRC32Context(b
"").hexdigest() == "00000000"
26 assert CRC32Context(b
"Hello").hexdigest() == "f7d18982"
27 assert CRC32Context(b
"Bye").hexdigest() == "4f7ad7d4"
33 assert md
.hexdigest() == "f7d18982"
37 data
= unhexlify(xdata
)
43 def xblake2sp_slow(xdata
):
44 data
= unhexlify(xdata
)
46 buf
= memoryview(data
)
49 md
.update(buf
[pos
: pos
+ 3])
55 assert Blake2SP(b
"").hexdigest() == "dd0e891776933f43c7d032b08a917e25741f8aa9a12c12e1cac8801500f2ca4f"
56 assert Blake2SP(b
"Hello").hexdigest() == "0d6bae0db99f99183d060f7994bb94b45c6490b2a0a628b8b1346ebea8ec1d66"
58 assert xblake2sp("") == "dd0e891776933f43c7d032b08a917e25741f8aa9a12c12e1cac8801500f2ca4f"
59 assert xblake2sp("00") == "a6b9eecc25227ad788c99d3f236debc8da408849e9a5178978727a81457f7239"
61 long1
= "000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f202122232425262728292a2b2c2d2e2f3031"
62 assert xblake2sp(long1
) == "270affa6426f1a515c9b76dfc27d181fc2fd57d082a3ba2c1eef071533a6dfb7"
65 assert xblake2sp(long2
) == "24a78d92592d0761a3681f32935225ca55ffb8eb16b55ab9481c89c59a985ff3"
66 assert xblake2sp_slow(long2
) == "24a78d92592d0761a3681f32935225ca55ffb8eb16b55ab9481c89c59a985ff3"
70 for n
in range(0, 200):
71 data
= bytearray(range(n
))
72 h1
= hashlib
.sha1(data
).hexdigest()
73 h2
= Rar3Sha1(data
).hexdigest()
76 data
= bytearray([(i
& 255) for i
in range(2000)])
79 for step
in (3, 17, 67, 128, 157):
81 while pos
< len(data
):
85 x1
.update(data
[pos
:pos2
])
86 x2
.update(data
[pos
:pos2
])
87 assert x1
.hexdigest() == x2
.hexdigest()
92 exp
= ("a160cb31cb262e9231c0b6fc984fbb0d", "aa54a659fb0c359b30f353a6343fb11d")
93 key
, iv
= rarfile
.rar3_s2k(b
"password", unhexlify("00FF00"))
94 assert (tohex(key
), tohex(iv
)) == exp
95 key
, iv
= rarfile
.rar3_s2k("password", unhexlify("00FF00"))
96 assert (tohex(key
), tohex(iv
)) == exp
98 exp
= ("ffff33ffaf31987c899ccc2f965a8927", "bdff6873721b247afa4f978448a5aeef")
99 key
, iv
= rarfile
.rar3_s2k("p" * 28, unhexlify("1122334455667788"))
100 assert (tohex(key
), tohex(iv
)) == exp
101 exp
= ("306cafde28f1ea78c9427c3ec642c0db", "173ecdf574c0bfe9e7c23bdfd96fa435")
102 key
, iv
= rarfile
.rar3_s2k("p" * 29, unhexlify("1122334455667788"))
103 assert (tohex(key
), tohex(iv
)) == exp