-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrandom.go
64 lines (53 loc) · 1.43 KB
/
random.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package hydrogen
// #cgo LDFLAGS: -lhydrogen
// #include <stdlib.h>
// #include <hydrogen.h>
import "C"
import (
"unsafe"
)
const (
RandomSeedBytes int = C.hydro_random_SEEDBYTES
)
// returns an unpredicatable value from 0 - 0xffffffff (included)
// Prototype:
// uint32_t hydro_random_u32(void);
func RandomU32() uint32 {
return uint32(C.hydro_random_u32())
}
// returns an unpredictable value between 0 and upper_bound (excluded)
// Prototype:
// uint32_t hydro_random_uniform(const uint32_t upper_bound);
func RandomUniform(upper_bound uint32) uint32 {
return uint32(C.hydro_random_uniform(C.uint(upper_bound)))
}
// Prototype:
// void hydro_random_buf(void *buf, size_t len);
func RandomBuf(l int) []byte {
CheckIntGt(l, 0, "random buf size")
out := make([]byte, l)
C.hydro_random_buf(unsafe.Pointer(&out[0]), C.size_t(l))
return out
}
// Prototype:
// void hydro_random_buf_deterministic(void *buf, size_t len, const uint8_t seed[hydro_random_SEEDBYTES]);
func RandomBufDeterministic(l int, seed []byte) []byte {
CheckSize(seed, RandomSeedBytes, "seed")
CheckIntGt(l, 0, "random buf det size")
out := make([]byte, l)
C.hydro_random_buf_deterministic(
unsafe.Pointer(&out[0]),
C.size_t(l),
(*C.uint8_t)(&seed[0]))
return out
}
// Prototype:
// void hydro_random_ratchet(void);
func RandomRatchet() {
C.hydro_random_ratchet()
}
// Prototype:
// void hydro_random_reseed(void);
func RandomReseed() {
C.hydro_random_reseed()
}