-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcrypto.go
More file actions
38 lines (29 loc) · 695 Bytes
/
crypto.go
File metadata and controls
38 lines (29 loc) · 695 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
package main
import (
"crypto/rand"
"encoding/base64"
)
func GenRandBytes(n int) ([]byte, error) {
bytes := make([]byte, n)
_, err := rand.Read(bytes)
if err != nil {
return nil, err
}
return bytes, nil
}
func GenRandString(n int) (string, error) {
// const alpha = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz-"
const alpha = "023456789abcdefghjkmnopqrstvwxyz"
bytes, err := GenRandBytes(n)
if err != nil {
return "", err
}
for i, b := range bytes {
bytes[i] = alpha[b%byte(len(alpha))]
}
return string(bytes), nil
}
func GenRandURL(n int) (string, error) {
bytes, err := GenRandBytes(n)
return base64.URLEncoding.EncodeToString(bytes), err
}