File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Load diff This file was deleted.
Original file line number Diff line number Diff line change 1+ fun byte_histogram(data: List<Int>) -> List<Int> [
2+ let counts = []
3+ let i = 0
4+ while i < 256 [
5+ counts = counts + [0]
6+ i = i + 1
7+ ]
8+ let j = 0
9+ while j < data.len() [
10+ let b = data[j]
11+ counts[b] = counts[b] + 1
12+ j = j + 1
13+ ]
14+ end counts
15+ ]
16+
17+ !! Entropia Shannona w bitach/bajt (0.0 = calkowicie przewidywalne dane,
18+ !! 8.0 = idealnie losowe bajty - typowe dla szyfrowania/kompresji).
19+ fun shannon_entropy(data: List<Int>) -> Float [
20+ let n = data.len()
21+ manual [
22+ if n == 0 [
23+ log("shannon_entropy: pusty bufor, entropia niezdefiniowana - zwracam 0.0")
24+ ]
25+ ]
26+ let counts = byte_histogram(data)
27+ let total = n as Float
28+ let entropy = 0.0
29+ let k = 0
30+ while k < 256 [
31+ let count = counts[k]
32+ if count > 0 [
33+ let p = (count as Float) / total
34+ entropy = entropy - (p * p.log2())
35+ ]
36+ k = k + 1
37+ ]
38+ end entropy
39+ ]
40+
41+ !! Prosty prog decyzyjny: czy dane "wygladaja" na spakowane/zaszyfrowane
42+ !! (entropia powyzej progu, domyslnie sugerowany 7.2 - patrz wywolujacy).
43+ fun looks_encrypted(data: List<Int>, threshold: Float) -> Bool [
44+ end shannon_entropy(data) >= threshold
45+ ]
You can’t perform that action at this time.
0 commit comments