Skip to content

Commit 94b5f87

Browse files
committed
Merge remote-tracking branch 'origin/main'
2 parents 984924c + fa0e857 commit 94b5f87

File tree

8 files changed

+129
-17
lines changed

8 files changed

+129
-17
lines changed

README.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,9 @@ Waiting for 60 seconds before clearing the clipboard.
6060
Where n is the length of the password.
6161
Length must be the last argument.
6262

63+
-hex
64+
./passwordgen --hex
65+
6366
-interactive
6467
./passwordgen --interactive[=false]
6568
(default true)
@@ -194,6 +197,33 @@ PS C:\Users\somebody\Downloads> .\passwordgen-v1.3.0-windows-amd64.exe -passphra
194197
└────┴───────────────────────────────────────────────┘
195198
```
196199
200+
## Hexadecimal PINs
201+
202+
Hex pins may be 4 or more characters long.
203+
204+
```shell
205+
passwordgen --hex 4
206+
┌───┬──────┐
207+
│ 0 │ FC70 │
208+
├───┼──────┤
209+
│ 1 │ DA10 │
210+
├───┼──────┤
211+
│ 2 │ 2DB6 │
212+
├───┼──────┤
213+
│ 3 │ C314 │
214+
├───┼──────┤
215+
│ 4 │ D186 │
216+
├───┼──────┤
217+
│ 5 │ 5139 │
218+
├───┼──────┤
219+
│ 6 │ D760 │
220+
├───┼──────┤
221+
│ 7 │ 5B32 │
222+
├───┼──────┤
223+
│ 8 │ 48F4 │
224+
└───┴──────┘
225+
```
226+
197227
# Building releases for multiple platforms
198228
199229
```shell

