-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
62 lines (56 loc) · 1.67 KB
/
main.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
package main
import (
"crypto/rand"
"errors"
"fmt"
"log"
"os"
"strconv"
"github.com/urfave/cli"
)
func main() {
app := cli.NewApp()
app.Name = "pswgen"
app.Usage = "Password generator"
app.Version = "0.0.1"
app.Flags = []cli.Flag{
cli.BoolFlag{
Name: "symbol, s",
Usage: "Including symbol",
},
}
app.Action = func(c *cli.Context) error {
if c.Args() == nil || len(c.Args()) == 0 {
log.SetFlags(0)
log.Fatal("\npswgen [OPTIONS] [PASSWORD LENGTH]\n\nOPTIONS:\n -h, --help:\n Show this help message and exit.\n -v, --version:\n Show version and exit.\n -s, --symbol:\n Add symbol to password.")
}
i, err := strconv.ParseUint(c.Args()[0], 10, 64)
if err != nil || i < 1 {
log.SetFlags(0)
log.Fatal("\npswgen [OPTIONS] [PASSWORD LENGTH]\n\nOPTIONS:\n -h, --help:\n Show this help message and exit.\n -v, --version:\n Show version and exit.\n -s, --symbol:\n Add symbol to password.")
}
random, _ := MakeRandomStr(i, c.Bool("symbol"))
fmt.Println(random)
return nil
}
err := app.Run(os.Args)
if err != nil {
log.SetFlags(0)
log.Fatal("\npswgen [OPTIONS] [PASSWORD LENGTH]\n\nOPTIONS:\n -h, --help:\n Show this help message and exit.\n -v, --version:\n Show version and exit.\n -s, --symbol:\n Add symbol to password.")
}
}
func MakeRandomStr(digit uint64, symbol bool) (string, error) {
var letters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
if symbol {
letters += "-_/*+.,!#$%&()~|"
}
b := make([]byte, digit)
if _, err := rand.Read(b); err != nil {
return "", errors.New("unexpected error")
}
var result string
for _, v := range b {
result += string(letters[int(v)%len(letters)])
}
return result, nil
}