From 24956285e3d7676677297adf1896ca55acaa119a Mon Sep 17 00:00:00 2001 From: Marko Kreen Date: Fri, 31 Jul 2020 08:33:31 +0300 Subject: [PATCH] Inline tohex --- dumprar.py | 14 ++++++++++---- rarfile.py | 8 +------- test/test_hashing.py | 9 +++++++-- 3 files changed, 18 insertions(+), 13 deletions(-) diff --git a/dumprar.py b/dumprar.py index 5d60d8a..b0835c0 100755 --- a/dumprar.py +++ b/dumprar.py @@ -2,6 +2,7 @@ """Dump archive contents, test extraction.""" +import binascii import getopt import io import sys @@ -182,6 +183,11 @@ def xprint(m, *args): print(m) +def tohex(data): + """Return hex string.""" + return binascii.hexlify(data).decode("ascii") + + def render_flags(flags, bit_list): """Show bit names. """ @@ -362,7 +368,7 @@ def show_item_v5(h): if h.CRC is not None: xprint(" crc=0x%08x (%d)", h.CRC, h.CRC) if h.blake2sp_hash is not None: - xprint(" blake2sp=%s", rf.tohex(h.blake2sp_hash)) + xprint(" blake2sp=%s", tohex(h.blake2sp_hash)) if h.date_time is not None: xprint(" date_time=%s", fmt_time(h.date_time)) if h.mtime: @@ -378,8 +384,8 @@ def show_item_v5(h): algo_name = "AES256" if enc_algo == rf.RAR5_XENC_CIPHER_AES256 else "UnknownAlgo" xprint(" algo=%d:%s enc_flags=%04x:%s kdf_lg=%d kdf_count=%d salt=%s iv=%s checkval=%s", enc_algo, algo_name, enc_flags, render_flags(enc_flags, r5_file_enc_flags), - kdf_count, 1 << kdf_count, rf.tohex(salt), rf.tohex(iv), - checkval and rf.tohex(checkval) or "-") + kdf_count, 1 << kdf_count, tohex(salt), tohex(iv), + checkval and tohex(checkval) or "-") if h.file_redir: redir_type, redir_flags, redir_name = h.file_redir xprint(" redir: type=%s flags=%d:%s destination=%s", @@ -402,7 +408,7 @@ def show_item_v5(h): xprint(" algo=%d:%s flags=0x%04x:%s", h.encryption_algo, algo_name, h.flags, render_flags(h.encryption_flags, r5_enc_flags)) xprint(" kdf_lg=%d kdf_count=%d", h.encryption_kdf_count, 1 << h.encryption_kdf_count) - xprint(" salt=%s", rf.tohex(h.encryption_salt)) + xprint(" salt=%s", tohex(h.encryption_salt)) else: xprint(" - missing info -") diff --git a/rarfile.py b/rarfile.py index 1334566..d2226b8 100644 --- a/rarfile.py +++ b/rarfile.py @@ -47,7 +47,6 @@ by :meth:`RarFile.open`:: print(ln.strip()) For decompression to work, either ``unrar`` or ``unar`` tool must be in PATH. - """ import errno @@ -91,11 +90,6 @@ class AES_CBC_Decrypt: self.decrypt = ciph.decryptor().update -def tohex(data): - """Return hex string.""" - return hexlify(data).decode("ascii") - - __version__ = "4.0a1" # export only interesting items @@ -2705,7 +2699,7 @@ class Blake2SP: def hexdigest(self): """Hexadecimal digest.""" - return tohex(self.digest()) + return hexlify(self.digest()).decode("ascii") class Rar3Sha1: diff --git a/test/test_hashing.py b/test/test_hashing.py index 84bc648..6843739 100644 --- a/test/test_hashing.py +++ b/test/test_hashing.py @@ -2,10 +2,15 @@ """ import hashlib -from binascii import unhexlify +from binascii import hexlify, unhexlify import rarfile -from rarfile import Blake2SP, CRC32Context, NoHashContext, Rar3Sha1, tohex +from rarfile import Blake2SP, CRC32Context, NoHashContext, Rar3Sha1 + + +def tohex(data): + """Return hex string.""" + return hexlify(data).decode("ascii") def test_nohash(): -- 2.11.4.GIT