-
Notifications
You must be signed in to change notification settings - Fork 1
/
passwordgen.go
143 lines (106 loc) · 3.98 KB
/
passwordgen.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
package main
// My first Golang code. It generates a screen full of random char passwords
// of a specified length. ChatGPT wrote the stub for me after this input:
// please write me a command line tool, written in golang, that generates
// passwords of random characters. The command line tool should allow the
// user to request a specific length of the generated passwords. The
// command line tool's output should generate exactly enough passwords to
// fill the screen but not any further. For example, if the command line
// tool is run in a terminal screen that is 30 rows high then the command
// line tool should generate 29 passwords.
// I then grabbed a gist for the column size stuff. The url is inline, below.
// But I had to tweak both the ChatGPT code and the gist to get things working.
import (
"github.com/fatih/color"
_ "github.com/fatih/color"
"os"
"strconv"
)
// Declare global variables
var selectedPasswordNumber int
var requestedPasswordLength int
var OS string
// main() is the entry point of the application. It handles the command-line
// arguments, detects the operating system, gets the console size, generates
// passwords, prints them to the screen, and optionally copies the selected
// password to the clipboard.
func main() {
OS = detectOS()
interactive, erase, randomPasswords, wordChains, memorable2, _, passPhrases, memorable, randomHex, examples, grammatical, grammaticalAI, grammaticalAIWithNumbers, mnemonic, memorable3, memorable4 := argsHandler()
if !*grammatical && !*grammaticalAI && !*grammaticalAIWithNumbers && !*mnemonic && !*memorable3 && !*memorable4 {
// Convert the requested length from string to int
// Length must be the last argument
requestedPasswordLength, _ = strconv.Atoi(os.Args[len(os.Args)-1])
// Check for password length and return defaults if needed
// TODO: add an optional length arg to --passphrases and --word-chains to specify the number of words
requestedPasswordLength = checkPasswordLength(requestedPasswordLength, randomHex, passPhrases, wordChains)
}
// Get the height and width of the console
var rowsColumns [2]int
if OS == "darwin" || OS == "linux" || OS == "unix" {
rowsColumns[0], rowsColumns[1] = consoleSizeUnix()
} else if OS == "windows" {
rowsColumns[0], rowsColumns[1] = consoleSizeWindows()
// Temporarily disable color output on Windows
color.NoColor = true // disables colorized output
}
// We only need the number of rows
var rows int
rows = rowsColumns[0]
if *wordChains {
*randomPasswords = false
}
if *memorable2 {
*randomPasswords = false
}
if *passPhrases {
*randomPasswords = false
}
if *memorable {
*randomPasswords = false
}
if *randomHex {
*randomPasswords = false
}
if *examples {
//for i := 0; i < 10; i++ {
// createMemorable3Password()
//}
*randomPasswords = false
printPasswordExamplesTable()
os.Exit(0)
}
if *grammatical {
*randomPasswords = false
}
if *grammaticalAI {
*randomPasswords = false
}
if *grammaticalAIWithNumbers {
*randomPasswords = false
}
if *mnemonic {
*randomPasswords = false
}
if *memorable3 {
*randomPasswords = false
}
if *memorable4 {
*randomPasswords = false
}
arrayPasswords := make([]string, rows)
if OS == "darwin" || OS == "linux" || OS == "unix" {
// Fill the screen with passwords
arrayPasswords = printPasswordTableUnix(arrayPasswords, *randomPasswords, *wordChains, *memorable2, *passPhrases, *memorable, *randomHex, *grammatical, *grammaticalAI, *grammaticalAIWithNumbers, *mnemonic, *memorable3, *memorable4)
} else if OS == "windows" {
// Fill the screen with passwords
arrayPasswords = printPasswordTableUnix(arrayPasswords, *randomPasswords, *wordChains, *memorable2, *passPhrases, *memorable, *randomHex, *grammatical, *grammaticalAI, *grammaticalAIWithNumbers, *mnemonic, *memorable3, *memorable4)
}
if ifInteractive(interactive, rows) {
// Copy the selected password to the clipboard
if copyToClipboard(erase, arrayPasswords) {
return
}
return
}
}