Skip to content

Commit 845c411

Browse files
authored
Update and rename main.hcs to entropy.hcs
1 parent e4d6cf8 commit 845c411

2 files changed

Lines changed: 45 additions & 1 deletion

File tree

libs/std/cybersecruity/cmd/main.hcs

Lines changed: 0 additions & 1 deletion
This file was deleted.
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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+
]

0 commit comments

Comments
 (0)