forked from kryptco/kr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.go
41 lines (36 loc) · 877 Bytes
/
util.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
package kr
import (
"crypto/rand"
"encoding/base64"
// base62 for random and compatible strings
"github.com/keybase/saltpack/encoding/basex"
)
func RandNBytes(n uint) (randBytes []byte, err error) {
randBytes = make([]byte, n)
_, err = rand.Read(randBytes)
return
}
func Rand256Base62() (encodedRand string, err error) {
return RandNBase62(32)
}
func Rand128Base62() (encodedRand string, err error) {
return RandNBase62(16)
}
func RandNBase62(n uint) (encodedRand string, err error) {
randBuf, err := RandNBytes(n)
_, err = rand.Read(randBuf)
if err != nil {
return
}
encodedRand = basex.Base62StdEncoding.EncodeToString(randBuf)
return
}
func RandNBase64(n uint) (encodedRand string, err error) {
randBuf, err := RandNBytes(n)
_, err = rand.Read(randBuf)
if err != nil {
return
}
encodedRand = base64.StdEncoding.EncodeToString(randBuf)
return
}