-
Notifications
You must be signed in to change notification settings - Fork 29
/
entropy.py
executable file
·36 lines (31 loc) · 1023 Bytes
/
entropy.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#!/usr/bin/env python3
import sys
import math
'''
Entropy does not consider the order of bytes.
python3 -c 'with open("seq256.bin", "wb") as f: f.write(bytes(range(256)))'
The file 'seq256.bin' will have a entropy of 8.0 (i.e. randomness == 1.0)
'''
def entropy_file(fp):
freq = [0] * 256
b = fp.read(1)
while b:
freq[b[0]] += 1
b = fp.read(1)
total = sum(freq)
for i in range(256):
if freq[i]:
freqFrac = freq[i] / total
freq[i] = freqFrac * math.log2(freqFrac)
# entropy == How many bits of information I can get from 1 byte of data (on average)
entropy = -sum(freq)
# randomness == How random a bit in data is (on average)
randomness = entropy / 8 # math.log2(256)
return entropy, randomness
def main():
for file_name in sys.argv[1 : ]:
with open(file_name, 'rb') as f:
e, r = entropy_file(f)
print('%s: e = %.4f, r = %.4f' % (file_name, e, r))
if __name__ == '__main__':
main()