Created
August 11, 2019 19:27
-
-
Save rjzak/3a381b5133832017b7c5d58c8b5a2181 to your computer and use it in GitHub Desktop.
Quick script for getting entropy values for files
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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