forked from noxecane/anansi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rand.go
30 lines (26 loc) · 957 Bytes
/
rand.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
package siber
import (
"crypto/rand"
"encoding/hex"
)
// RandomBytes returns securely generated random bytes. It will return an
// error if the system's secure random number generator fails to
// function correctly, in which case the caller should not continue.
func RandomBytes(n int) ([]byte, error) {
b := make([]byte, n)
_, err := rand.Read(b)
// Note that err == nil only if we read len(b) bytes.
if err != nil {
return nil, err
}
return b, nil
}
// RandomString returns a securely generated random hex string.
// It will return an error if the system's secure random number generator fails
// to function correctly, in which case the caller should not continue. Keep in mind
// that s is the length of the returned string, not the number of bytes to be produced
func RandomString(s int) (string, error) {
// use half of the size as hex always returns double the size.
b, err := RandomBytes(s / 2)
return hex.EncodeToString(b), err
}