-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpasswdgen.go
122 lines (110 loc) · 3.16 KB
/
passwdgen.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
package main
import (
"fmt"
"log"
"os"
"math/rand"
"github.com/urfave/cli/v2"
"time"
"strings"
)
func contains(slice []string, s string) bool {
for _, value := range slice {
if value == s {
return true
}
}
return false
}
func removeChars(slice []string, removeChars string) []string {
var result []string
for _, str := range slice {
for _, char := range removeChars {
str = strings.ReplaceAll(str, string(char), "")
}
result = append(result, str)
}
return result
}
func generatePassword(length int, opt bool, bl string) string {
r := rand.New(rand.NewSource(time.Now().UnixNano()))
charList := []string{
"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m",
"n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z",
"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M",
"N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z",
"0", "1", "2", "3", "4", "5", "6", "7", "8", "9",
}
optionalChars := []string{
"!", "@", "#", "$", "%", "^", "&", "*", "(", ")", "-", "_", "=", "+",
"[", "]", "{", "}", "|", ";", ":", "'", "\"", ",", ".", "<", ">", "?", "~",
}
for _, char := range bl {
if opt == false && contains(charList, string(char)) {
charList = removeChars(charList, string(char))
} else if opt == true && contains(optionalChars, string(char)) {
optionalChars = removeChars(optionalChars, string(char))
} else {
fmt.Println("\n=+PasswdGen Runtime Error: BOOLEAN ERR...+=\n")
}
}
password := ""
for i := 0; i < length; i++ {
var chosenChars []string
if opt == false {
chosenChars = charList
} else {
chosenChars = append(charList, optionalChars...)
}
randomChar := chosenChars[r.Intn(len(chosenChars))]
password += randomChar
}
return password
}
func main() {
app := &cli.App{
EnableBashCompletion: true,
Commands: []*cli.Command{
{
Name: "genpasswd",
Aliases: []string{"gp"},
Usage: "Generate password(s) a specified amount of times with options | <1-10>",
Flags: []cli.Flag{
&cli.IntFlag{
Name: "length, l",
Value: 12,
Usage: "Length of the password",
},
&cli.IntFlag{
Name: "times, t",
Value: 1,
Usage: "Number of passwords to generate",
},
&cli.BoolFlag{
Name: "optional, opt",
Value: true,
Usage: "Enable/Disable optional characters",
},
&cli.StringFlag{
Name: "blacklist, bl",
Value: "",
Usage: "Characters you want blacklisted | <Takes in a String>",
},
},
Action: func(cCtx *cli.Context) error {
length := cCtx.Int("length")
times := cCtx.Int("times")
opt := cCtx.Bool("optional")
blacklist := strings.TrimSuffix(cCtx.String("blacklist"), "\n")
for i := 0; i < times; i++ {
fmt.Println(generatePassword(length, opt, blacklist))
}
return nil
},
},
},
}
if err := app.Run(os.Args); err != nil {
log.Fatal(err)
}
}