mirror of
https://github.com/gitleaks/gitleaks.git
synced 2026-11-28 12:14:22 +01:00
26 lines
452 B
Go
26 lines
452 B
Go
package gitleaks
|
|
|
|
import (
|
|
"math"
|
|
)
|
|
|
|
// getShannonEntropy https://en.wiktionary.org/wiki/Shannon_entropy
|
|
func getShannonEntropy(data string) (entropy float64) {
|
|
if data == "" {
|
|
return 0
|
|
}
|
|
|
|
charCounts := make(map[rune]int)
|
|
for _, char := range data {
|
|
charCounts[char]++
|
|
}
|
|
|
|
invLength := 1.0 / float64(len(data))
|
|
for _, count := range charCounts {
|
|
freq := float64(count) * invLength
|
|
entropy -= freq * math.Log2(freq)
|
|
}
|
|
|
|
return entropy
|
|
}
|