-
Notifications
You must be signed in to change notification settings - Fork 12
/
rand.go
57 lines (45 loc) · 949 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
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
// Copyright (c) WithSecure Corporation
// https://foundry.withsecure.com
//
// Use of this source code is governed by the license
// that can be found in the LICENSE file.
package cmd
import (
"crypto/rand"
"fmt"
"log"
"time"
"golang.org/x/term"
)
const (
rngRounds = 10
rngSize = 32
rngCount = 1000
)
func init() {
Add(Cmd{
Name: "rand",
Help: "gather 32 random bytes",
Fn: randCmd,
})
}
func randCmd(_ *Interface, _ *term.Terminal, _ []string) (string, error) {
buf := make([]byte, 32)
rand.Read(buf)
return fmt.Sprintf("%x", buf), nil
}
func rngTest() {
msg("rng (%d runs)", rngRounds)
for i := 0; i < rngRounds; i++ {
rng := make([]byte, rngSize)
rand.Read(rng)
log.Printf("%x", rng)
}
msg("rng benchmark")
start := time.Now()
for i := 0; i < rngCount; i++ {
rng := make([]byte, rngSize)
rand.Read(rng)
}
log.Printf("retrieved %d random bytes in %s", rngSize*rngCount, time.Since(start))
}