mirror of
https://github.com/gitleaks/gitleaks.git
synced 2026-11-29 04:34:46 +01:00
go mod and even more work
This commit is contained in:
1 parent
1c14586cde
commit
e79ffc6ae8
62 files changed
+1003
-8132
No files matched your search
@@ -0,0 +1,51 @@
|
||||
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
|
||||
}
|
||||
|
||||
func entropyIsHighEnough(entropy float64) bool {
|
||||
if entropy >= opts.Entropy && len(config.Entropy.entropyRanges) == 0 {
|
||||
return true
|
||||
}
|
||||
|
||||
for _, eR := range config.Entropy.entropyRanges {
|
||||
if entropy > eR.v1 && entropy < eR.v2 {
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
func highEntropyLineIsALeak(line string) bool {
|
||||
if !opts.NoiseReduction {
|
||||
return true
|
||||
}
|
||||
|
||||
for _, re := range config.Entropy.regexes {
|
||||
if re.FindString(line) != "" {
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
Reference in new issue
Block a user