-
Notifications
You must be signed in to change notification settings - Fork 5
/
util.go
49 lines (42 loc) · 911 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
42
43
44
45
46
47
48
49
package xingyun
//This code is adapted from https://github.com/dustin/randbo
import (
"fmt"
"io"
"math/rand"
"time"
)
// Randbo creates a stream of non-crypto quality random bytes
type randbo struct {
rand.Source
}
// New creates a new random reader with a time source.
func newRandbo() io.Reader {
return newRandoFrom(rand.NewSource(time.Now().UnixNano()))
}
// NewFrom creates a new reader from your own rand.Source
func newRandoFrom(src rand.Source) io.Reader {
return &randbo{src}
}
// Read satisfies io.Reader
func (r *randbo) Read(p []byte) (n int, err error) {
todo := len(p)
offset := 0
for {
val := int64(r.Int63())
for i := 0; i < 8; i++ {
p[offset] = byte(val)
todo--
if todo == 0 {
return len(p), nil
}
offset++
val >>= 8
}
}
}
func GenRandString(length int) string {
buf := make([]byte, length)
newRandbo().Read(buf)
return fmt.Sprintf("%x", buf)
}