-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathcryptoRand.go
More file actions
46 lines (40 loc) · 757 Bytes
/
cryptoRand.go
File metadata and controls
46 lines (40 loc) · 757 Bytes
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
package main
import (
"crypto/rand"
"encoding/base64"
"fmt"
"os"
"strconv"
)
// This function returns (secure) random bytes
func generateBytes(n int64) ([]byte, error) {
b := make([]byte, n)
_, err := rand.Read(b)
if err != nil {
return nil, err
}
return b, nil
}
func generatePass(s int64) (string, error) {
b, err := generateBytes(s)
return base64.URLEncoding.EncodeToString(b), err
}
func main() {
var LENGTH int64 = 8
arguments := os.Args
switch len(arguments) {
case 2:
LENGTH, _ = strconv.ParseInt(os.Args[1], 10, 64)
if LENGTH <= 0 {
LENGTH = 8
}
default:
fmt.Println("Using default values!")
}
myPass, err := generatePass(LENGTH)
if err != nil {
fmt.Println(err)
return
}
fmt.Println(myPass[0:LENGTH])
}