inputHandlers.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ func argsHandler() (
1616
help *bool,
1717
passPhrases *bool,
1818
memorable *bool,
19+
randomHex *bool,
1920
) {
2021
help = flag.Bool(
2122
"help",
@@ -25,7 +26,7 @@ func argsHandler() (
2526

2627
if *help {
2728
flag.Usage()
28-
return nil, nil, nil, nil, nil, nil, nil, nil
29+
return nil, nil, nil, nil, nil, nil, nil, nil, nil
2930
}
3031

3132
// Interactive mode is the default
@@ -64,15 +65,20 @@ func argsHandler() (
6465
false,
6566
"./passwordgen --memorable\n")
6667

68+
randomHex = flag.Bool(
69+
"hex",
70+
false,
71+
"./passwordgen --hex\n")
72+
6773
flag.Parse()
6874

6975
// For now the length is mandatory and must be the last arg
7076
if len(os.Args) < 2 {
7177

7278
color.HiRed("\nPlease provide a password length as the last argument\nOr -h for help.\n")
73-
return nil, nil, nil, nil, nil, nil, nil, nil
79+
return nil, nil, nil, nil, nil, nil, nil, nil, nil
7480
}
75-
return interactive, erase, randomPasswords, wordChains, mixedPasswords, nil, passPhrases, memorable
81+
return interactive, erase, randomPasswords, wordChains, mixedPasswords, nil, passPhrases, memorable, randomHex
7682
}
7783

7884
func ifInteractive(interactive *bool, rows int) bool {

passwordUtils.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,20 @@ import (
99
"time"
1010
)
1111

12-
func randStringPassword(lengthOfRandString int) string {
12+
func randStringPassword(lengthOfRandString int, hexOnly bool) string {
13+
14+
var allowedCharacters []int32
1315

1416
// Set allowed characters
15-
var allowedCharacters = []int32("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#^&*()[]{}%")
17+
if !hexOnly {
18+
19+
allowedCharacters = []int32("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#^&*()[]{}%")
20+
21+
} else {
22+
23+
allowedCharacters = []int32("ABCDEF0123456789")
24+
25+
}
1626

1727
// Make a list of type int32 of the length the user requested their passwords should be
1828
listOfInt32Characters := make([]int32, lengthOfRandString)

passwordUtils_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ func TestRandStringPassword(t *testing.T) {
1515
//rand.Seed(time.Now().UnixNano())
1616

1717
// Call the function to generate a random password string
18-
password := randStringPassword(passwordLength)
18+
password := randStringPassword(passwordLength, false)
1919

2020
// Check if the length of the generated password matches the expected length
2121
assert.Equal(t, passwordLength, len(password))

passwordgen

67.4 KB
Binary file not shown.

passwordgen.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ func main() {
3434

3535
OS = detectOS()
3636

37-
interactive, erase, randomPasswords, wordChains, mixedPasswords, _, passPhrases, memorable := argsHandler()
37+
interactive, erase, randomPasswords, wordChains, mixedPasswords, _, passPhrases, memorable, randomHex := argsHandler()
3838
//if *done {
3939
// return
4040
//}
@@ -47,7 +47,7 @@ func main() {
4747

4848
// Check for password length and return errors if needed
4949
// For now the length is mandatory and must be the last arg
50-
if checkPasswordLength(requestedPasswordLength) {
50+
if checkPasswordLength(requestedPasswordLength, randomHex) {
5151
return
5252
}
5353
}
@@ -87,18 +87,22 @@ func main() {
8787

8888
*randomPasswords = false
8989
}
90+
if *randomHex {
91+
92+
*randomPasswords = false
93+
}
9094

9195
arrayPasswords := make([]string, rows)
9296

9397
if OS == "darwin" || OS == "linux" || OS == "unix" {
9498

9599
// Fill the screen with passwords
96-
arrayPasswords = printPasswordTableUnix(arrayPasswords, *randomPasswords, *wordChains, *mixedPasswords, *passPhrases, *memorable)
100+
arrayPasswords = printPasswordTableUnix(arrayPasswords, *randomPasswords, *wordChains, *mixedPasswords, *passPhrases, *memorable, *randomHex)
97101

98102
} else if OS == "windows" {
99103

100104
// Fill the screen with passwords
101-
arrayPasswords = printPasswordTableUnix(arrayPasswords, *randomPasswords, *wordChains, *mixedPasswords, *passPhrases, *memorable)
105+
arrayPasswords = printPasswordTableUnix(arrayPasswords, *randomPasswords, *wordChains, *mixedPasswords, *passPhrases, *memorable, *randomHex)
102106
//arrayPasswords = printPasswordTableWindows(
103107
// rows,
104108
// requestedPasswordLength,

printUtils.go

Lines changed: 53 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ func printPasswordTableWindows(
175175
// - requestedPasswordLength: an int specifying the length of each password to generate
176176
// - arrayPasswords: a slice of strings representing the passwords to be populated
177177
// Returns: nothing
178-
func printPasswordTableUnix(arrayPasswords []string, randomPasswords bool, wordChains bool, mixedPasswords bool, passPhrases bool, memorable bool) []string {
178+
func printPasswordTableUnix(arrayPasswords []string, randomPasswords bool, wordChains bool, mixedPasswords bool, passPhrases bool, memorable bool, randomHex bool) []string {
179179

180180
if passPhrases {
181181

@@ -196,6 +196,11 @@ func printPasswordTableUnix(arrayPasswords []string, randomPasswords bool, wordC
196196
} else if memorable {
197197

198198
arrayPasswords = printMemorableTable()
199+
200+
} else if randomHex {
201+
202+
arrayPasswords = printRandomHexTable()
203+
199204
}
200205

201206
return arrayPasswords
@@ -447,7 +452,7 @@ func printRandomPasswordsTable() []string {
447452
// Loop through the console screen height and print a table of random passwords
448453
for i := 0; i < (consoleHeight/2)-1; i++ {
449454

450-
randomPasswordNoColor := randStringPassword(requestedPasswordLength)
455+
randomPasswordNoColor := randStringPassword(requestedPasswordLength, false)
451456

452457
// Colorize the random password that we're saving to the array
453458
// The following works on all platforms but no color renders on Windows
@@ -515,3 +520,49 @@ func printMemorableTable() []string {
515520
// clipboard functions if we're in interactive mode.
516521
return arrayOfMemorablePasswords
517522
}
523+
524+
func printRandomHexTable() []string {
525+
// TODO this function could be combined with printRandomPasswordTable() in some way
526+
527+
var consoleHeight int
528+
529+
// Set the console height
530+
consoleHeight = funcName(consoleHeight)
531+
532+
// Instantiate a new table writer object
533+
tableWriter := table.NewWriter()
534+
tableWriter.SetOutputMirror(os.Stdout)
535+
536+
// Create a new empty array with the same length as the original array
537+
// This avoids leftover empty array elements causing clipboard copy
538+
// failures later on.
539+
arrayOfRandomHex := make([]string, consoleHeight/2)
540+
541+
// Loop through the console screen height and print a table of random hex passwords
542+
for i := 0; i < (consoleHeight/2)-1; i++ {
543+
544+
// TODO: Call a new function for randStringHex() here
545+
randomHexNoColor := randStringPassword(requestedPasswordLength, true)
546+
547+
// Colorize the random hex passwords that we're saving to the array
548+
// The following works on all platforms but no color renders on Windows
549+
randomHexColorized := colorizeCharactersUnix(randomHexNoColor, false)
550+
551+
// Append the random hex password to the array to be used by the clipboard if in interactive mode
552+
arrayOfRandomHex[i] = randomHexNoColor
553+
554+
// Prepare color for the index number
555+
red := color.New(color.FgHiRed).SprintfFunc()
556+
557+
// Print the index number and current element of the array
558+
tableWriter.AppendRow([]interface{}{red("%d", i), randomHexColorized})
559+
560+
tableWriter.AppendSeparator()
561+
}
562+
tableWriter.SetStyle(table.StyleLight)
563+
tableWriter.Render()
564+
565+
// Return the array because it's needed for the
566+
// clipboard functions if we're in interactive mode.
567+
return arrayOfRandomHex
568+
}

utilsOther.go

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -111,14 +111,25 @@ func detectOS() string {
111111
//
112112
// Returns:
113113
// - a boolean value indicating if the password length is valid or not
114-
func checkPasswordLength(requestedPasswordLength int) bool {
114+
func checkPasswordLength(requestedPasswordLength int, randomHex *bool) bool {
115115

116-
// Convert requestedPasswordLength to an integer if needed
117-
if int(requestedPasswordLength) < 8 {
116+
if *randomHex {
118117

119-
color.HiRed("\nPassword length must be 8 or longer.\n\n")
120-
return true
118+
if int(requestedPasswordLength) < 4 {
119+
120+
color.HiRed("\nHex PIN length must be 4 or longer.\n\n")
121+
return true
122+
}
123+
124+
} else if !*randomHex {
125+
126+
if int(requestedPasswordLength) < 8 {
127+
128+
color.HiRed("\nPassword length must be 8 or longer.\n\n")
129+
return true
130+
}
121131
}
132+
122133
//else if int(requestedPasswordLength) > 255 {
123134
// // Use passwordLength as an integer
124135
// requestedPasswordLength = 10

0 commit comments

Comments
 (0)