entropy.go 926 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. package main
  2. import "math"
  3. // getShannonEntropy https://en.wiktionary.org/wiki/Shannon_entropy
  4. func getShannonEntropy(data string) (entropy float64) {
  5. if data == "" {
  6. return 0
  7. }
  8. charCounts := make(map[rune]int)
  9. for _, char := range data {
  10. charCounts[char]++
  11. }
  12. invLength := 1.0 / float64(len(data))
  13. for _, count := range charCounts {
  14. freq := float64(count) * invLength
  15. entropy -= freq * math.Log2(freq)
  16. }
  17. return entropy
  18. }
  19. func entropyIsHighEnough(entropy float64) bool {
  20. if entropy >= opts.Entropy && len(config.Entropy.entropyRanges) == 0 {
  21. return true
  22. }
  23. for _, eR := range config.Entropy.entropyRanges {
  24. if entropy > eR.v1 && entropy < eR.v2 {
  25. return true
  26. }
  27. }
  28. return false
  29. }
  30. func highEntropyLineIsALeak(line string) bool {
  31. if !opts.NoiseReduction {
  32. return true
  33. }
  34. for _, re := range config.Entropy.regexes {
  35. if re.FindString(line) != "" {
  36. return true
  37. }
  38. }
  39. return false
  40. }