#!/usr/bin/python # # Stolen from Ero Carrera # http://blog.dkbza.org/2007/05/scanning-data-for-entropy-anomalies.html import math, string, sys, fileinput 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 def main (): for row in fileinput.input(): string = row.rstrip('\n') print ("%s: %f" % (string, H(string, range_printable))) for str in ['gargleblaster', 'tripleee', 'magnus', 'lkjasdlk', 'aaaaaaaa', 'sadfasdfasdf', '7&wS/p(']: print ("%s: %f" % (str, H(str, range_printable)))
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question