Skip to content

Commit

Permalink
Small simplifications.
Browse files Browse the repository at this point in the history
Move get_hash_md5, so the hash functions are defined sorted by output size.

Python 3 handles float division by default, so omit an explicit float() conversion.

Remove redundant length checks in parse_imports.
  • Loading branch information
j-t-1 authored Oct 8, 2024
1 parent 4b3b1e2 commit 8e810b0
Showing 1 changed file with 10 additions and 11 deletions.
21 changes: 10 additions & 11 deletions pefile.py
Original file line number Diff line number Diff line change
Expand Up @@ -1300,6 +1300,12 @@ def get_entropy(self):

return self.entropy_H(self.get_data())

def get_hash_md5(self):
"""Get the MD5 hex-digest of the section's data."""

if md5 is not None:
return md5(self.get_data()).hexdigest()

def get_hash_sha1(self):
"""Get the SHA-1 hex-digest of the section's data."""

Expand All @@ -1318,12 +1324,6 @@ def get_hash_sha512(self):
if sha512 is not None:
return sha512(self.get_data()).hexdigest()

def get_hash_md5(self):
"""Get the MD5 hex-digest of the section's data."""

if md5 is not None:
return md5(self.get_data()).hexdigest()

def entropy_H(self, data):
"""Calculate the entropy of a chunk of data."""

Expand All @@ -1334,7 +1334,7 @@ def entropy_H(self, data):

entropy = 0
for x in occurences.values():
p_x = float(x) / len(data)
p_x = x / len(data)
entropy -= p_x * math.log(p_x, 2)

return entropy
Expand Down Expand Up @@ -6002,13 +6002,12 @@ def parse_imports(
# bound.
iat = self.get_import_table(first_thunk, max_length, contains_addresses)

# Would crash if IAT or ILT had None type
if (not iat or len(iat) == 0) and (not ilt or len(ilt) == 0):
if (not iat) and (not ilt):
self.__warnings.append(
"Damaged Import Table information. "
"ILT and/or IAT appear to be broken. "
f"OriginalFirstThunk: 0x{original_first_thunk:x} "
f"FirstThunk: 0x{first_thunk:x}"
f"OriginalFirstThunk: {original_first_thunk:#x} "
f"FirstThunk: {first_thunk:#x}"
)
return []

Expand Down

0 comments on commit 8e810b0

Please sign in to comment.