-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
49 lines (42 loc) · 992 Bytes
/
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
package main
import (
"flag"
"fmt"
"math/big"
"crypto/rand"
"encoding/base64"
)
var (
optLength = flag.Int("l", 10, "\"i\" length")
optQuantity = flag.Int("q", 1, "\"q\" quantity")
optEncoding = flag.String("e", "", "\"e\" encoding (ex: base64)")
)
func main() {
flag.Parse()
length := *optLength
qty := *optQuantity
encoding := *optEncoding
for i := 0; i < qty; i++ {
secret, err := generateRandomString(length)
if err != nil {
fmt.Print(err.Error())
return
}
if encoding == "base64" {
secret = base64.StdEncoding.EncodeToString([]byte(secret))
}
fmt.Println(secret)
}
}
func generateRandomString(n int) (string, error) {
const letters = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz!@#$%^&*()_+|~-=[]{};:"
ret := make([]byte, n)
for i := 0; i < n; i++ {
num, err := rand.Int(rand.Reader, big.NewInt(int64(len(letters))))
if err != nil {
return "", err
}
ret[i] = letters[num.Int64()]
}
return string(ret), nil
}