Skip to content

Instantly share code, notes, and snippets.

@rjzak
Created August 11, 2019 19:27
Show Gist options
  • Save rjzak/3a381b5133832017b7c5d58c8b5a2181 to your computer and use it in GitHub Desktop.
Save rjzak/3a381b5133832017b7c5d58c8b5a2181 to your computer and use it in GitHub Desktop.
Quick script for getting entropy values for files
#!/usr/bin/python
import math, string, sys
def range_bytes (): return range(256)
def range_printable(): return (ord(c) for c in string.printable)
def H(data, iterator=range_bytes):
if not data:
return 0
entropy = 0
for x in iterator():
p_x = float(data.count(chr(x)))/len(data)
if p_x > 0:
entropy += - p_x*math.log(p_x, 2)
return entropy
if __name__ == '__main__':
if len(sys.argv) == 1:
print H(sys.stdin.read())
else:
for arg in sys.argv[1:]:
e = H(open(arg, "rb").read())
print "%s: %1.4f" % (arg, e)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment