bd9383fd82
12 funciones puras con implementación real: HashSHA256, HashMD5, EntropyShannon, IsBase64, IsHex, ExtractURLs, ParseIPCIDR, IPInRange, NormalizeURL, DetectSQLInjection, LevenshteinDistance, JaccardSimilarity 4 funciones impuras con implementación real (stdlib): LookupWhois, ResolveDNS, FetchHTTPHeaders, ScanPortTCP Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
27 lines
506 B
Go
27 lines
506 B
Go
package cybersecurity
|
|
|
|
import "math"
|
|
|
|
// EntropyShannon calcula la entropia de Shannon de los datos proporcionados.
|
|
// Devuelve un valor entre 0 (datos uniformes) y 8 (datos completamente aleatorios para bytes).
|
|
func EntropyShannon(data []byte) float64 {
|
|
if len(data) == 0 {
|
|
return 0
|
|
}
|
|
|
|
var freq [256]float64
|
|
for _, b := range data {
|
|
freq[b]++
|
|
}
|
|
|
|
n := float64(len(data))
|
|
entropy := 0.0
|
|
for _, f := range freq {
|
|
if f > 0 {
|
|
p := f / n
|
|
entropy -= p * math.Log2(p)
|
|
}
|
|
}
|
|
return entropy
|
|
}
|