-
Notifications
You must be signed in to change notification settings - Fork 0
/
pass-generator.go
73 lines (58 loc) · 1.38 KB
/
pass-generator.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
package main
import (
"flag"
"fmt"
"math/rand"
"time"
)
var (
length = flag.Int("length", 12, "Password length")
useLower = flag.Bool("lower", true, "Use lowercase letters")
useUpper = flag.Bool("upper", true, "Use uppercase letters")
useDigits = flag.Bool("digits", false, "Use digits")
useSpecial = flag.Bool("special", false, "Use special characters")
)
const (
lowercase = "abcdefghijklmnopqrstuvwxyz"
uppercase = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
digits = "0123456789"
special = "!@#$%^&*()-_=+,.?/:;{}[]`~"
)
func generatePassword() string {
var charset string
if *useLower {
charset += lowercase
}
if *useUpper {
charset += uppercase
}
if *useDigits {
charset += digits
}
if *useSpecial {
charset += special
}
if charset == "" {
panic("No character sets selected")
}
r := rand.New(rand.NewSource(time.Now().UnixNano()))
password := make([]byte, *length)
for i := range password {
password[i] = charset[r.Intn(len(charset))]
}
return string(password)
}
func main() {
flag.Parse()
if *length <= 0 {
fmt.Println("Error: Password length must be greater than 0.")
return
}
if !(*useLower || *useUpper || *useDigits || *useSpecial) {
fmt.Println("Error: At least one character set must be selected.")
fmt.Println("Use -h for help.")
return
}
password := generatePassword()
fmt.Println(password)
}