aboutsummaryrefslogtreecommitdiffstats
path: root/vendor/github.com/dexon-foundation/dexon-consensus/common/utils.go
blob: 0e847900f464e0f08688b898418ac86715685f18 (plain) (blame)
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
package common

import (
    "math/rand"
    "time"
)

var random *rand.Rand

func init() {
    random = rand.New(rand.NewSource(time.Now().Unix()))
}

// NewRandomHash returns a random Hash-like value.
func NewRandomHash() Hash {
    x := Hash{}
    for i := 0; i < HashLength; i++ {
        x[i] = byte(random.Int() % 256)
    }
    return x
}

// GenerateRandomBytes generates bytes randomly.
func GenerateRandomBytes() []byte {
    randomness := make([]byte, 32)
    _, err := rand.Read(randomness)
    if err != nil {
        panic(err)
    }
    return randomness
}

// CopyBytes copies byte slice.
func CopyBytes(src []byte) (dst []byte) {
    if len(src) == 0 {
        return
    }
    dst = make([]byte, len(src))
    copy(dst, src)
    return
}