-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutilsOther.go
193 lines (141 loc) · 5.39 KB
/
utilsOther.go
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
package main
import (
"github.com/fatih/color"
"log"
"math"
"os"
"os/exec"
"runtime"
"strconv"
"strings"
)
// consoleSizeUnix returns the height and width of the user's terminal console in number of characters.
// It executes the shell command `stty size` and reads its output to get the console size.
// If any errors occur during this process, it will log an error message to the screen and exit.
//
// Returns:
// - an integer representing the height of the console in number of characters
// - an integer representing the width of the console in number of characters
func consoleSizeUnix() (int, int) {
// Originally from https://gist.github.com/steinelu/aa9a5f402b584bc967eb216e054ceefb
// Execute the shell command `stty size` which returns two integers:
// height and width of the user's heightAndWidthString terminal
sttyCommand := exec.Command("stty", "size")
// Specify the shell's heightAndWidthString STDIN so that executing
// `stty size` will work
sttyCommand.Stdin = os.Stdin
// Execute the `stty size` command and save the output and any resulting error.
heightAndWidthBytes, err := sttyCommand.Output()
// If it errored heightAndWidthBytes, log to the screen and exit
if err != nil {
log.Fatal("Error trying to get the size of the terminal:", err)
}
// Save the height and width values as a string
heightAndWidthString := string(heightAndWidthBytes)
// Remove extra whitespace
heightAndWidthString = strings.TrimSpace(heightAndWidthString)
// Split height and width into an array of two strings
heightAndWidthArray := strings.Split(heightAndWidthString, " ")
// Convert height to an integer
// Atoi is equivalent to ParseInt(s, 10, 0), converted to type int.
height, err := strconv.Atoi(heightAndWidthArray[0])
// If the conversion to int errored out, log to the screen and exit
if err != nil {
log.Fatal("Error trying to convert terminal height to an integer:", err)
}
// Convert width to an integer
width, err := strconv.Atoi(heightAndWidthArray[1])
// If the conversion to int errored out, log to the screen and exit
if err != nil {
log.Fatal("Error trying to convert terminal width to an integer:", err)
}
return height, width
}
// Set the Windows terminal dimensions manually
func consoleSizeWindows() (int, int) {
// Set to Windows default values for now
return 30, 120
}
func funcName(consoleHeight int) int {
if OS == "darwin" || OS == "linux" || OS == "unix" {
consoleHeight, _ = consoleSizeUnix()
} else if OS == "windows" {
consoleHeight, _ = consoleSizeWindows()
}
return consoleHeight
}
func detectOS() string {
//fmt.Printf("Running on %s\n", runtime.GOOS)
//OS = runtime.GOOS
return runtime.GOOS
}
// checkPasswordLength() checks the length of a password and returns a boolean indicating
// whether the password length is valid or not. If the requested password length is less
// than 8 characters, it will print a red-colored error message and return true. If an
// error is passed in as the second argument, it will print a red-colored error message
// indicating that the password length argument is invalid and return true. Otherwise,
// it will return false to indicate that the password length is valid.
//
// Parameters:
// - requestedPasswordLength: the length of the password to be checked
// - err: an error indicating if the password length argument is invalid
//
// Returns:
// - a boolean value indicating if the password length is valid or not
func checkPasswordLength(requestedPasswordLength int, randomHex *bool, passphrases *bool, wordChains *bool) int {
if *randomHex {
if int(requestedPasswordLength) < 4 {
// --hex defaults to 4 characters
color.HiRed("\nHex PIN length defaults to 4 characters if you don't specify a length as the last argument.\n\n")
return 4
}
} else if *passphrases || *wordChains {
if requestedPasswordLength <= 0 {
if *passphrases {
// --passphrases defaults to 5 words
color.HiRed("\nPassphrase length defaults to 5 words if you don't specify a length as the last argument.\n\n")
// Hardcode the default to 5
requestedPasswordLength = 5
} else if *wordChains {
// --word-chains defaults to 3 words
color.HiRed("\nWord chain length defaults to 3 words if you don't specify a length as the last argument.\n\n")
// Hardcode the default to 3
requestedPasswordLength = 3
}
return requestedPasswordLength
} else if *passphrases {
// Use the length the user asked for
return requestedPasswordLength
}
} else if *wordChains {
// Use the length the user asked for
return requestedPasswordLength
} else {
// For other password types, default to 8
if int(requestedPasswordLength) < 8 {
color.HiRed("\nPassword length defaults to 8 characters if you don't specify a length as the last argument.\n\n")
return 8
}
}
//else if int(requestedPasswordLength) > 255 {
// // Use passwordLength as an integer
// requestedPasswordLength = 10
//}
// If we get here just return the length the user asked for
return requestedPasswordLength
}
func isHighEntropy(s string) bool {
entropy := 0.0
counts := make(map[rune]int)
// Count the number of occurrences of each character
for _, r := range s {
counts[r]++
}
// Calculate the entropy of the string
for _, count := range counts {
p := float64(count) / float64(len(s))
entropy -= p * math.Log2(p)
}
// Check if the entropy is high enough
return entropy >= math.Log2(float64(len(s)))-1
}