Skip to content

Instantly share code, notes, and snippets.

@SimonTheCoder
Last active October 21, 2021 16:07
Show Gist options
  • Save SimonTheCoder/8aa6e2378ab66935170984638b2665a4 to your computer and use it in GitHub Desktop.
Save SimonTheCoder/8aa6e2378ab66935170984638b2665a4 to your computer and use it in GitHub Desktop.
Calculate the entropy of a bytes array.
import numpy as np
from scipy.stats import entropy
def bytes_entropy(target_bytes,base=None):
v,c = np.unique([int(i) for i in target_bytes],return_counts=True)
if base is None:
base = len(target_bytes)
if base >256:
base = 256
return entropy(c,base=base)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment