@@ -2,19 +2,39 @@ package utils
2
2
3
3
import (
4
4
"github.com/gofrs/uuid/v5"
5
- "github.com/zhangyunhao116/fastrand "
5
+ "github.com/metacubex/randv2 "
6
6
)
7
7
8
- type fastRandReader struct {}
8
+ type unsafeRandReader struct {}
9
9
10
- func (r fastRandReader ) Read (p []byte ) (int , error ) {
11
- return fastrand .Read (p )
10
+ func (r unsafeRandReader ) Read (p []byte ) (n int , err error ) {
11
+ // modify from https://github.com/golang/go/blob/587c3847da81aa7cfc3b3db2677c8586c94df13a/src/runtime/rand.go#L70-L89
12
+ // Inspired by wyrand.
13
+ n = len (p )
14
+ v := randv2 .Uint64 ()
15
+ for len (p ) > 0 {
16
+ v ^= 0xa0761d6478bd642f
17
+ v *= 0xe7037ed1a0b428db
18
+ size := 8
19
+ if len (p ) < 8 {
20
+ size = len (p )
21
+ }
22
+ for i := 0 ; i < size ; i ++ {
23
+ p [i ] ^= byte (v >> (8 * i ))
24
+ }
25
+ p = p [size :]
26
+ v = v >> 32 | v << 32
27
+ }
28
+
29
+ return
12
30
}
13
31
14
- var UnsafeUUIDGenerator = uuid .NewGenWithOptions (uuid .WithRandomReader (fastRandReader {}))
32
+ var UnsafeRandReader = unsafeRandReader {}
33
+
34
+ var UnsafeUUIDGenerator = uuid .NewGenWithOptions (uuid .WithRandomReader (UnsafeRandReader ))
15
35
16
36
func NewUUIDV1 () uuid.UUID {
17
- u , _ := UnsafeUUIDGenerator .NewV1 () // fastrand.Read wouldn't cause error, so ignore err is safe
37
+ u , _ := UnsafeUUIDGenerator .NewV1 () // unsafeRandReader wouldn't cause error, so ignore err is safe
18
38
return u
19
39
}
20
40
@@ -23,7 +43,7 @@ func NewUUIDV3(ns uuid.UUID, name string) uuid.UUID {
23
43
}
24
44
25
45
func NewUUIDV4 () uuid.UUID {
26
- u , _ := UnsafeUUIDGenerator .NewV4 () // fastrand.Read wouldn't cause error, so ignore err is safe
46
+ u , _ := UnsafeUUIDGenerator .NewV4 () // unsafeRandReader wouldn't cause error, so ignore err is safe
27
47
return u
28
48
}
29
49
@@ -32,12 +52,12 @@ func NewUUIDV5(ns uuid.UUID, name string) uuid.UUID {
32
52
}
33
53
34
54
func NewUUIDV6 () uuid.UUID {
35
- u , _ := UnsafeUUIDGenerator .NewV6 () // fastrand.Read wouldn't cause error, so ignore err is safe
55
+ u , _ := UnsafeUUIDGenerator .NewV6 () // unsafeRandReader wouldn't cause error, so ignore err is safe
36
56
return u
37
57
}
38
58
39
59
func NewUUIDV7 () uuid.UUID {
40
- u , _ := UnsafeUUIDGenerator .NewV7 () // fastrand.Read wouldn't cause error, so ignore err is safe
60
+ u , _ := UnsafeUUIDGenerator .NewV7 () // unsafeRandReader wouldn't cause error, so ignore err is safe
41
61
return u
42
62
}
43
63
0 commit comments