-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrandompassword.go
94 lines (80 loc) · 2.21 KB
/
randompassword.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
package main
import (
"crypto/rand"
"fmt"
"log"
"math/big"
"os"
"github.com/urfave/cli/v2"
)
// Ambiguous characters have been removed
const alpha string = "ABCDEFGHJKMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"
const numbers string = "23456789"
const special string = "!@#$%^&*()[]{}_+.,:"
func main() {
cli.AppHelpTemplate = `NAME:
{{template "helpNameTemplate" .}}
USAGE:
{{if .UsageText}}{{wrap .UsageText 3}}{{else}}{{.HelpName}} {{if .VisibleFlags}}[options]{{end}}{{if .ArgsUsage}} {{.ArgsUsage}}{{else}}{{if .Args}} [arguments...]{{end}}{{end}}{{end}}{{if .Version}}{{if not .HideVersion}}
VERSION:
{{.Version}}{{end}}{{end}}{{if .Description}}
DESCRIPTION:
{{template "descriptionTemplate" .}}{{end}}
{{- if len .Authors}}
AUTHOR{{template "authorsTemplate" .}}{{end}}{{if .VisibleFlagCategories}}
OPTIONS:{{template "visibleFlagCategoryTemplate" .}}{{else if .VisibleFlags}}
OPTIONS:{{template "visibleFlagTemplate" .}}{{end}}{{if .Copyright}}
COPYRIGHT:
{{template "copyrightTemplate" .}}{{end}}`
app := (&cli.App{
Name: "randompassword",
Usage: "Generate secure random passwords",
Flags: []cli.Flag{
&cli.IntFlag{
Name: "length",
Value: 24,
Usage: "Length of the password(s)",
},
&cli.IntFlag{
Name: "count",
Value: 10,
Usage: "Number of passwords to generate",
},
},
Action: func(c *cli.Context) error {
passwordLength := int(c.Int("length"))
if passwordLength < 1 {
panic("Password length must be greater than 0.")
}
count := c.Int("count")
if count < 1 {
panic("Count must be greater than 0.")
}
for g := 0; g < c.Int("count"); g++ {
RandomPassword(passwordLength)
fmt.Println("")
}
return nil
},
})
if err := app.Run(os.Args); err != nil {
log.Fatal(err)
}
}
func RandomPassword(passwordLength int) {
// Bias for alpha and numbers
allChars := alpha + alpha + numbers + numbers + special
length := int64(len(allChars))
for i := 0; i < passwordLength; i++ {
fmt.Print(RandomChar(allChars, length))
}
}
func RandomChar(s string, length int64) string {
x, err := rand.Int(rand.Reader, big.NewInt(length))
if err != nil {
fmt.Println("ERROR: ", err)
panic(err)
}
y := x.Int64()
return string(s[y])
}