-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
147 lines (127 loc) · 3.46 KB
/
Copy pathmain.go
File metadata and controls
147 lines (127 loc) · 3.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
package main
import (
"bufio"
"fmt"
"log"
"math"
"os"
"sort"
"strings"
"github.com/alecthomas/kingpin/v2"
)
var (
wordlistPath = kingpin.Arg("wordlist", "Path to wordlist file").Required().String()
top = kingpin.Flag("top", "Number of top words to display").Default("10").Int()
outputFile = kingpin.Flag("output", "Output file").String()
)
func main() {
kingpin.Parse()
// Load wordlist
wordlist := ReadLines(*wordlistPath)
if *top > len(wordlist) {
*top = len(wordlist)
}
// Calculate entropy for each word in the wordlist and store it in a map.
wordEntropy := make(map[string]float64)
for _, word := range wordlist {
wordEntropy[word] = ShannonEntropy(word)
}
// Sort the wordlist by entropy.
sortedWordlist := SortByValue(wordEntropy)
// Reverse the sorted wordlist to get the highest entropy words first.
for i, j := 0, len(sortedWordlist)-1; i < j; i, j = i+1, j-1 {
sortedWordlist[i], sortedWordlist[j] = sortedWordlist[j], sortedWordlist[i]
}
// Print the top words with their entropy.
for _, word := range sortedWordlist[:*top] {
fmt.Printf("%s\t%.2f\n", word, wordEntropy[word])
}
// Write the output to a file if specified.
if *outputFile != "" {
file, err := os.Create(*outputFile)
if err != nil {
log.Fatal(err)
}
defer file.Close()
for _, word := range sortedWordlist {
fmt.Fprintf(file, "%s\n", word)
}
}
}
// SortByValue sorts a map by its values.
func SortByValue(m map[string]float64) []string {
var keys []string
for key := range m {
keys = append(keys, key)
}
// Sort keys by value
sort.Slice(keys, func(i, j int) bool {
return m[keys[i]] < m[keys[j]]
})
return keys
}
// ShannonEntropy calculates the entropy of a string using the Shannon entropy formula.
//
// The Shannon entropy formula is defined as:
//
// H(X) = - Σ (P(xi) * log2(P(xi)))
//
// Where:
// - H(X) is the entropy of the string.
// - Σ is the sum over all characters in the string.
// - xi is a character in the string at i'th position.
// - p(x) is the probability of the character x.
func ShannonEntropy(s string) float64 {
entropy := 0.0
probabilities := Probabilities(s)
for _, p := range probabilities {
entropy -= p * math.Log2(p)
}
return entropy
}
// Probabilities calculates the probability of each character in a string.
func Probabilities(s string) map[string]float64 {
probabilities := make(map[string]float64)
for _, char := range s {
probabilities[string(char)]++
}
for key, value := range probabilities {
probabilities[key] = value / float64(len(s))
}
return probabilities
}
// ReadLines reads a file and returns a slice of strings representing each line.
func ReadLines(filePath string) []string {
var lines []string
file, err := os.Open(filePath)
if err != nil {
log.Fatal(err)
}
scanner := bufio.NewScanner(file)
scanner.Split(bufio.ScanLines)
for scanner.Scan() {
lines = append(lines, strings.TrimSpace(scanner.Text()))
}
file.Close()
return lines
}
func TestShannonEntropy() {
// Test cases for ShannonEntropy function.
testCases := []struct {
input string
expected float64
}{
{"hello", 2.584962500721156},
{"world", 2.584962500721156},
{"hello world", 3.169925001442312},
{"entropy", 2.807354922057604},
{"shannon", 2.807354922057604},
{"shannon entropy", 3.169925001442312},
}
for _, tc := range testCases {
actual := ShannonEntropy(tc.input)
if actual != tc.expected {
fmt.Printf("FAIL: ShannonEntropy(%q) = %f; expected %f\n", tc.input, actual, tc.expected)
}
}
}