mediocre-go-lib/mtest/mtest.go

30 lines
637 B
Go
Raw Permalink Normal View History

2018-02-11 16:05:36 +00:00
// Package mtest contains types and functions which are useful when writing
// tests
package mtest
import (
crand "crypto/rand"
"encoding/hex"
"math/rand"
"time"
)
// Rand is a public instance of rand.Rand, seeded with the current
// nano-timestamp
var Rand = rand.New(rand.NewSource(time.Now().UnixNano()))
// RandBytes returns n random bytes
func RandBytes(n int) []byte {
b := make([]byte, n)
if _, err := crand.Read(b); err != nil {
panic(err)
}
return b
}
// RandHex returns a random hex string which is n characters long
func RandHex(n int) string {
b := RandBytes(hex.DecodedLen(n))
return hex.EncodeToString(b)
}