-
Notifications
You must be signed in to change notification settings - Fork 1
/
printUtils.go
880 lines (648 loc) · 31.6 KB
/
printUtils.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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
package main
import (
"fmt"
"github.com/fatih/color"
"github.com/jedib0t/go-pretty/v6/table"
"github.com/vbauerster/mpb/v7"
"os"
)
// printPasswordTableWindows prints a table of randomized passwords with index numbers to the terminal screen.
//
// The function takes in the number of rows to print, the requested length of each password,
// and an array of passwords to populate. The function prints an index number for each password
// and colors each character of the password string. The table is bordered with a horizontal
// line and cross line characters, and each row is separated with a vertical line. The password
// array is populated with the generated passwords.
//
// Parameters:
// - rows: an int specifying the number of rows to print
// - requestedPasswordLength: an int specifying the length of each password to generate
// - arrayPasswords: a slice of strings representing the passwords to be populated
// Returns: nothing
// printPasswordTableUnix prints a table of randomized passwords with index numbers to the terminal screen.
//
// The function takes in the number of rows to print, the requested length of each password,
// and an array of passwords to populate. The function prints an index number for each password
// and colors each character of the password string. The table is bordered with a horizontal
// line and cross line characters, and each row is separated with a vertical line. The password
// array is populated with the generated passwords.
//
// Parameters:
// - rows: an int specifying the number of rows to print
// - requestedPasswordLength: an int specifying the length of each password to generate
// - arrayPasswords: a slice of strings representing the passwords to be populated
func printPasswordTableUnix(arrayPasswords []string, randomPasswords bool, wordChains bool, memorable2 bool, passPhrases bool, memorable bool, randomHex bool, grammatical bool, grammaticalAI bool, grammaticalAIWithNumbers bool, mnemonic bool, memorable3 bool, memorable4 bool) []string {
if passPhrases {
arrayPasswords = printPassphraseTable(requestedPasswordLength)
} else if wordChains {
arrayPasswords = printWordChainsTable()
} else if memorable2 {
arrayPasswords = printMemorable2Table(memorable2, randomPasswords)
} else if randomPasswords {
arrayPasswords = printRandomPasswordsTable()
} else if memorable {
arrayPasswords = printMemorableTable(1)
} else if randomHex {
arrayPasswords = printRandomHexTable()
} else if grammatical {
arrayPasswords = printGrammaticalTable(false, false)
} else if grammaticalAI {
arrayPasswords = printGrammaticalTable(true, false)
} else if grammaticalAIWithNumbers {
arrayPasswords = printGrammaticalTable(true, true)
} else if mnemonic {
arrayPasswords = printMnemonicTable()
} else if memorable3 {
arrayPasswords = printMemorableTable(3)
} else if memorable4 {
arrayPasswords = printMemorableTable(4)
}
return arrayPasswords
}
// colorizeCharactersUnix This function colorizes a given password string
// according to its characters' ASCII values.
func colorizeCharactersUnix(password string, print bool) string {
var coloredCharsString string
// TODO: Create flag to trim the password down to the requestedPasswordLength
//password = trimPassword(password, requestedPasswordLength)
// Check each character's ascii value and colorize according to category
//for i := 0; i < requestedPasswordLength; i++ {
for i := 0; i < len(password); i++ {
// Convert the character back to ascii value for the color assignment
character := int32(password[i])
if character >= 65 && character <= 90 {
// Assign a color to uppercase characters
coloredCharsString += color.WhiteString(string(character))
} else if character >= 97 && character <= 122 {
// Assign a color to lowercase characters
coloredCharsString += color.HiGreenString(string(character))
} else if character >= 48 && character <= 57 {
// Assign a color to number characters
coloredCharsString += color.CyanString(string(character))
} else if character >= 33 && character <= 47 {
// Assign a color to special characters, first range
coloredCharsString += color.HiBlueString(string(character))
} else if character >= 58 && character <= 64 {
// Assign a color to special characters, second range
coloredCharsString += color.HiBlueString(string(character))
} else if character >= 91 && character <= 96 {
// Assign a color to special characters, third range
coloredCharsString += color.HiBlueString(string(character))
} else if character >= 123 && character <= 126 {
// Assign a color to special characters, fourth range
coloredCharsString += color.HiBlueString(string(character))
} else {
// Assign a color to any character not represented above
coloredCharsString += color.HiYellowString(string(character))
}
}
if print == true {
fmt.Print(coloredCharsString)
}
return coloredCharsString
}
// printPassphraseTable() generates an array of random passphrases and prints them
// in a table format to the console output.
//
// printPassphraseTable generates and prints a table of passphrases with
// colorized characters. The function sets the console height, instantiates a new
// table writer object, creates a new empty array with the same length as the
// original array, and loops through the console screen height and prints a table
// of passphrases. Each row of the table contains an index number and the
// passphrase. The function returns an array of passphrases because it's needed
// for the clipboard functions if the program is in interactive mode. Note: The
// colorization of characters works on all platforms but no color renders on
// Windows.
//
// Returns:
// - An array of strings
func printPassphraseTable(requestedPasswordLength int) []string {
var consoleHeight int
// Set the console height
consoleHeight = funcName(consoleHeight)
// Instantiate a new table writer object
tableWriter := table.NewWriter()
tableWriter.SetOutputMirror(os.Stdout)
// Create a new empty array with the same length as the original array
// This avoids leftover empty array elements causing clipboard copy
// failures later on.
arrayOfPassphrases := make([]string, (consoleHeight/2)-1)
// Loop through the console screen height and print a table of passphrases
for i := 0; i < (consoleHeight/2)-1; i++ {
passphrase := createPassphrase(requestedPasswordLength)
// Colorize the passphrase that we're saving to the array
// The following works on all platforms but no color renders on Windows
passphrase = colorizeCharactersUnix(passphrase, false)
// Append the passphrase to the array
arrayOfPassphrases[i] = passphrase
// Prepare color for the index number
red := color.New(color.FgHiRed).SprintfFunc()
// Print the index number and current element of the array
tableWriter.AppendRow([]interface{}{red("%d", i), passphrase})
tableWriter.AppendSeparator()
}
tableWriter.SetStyle(table.StyleLight)
tableWriter.Render()
// Return the array because it's needed for the
// clipboard functions if we're in interactive mode.
return arrayOfPassphrases
}
// printWordChainsTable() prints a table of word chains to the console screen and returns an array of word chains.
// The console screen height is determined by the funcName function, which is called to set the consoleHeight variable.
// An array of word chains is created with the same length as the original array to avoid leftover empty array elements
// causing clipboard copy failures later on.
//
// The function loops through the console screen height and prints a table of word chains by appending each word chain
// to the arrayOfWordChains array and displaying it with its corresponding index number in a table using the table writer object.
// The function also colorizes the word chain and its index number for better readability and uses the colorizeCharactersUnix function
// to colorize the word chain, which works on all platforms except Windows. The function then sets the table style to Light and
// renders the table.
//
// The function finally returns the arrayOfWordChains array because it's needed for the clipboard functions
// if the program is in interactive mode.
//
// Returns:
// - An array of strings
func printWordChainsTable() []string {
var consoleHeight int
// Set the console height
consoleHeight = funcName(consoleHeight)
// Instantiate a new table writer object
tableWriter := table.NewWriter()
tableWriter.SetOutputMirror(os.Stdout)
// Create a new empty array with the same length as the original array
// This avoids leftover empty array elements causing clipboard copy
// failures later on.
arrayOfWordChains := make([]string, consoleHeight/2)
//fmt.Printf("In printWordChainsTable() before loop.\nrequestedPasswordLength == '%d'\n", requestedPasswordLength)
// Loop through the console screen height and print a table of word chains
for i := 0; i < (consoleHeight/2)-1; i++ {
wordChainNoColor := createWordChain(requestedPasswordLength)
// Colorize the word chain that we're saving to the array
// The following works on all platforms but no color renders on Windows
wordChainColorized := colorizeCharactersUnix(wordChainNoColor, false)
// Append the word chain to the array to be used by the clipboard if in interactive mode
arrayOfWordChains[i] = wordChainNoColor
// Prepare color for the index number
red := color.New(color.FgHiRed).SprintfFunc()
// Print the index number and current element of the array
tableWriter.AppendRow([]interface{}{red("%d", i), wordChainColorized})
tableWriter.AppendSeparator()
}
tableWriter.SetStyle(table.StyleLight)
tableWriter.Render()
// Return the array because it's needed for the
// clipboard functions if we're in interactive mode.
return arrayOfWordChains
}
// printMemorable2Table This function prints a table of memorable and random
// passwords of the specified console height with their indexes.
func printMemorable2Table(memorable2 bool, randomPasswords bool) []string {
var consoleHeight int
// Set the console height
consoleHeight = funcName(consoleHeight)
// Instantiate a new table writer object
tableWriter := table.NewWriter()
tableWriter.SetOutputMirror(os.Stdout)
// Create a new empty array with the same length as the original array
// This avoids leftover empty array elements causing clipboard copy
// failures later on.
arrayOfMemorable2Passwords := make([]string, consoleHeight/2)
// Loop through the console screen height and print a table of memorable2 passwords
for i := 0; i < (consoleHeight/2)-1; i++ {
memorable2PasswordNoColor := createMemorable2Password(memorable2, randomPasswords, consoleHeight)
// Colorize the memorable2 password that we're saving to the array
// The following works on all platforms but no color renders on Windows
memorable2PasswordColorized := colorizeCharactersUnix(memorable2PasswordNoColor, false)
// Append the memorable2 password to the array to be used by the clipboard if in interactive mode
arrayOfMemorable2Passwords[i] = memorable2PasswordNoColor
// Prepare color for the index number
red := color.New(color.FgHiRed).SprintfFunc()
// Print the index number and current element of the array
tableWriter.AppendRow([]interface{}{red("%d", i), memorable2PasswordColorized})
tableWriter.AppendSeparator()
}
tableWriter.SetStyle(table.StyleLight)
tableWriter.Render()
// Return the array because it's needed for the
// clipboard functions if we're in interactive mode.
return arrayOfMemorable2Passwords
}
// printRandomPasswordsTable This function prints a table of random passwords and
// returns an array of them to be used for clipboard functions if needed.
func printRandomPasswordsTable() []string {
var consoleHeight int
// Set the console height
consoleHeight = funcName(consoleHeight)
// Instantiate a new table writer object
tableWriter := table.NewWriter()
tableWriter.SetOutputMirror(os.Stdout)
// Create a new empty array with the same length as the original array
// This avoids leftover empty array elements causing clipboard copy
// failures later on.
arrayOfRandomPasswords := make([]string, consoleHeight/2)
// Loop through the console screen height and print a table of random passwords
for i := 0; i < (consoleHeight/2)-1; i++ {
randomPasswordNoColor := randStringPassword(requestedPasswordLength, false)
// Colorize the random password that we're saving to the array
// The following works on all platforms but no color renders on Windows
randomPasswordColorized := colorizeCharactersUnix(randomPasswordNoColor, false)
// Append the random password to the array to be used by the clipboard if in interactive mode
arrayOfRandomPasswords[i] = randomPasswordNoColor
// Prepare color for the index number
red := color.New(color.FgHiRed).SprintfFunc()
// Print the index number and current element of the array
tableWriter.AppendRow([]interface{}{red("%d", i), randomPasswordColorized})
tableWriter.AppendSeparator()
}
tableWriter.SetStyle(table.StyleLight)
tableWriter.Render()
// Return the array because it's needed for the
// clipboard functions if we're in interactive mode.
return arrayOfRandomPasswords
}
// printMemorableTable This function prints a table of memorable passwords
// of types 3 or 4, which are then stored in an array and returned for further use.
func printMemorableTable(memorableType int) []string {
var consoleHeight int
// Set the console height
consoleHeight = funcName(consoleHeight)
// Instantiate a new table writer object
tableWriter := table.NewWriter()
tableWriter.SetOutputMirror(os.Stdout)
// Create a new empty array with the same length as the original array
// This avoids leftover empty array elements causing clipboard copy
// failures later on.
arrayOfMemorablePasswords := make([]string, consoleHeight/2)
var memorablePasswordNoColor string
// Define the total number of iterations. This will be used by the progress bar
totalIterations := (consoleHeight / 2) - 1
// Create a new progress bar container
progressBarContainer := mpb.New()
// Create a progress bar called progressBar
progressBar := createProgressBar(progressBarContainer, totalIterations)
// Loop through the console screen height and print a table of memorable passwords
for i := 0; i < (consoleHeight/2)-1; i++ {
if memorableType == 1 {
memorablePasswordNoColor = createMemorablePassword(requestedPasswordLength)
} else if memorableType == 3 {
memorablePasswordNoColor = createMemorable3Password()
} else if memorableType == 4 {
memorablePasswordNoColor = createMemorablePasswordAI()
}
// Colorize the word chain that we're saving to the array
// The following works on all platforms but no color renders on Windows
memorablePasswordColorized := colorizeCharactersUnix(memorablePasswordNoColor, false)
// Append the memorable password to the array to be used by the clipboard if in interactive mode
arrayOfMemorablePasswords[i] = memorablePasswordNoColor
// Prepare color for the index number
red := color.New(color.FgHiRed).SprintfFunc()
// Print the index number and current element of the array
tableWriter.AppendRow([]interface{}{red("%d", i), memorablePasswordColorized})
tableWriter.AppendSeparator()
// Increment the progress progressBar
progressBar.Increment()
}
// Wait for the progress progressBar to finish rendering
progressBarContainer.Wait()
tableWriter.SetStyle(table.StyleLight)
tableWriter.Render()
// Return the array because it's needed for the
// clipboard functions if we're in interactive mode.
return arrayOfMemorablePasswords
}
// printRandomHexTable This function provides an efficient way to generate an
// array of random hex passwords and print it as a table.
func printRandomHexTable() []string {
// TODO this function could be combined with printRandomPasswordTable() in some way
var consoleHeight int
// Set the console height
consoleHeight = funcName(consoleHeight)
// Instantiate a new table writer object
tableWriter := table.NewWriter()
tableWriter.SetOutputMirror(os.Stdout)
// Create a new empty array with the same length as the original array
// This avoids leftover empty array elements causing clipboard copy
// failures later on.
arrayOfRandomHex := make([]string, consoleHeight/2)
// Loop through the console screen height and print a table of random hex passwords
for i := 0; i < (consoleHeight/2)-1; i++ {
// TODO: Call a new function for randStringHex() here
randomHexNoColor := randStringPassword(requestedPasswordLength, true)
// Colorize the random hex passwords that we're saving to the array
// The following works on all platforms but no color renders on Windows
randomHexColorized := colorizeCharactersUnix(randomHexNoColor, false)
// Append the random hex password to the array to be used by the clipboard if in interactive mode
arrayOfRandomHex[i] = randomHexNoColor
// Prepare color for the index number
red := color.New(color.FgHiRed).SprintfFunc()
// Print the index number and current element of the array
tableWriter.AppendRow([]interface{}{red("%d", i), randomHexColorized})
tableWriter.AppendSeparator()
}
tableWriter.SetStyle(table.StyleLight)
tableWriter.Render()
// Return the array because it's needed for the
// clipboard functions if we're in interactive mode.
return arrayOfRandomHex
}
// printPasswordExamplesTable This function generates a table of example usages for
// different types of passwords, along with their corresponding command flag.
func printPasswordExamplesTable() []string {
/*---------------------------------------------------------------------------
Set up the progress bar
---------------------------------------------------------------------------*/
// Define the total number of iterations. This will be used by the progress bar
// Here it is hardcoded for the number of password examples printed
totalIterations := 10
// Create a new progress bar container
progressBarContainer := mpb.New()
// Create a progress bar called progressBar
progressBar := createProgressBar(progressBarContainer, totalIterations)
/*---------------------------------------------------------------------------
Set up the table to be printed and data structures to populate it
---------------------------------------------------------------------------*/
// Define a struct to hold a string pair that will populate each row of the table
type PasswordAndCommandFlag struct {
PasswordExample string
CommandFlag string
}
// Create a slice of PasswordAndCommandFlag structs
var arrayOfPasswordTypes []PasswordAndCommandFlag
// Instantiate a new table writer object
tableWriter := table.NewWriter()
tableWriter.SetOutputMirror(os.Stdout)
/*---------------------------------------------------------------------------
Create pairs of each password type and its corresponding command flag.
Increment the progress bar each time
---------------------------------------------------------------------------*/
// --grammatical -----------------------------------------------------------
grammaticalExample := createGrammaticalPassword()
arrayOfPasswordTypes = append(arrayOfPasswordTypes,
PasswordAndCommandFlag{
PasswordExample: grammaticalExample,
CommandFlag: "--grammatical",
})
progressBar.Increment()
// -------------------------------------------------------------------------
// --grammatical-ai --------------------------------------------------------
nonSensicalSentence := createGrammaticalPassword()
grammaticalExampleAI := createGrammaticalPasswordAI(nonSensicalSentence, false)
arrayOfPasswordTypes = append(arrayOfPasswordTypes,
PasswordAndCommandFlag{
PasswordExample: grammaticalExampleAI,
CommandFlag: "--grammatical-ai",
})
progressBar.Increment()
// ------------------------------------------------------------------------
// --grammatical-ai-with-numbers ------------------------------------------
nonSensicalSentence2 := createGrammaticalPassword()
grammaticalExampleAIWithNumbers := createGrammaticalPasswordAI(nonSensicalSentence2, true)
arrayOfPasswordTypes = append(arrayOfPasswordTypes,
PasswordAndCommandFlag{
PasswordExample: grammaticalExampleAIWithNumbers,
CommandFlag: "--grammatical-ai-with-numbers",
})
progressBar.Increment()
// -------------------------------------------------------------------------
// -- hex ------------------------------------------------------------------
randHexPasswordExample := randStringPassword(requestedPasswordLength, true)
arrayOfPasswordTypes = append(arrayOfPasswordTypes,
PasswordAndCommandFlag{
PasswordExample: randHexPasswordExample,
CommandFlag: "--hex",
})
progressBar.Increment()
// --------------------------------------------------------------------------
// --memorable --------------------------------------------------------------
var memorablePassword string
memorableExample := chooseMemorableTransform(memorablePassword, requestedPasswordLength)
arrayOfPasswordTypes = append(arrayOfPasswordTypes,
PasswordAndCommandFlag{
PasswordExample: memorableExample,
CommandFlag: "--memorable",
})
progressBar.Increment()
// --------------------------------------------------------------------------
// --memorable-2 ------------------------------------------------------------
// TODO: This errors out if rows is < 8
memorable2PasswordExample := createMemorable2Password(true, false, 8)
arrayOfPasswordTypes = append(arrayOfPasswordTypes,
PasswordAndCommandFlag{
PasswordExample: memorable2PasswordExample,
CommandFlag: "--memorable-2",
})
progressBar.Increment()
// --------------------------------------------------------------------------
// --memorable-3 ------------------------------------------------------------
memorable3PasswordExample := createMemorable3Password()
arrayOfPasswordTypes = append(arrayOfPasswordTypes,
PasswordAndCommandFlag{
PasswordExample: memorable3PasswordExample,
CommandFlag: "--memorable-3",
})
progressBar.Increment()
// --------------------------------------------------------------------------
// --memorable-4 ------------------------------------------------------------
memorable4PasswordExample := createMemorablePasswordAI()
arrayOfPasswordTypes = append(arrayOfPasswordTypes,
PasswordAndCommandFlag{
PasswordExample: memorable4PasswordExample,
CommandFlag: "--memorable-4",
})
progressBar.Increment()
// --------------------------------------------------------------------------
// --mnemonic ---------------------------------------------------------------
var arrMnemonicPair [2]string
arrMnemonicPair = printMnemonicExampleRow()
// The --mnemonic example has an extra "\n" in the row
mnemonicExample := arrMnemonicPair[0] + "\n" + arrMnemonicPair[1]
arrayOfPasswordTypes = append(arrayOfPasswordTypes,
PasswordAndCommandFlag{
PasswordExample: mnemonicExample,
CommandFlag: "--mnemonic",
})
progressBar.Increment()
// --------------------------------------------------------------------------
// --passphrases ------------------------------------------------------------
// is hardcoded to 5 here to match its default value
passphraseExample := createPassphrase(5)
arrayOfPasswordTypes = append(arrayOfPasswordTypes,
PasswordAndCommandFlag{
PasswordExample: passphraseExample,
CommandFlag: "--passphrases",
})
progressBar.Increment()
// --------------------------------------------------------------------------
// --random -----------------------------------------------------------------
randStringPasswordExample := randStringPassword(requestedPasswordLength, false)
arrayOfPasswordTypes = append(arrayOfPasswordTypes,
PasswordAndCommandFlag{
PasswordExample: randStringPasswordExample,
CommandFlag: "--random",
})
progressBar.Increment()
// --------------------------------------------------------------------------
// --word-chains ------------------------------------------------------------
wordChainPasswordExample := createWordChain(requestedPasswordLength)
arrayOfPasswordTypes = append(arrayOfPasswordTypes,
PasswordAndCommandFlag{
PasswordExample: wordChainPasswordExample,
CommandFlag: "--word-chains",
})
progressBar.Increment()
// --------------------------------------------------------------------------
/*---------------------------------------------------------------------------
Print the table
---------------------------------------------------------------------------*/
// Print the slice of string pairs
for _, pair := range arrayOfPasswordTypes {
exampleColorized := colorizeCharactersUnix(pair.PasswordExample, false)
// Prepare color for the command color
red := color.New(color.FgHiRed).SprintfFunc()
// Print the command option and example password
tableWriter.AppendRow([]interface{}{red("%s", pair.CommandFlag), exampleColorized})
tableWriter.AppendSeparator()
}
// Wait for the progress progressBar to finish rendering
progressBarContainer.Wait()
// Render the table
tableWriter.SetStyle(table.StyleLight)
tableWriter.Render()
//Return the array because it's needed for the
//clipboard functions if we're in interactive mode.
//return arrayOfRandomHex
return nil
}
// printGrammaticalTable generates and prints a table of randomly generated
// sentences which have been colorized and can optionally be improved with AI
// and the AI sentences can optionally contain numbers. Currently, there is no
// option for non-AI sentences containing numbers. The function returns an array
// containing the generated sentences.
func printGrammaticalTable(grammaticalAI bool, grammaticalAIWithNumbers bool) []string {
var consoleHeight int
// Set the console height
consoleHeight = funcName(consoleHeight)
// Instantiate a new table writer object
tableWriter := table.NewWriter()
tableWriter.SetOutputMirror(os.Stdout)
// Create a new empty array with the same length as the original array
// This avoids leftover empty array elements causing clipboard copy
// failures later on.
arrayOfGrammatical := make([]string, consoleHeight/2)
var randomSentenceNoColor string
// Define the total number of iterations. This will be used by the progress bar
totalIterations := (consoleHeight / 2) - 1
// Create a new progress bar container
progressBarContainer := mpb.New()
// Create a progress bar called progressBar
progressBar := createProgressBar(progressBarContainer, totalIterations)
// Loop through the console screen height and print a table of random sentences
for i := 0; i < (consoleHeight/2)-1; i++ {
if grammaticalAI == false {
randomSentenceNoColor = createGrammaticalPassword()
} else {
if grammaticalAIWithNumbers == true {
nonSensicalSentence := createGrammaticalPassword()
// Have AI rewrite the sentence once
nonSensicalSentence = createGrammaticalPasswordAI(nonSensicalSentence, false)
// Then use AI again to improve the sentence we generated but also have it add numbers
randomSentenceNoColor = createGrammaticalPasswordAI(nonSensicalSentence, grammaticalAIWithNumbers)
} else if grammaticalAIWithNumbers == false {
nonSensicalSentence := createGrammaticalPassword()
// Use AI to improve the sentence we generated
randomSentenceNoColor = createGrammaticalPasswordAI(nonSensicalSentence, grammaticalAIWithNumbers)
}
}
// Colorize the random sentences that we're saving to the array
// The following works on all platforms but no color renders on Windows
randomSentenceColorized := colorizeCharactersUnix(randomSentenceNoColor, false)
// Append the random sentence to the array to be used by the clipboard if in interactive mode
arrayOfGrammatical[i] = randomSentenceNoColor
// Prepare color for the index number
red := color.New(color.FgHiRed).SprintfFunc()
// Print the index number and current element of the array
tableWriter.AppendRow([]interface{}{red("%d", i), randomSentenceColorized})
tableWriter.AppendSeparator()
// Increment the progress progressBar
progressBar.Increment()
}
// Wait for the progress progressBar to finish rendering
progressBarContainer.Wait()
tableWriter.SetStyle(table.StyleLight)
tableWriter.Render()
// Return the array because it's needed for the
// clipboard functions if we're in interactive mode.
return arrayOfGrammatical
}
func printMnemonicTable() []string {
var consoleHeight int
// Set the console height
consoleHeight = funcName(consoleHeight)
// Instantiate a new table writer object
tableWriter := table.NewWriter()
tableWriter.SetOutputMirror(os.Stdout)
// Create a new empty array with the same length as the original array
// This avoids leftover empty array elements causing clipboard copy
// failures later on.
arrayOfMnemonics := make([]string, consoleHeight/2)
var randomSentenceNoColor string
var arrayMnemonicAndSentence [2]string
// Define the total number of iterations
totalIterations := (consoleHeight / 2) - 1
// Create a new progress container
progressBarContainer := mpb.New()
// Create a progress bar called progressBar
progressBar := createProgressBar(progressBarContainer, totalIterations)
// Loop through the console screen height and print a table of random sentences
for i := 0; i < (consoleHeight/2)-1; i++ {
nonSensicalSentence := createGrammaticalPassword()
// Have AI rewrite the sentence once
nonSensicalSentence = createGrammaticalPasswordAI(nonSensicalSentence, false)
// Then use AI again to improve the sentence we generated but also have it add numbers
randomSentenceNoColor = createGrammaticalPasswordAI(nonSensicalSentence, true)
// Populate a two-element array containing the mnemonic password and the corresponding sentence. Example:
// "Ivd79tdI?" and "I verbally described 79 treats, didn't I?"
//
//arrayMnemonicAndSentence[0] = createMnemonicPasswordAI(randomSentenceNoColor)
arrayMnemonicAndSentence[0] = createMnemonicFromSentence(randomSentenceNoColor)
arrayMnemonicAndSentence[1] = randomSentenceNoColor
// Colorize the random sentences that we're saving to the array
// The following works on all platforms but no color renders on Windows
randomSentenceColorized := colorizeCharactersUnix(randomSentenceNoColor, false)
mnemonicPasswordColorized := colorizeCharactersUnix(arrayMnemonicAndSentence[0], false)
// TODO: Not sure how to handle the clipboard with this pair of values
// Append the random sentence to the array to be used by the clipboard if in interactive mode
arrayOfMnemonics[i] = randomSentenceNoColor
// Prepare color for the index number
red := color.New(color.FgHiRed).SprintfFunc()
// Print the index number, password, and sentence
tableWriter.AppendRow([]interface{}{red("%d", i), mnemonicPasswordColorized, randomSentenceColorized})
tableWriter.AppendSeparator()
// Increment the progress progressBar
progressBar.Increment()
}
// Wait for the progress progressBar to finish rendering
progressBarContainer.Wait()
tableWriter.SetStyle(table.StyleLight)
tableWriter.Render()
// Return the array because it's needed for the
// clipboard functions if we're in interactive mode.
return arrayOfMnemonics
}
// printMnemonicExampleRow This function creates a randomly generated mnemonic
// password and its corresponding sentence.
func printMnemonicExampleRow() [2]string {
var randomSentenceNoColor string
var arrayMnemonicAndSentence [2]string
nonSensicalSentence := createGrammaticalPassword()
// Have AI rewrite the sentence once
nonSensicalSentence = createGrammaticalPasswordAI(nonSensicalSentence, false)
// Then use AI again to improve the sentence we generated but also have it add numbers
randomSentenceNoColor = createGrammaticalPasswordAI(nonSensicalSentence, true)
// Populate a two-element array containing the mnemonic password and the corresponding sentence. Example:
// "Ivd79tdI?" and "I verbally described 79 treats, didn't I?"
arrayMnemonicAndSentence[0] = createMnemonicFromSentence(randomSentenceNoColor)
arrayMnemonicAndSentence[1] = randomSentenceNoColor
return arrayMnemonicAndSentence
}