go-libp2p-peerstore/test/utils.go

37 lines
814 B
Go
Raw Normal View History

package testutil
import (
"io"
2017-03-24 09:57:39 +08:00
"math/rand"
"time"
2017-02-04 04:49:59 +08:00
ci "github.com/libp2p/go-libp2p-crypto"
peer "github.com/libp2p/go-libp2p-peer"
2017-03-24 09:57:39 +08:00
mh "github.com/multiformats/go-multihash"
)
2017-03-24 09:57:39 +08:00
func timeSeededRand() io.Reader {
return rand.New(rand.NewSource(time.Now().UnixNano()))
}
func RandPeerID() (peer.ID, error) {
buf := make([]byte, 16)
2017-03-24 09:57:39 +08:00
if _, err := io.ReadFull(timeSeededRand(), buf); err != nil {
return "", err
}
2017-03-24 09:57:39 +08:00
h, err := mh.Sum(buf, mh.SHA2_256, -1)
if err != nil {
return "", err
}
return peer.ID(h), nil
}
func RandTestKeyPair(bits int) (ci.PrivKey, ci.PubKey, error) {
2017-03-24 09:57:39 +08:00
return ci.GenerateKeyPairWithReader(ci.RSA, bits, timeSeededRand())
}
func SeededTestKeyPair(seed int64) (ci.PrivKey, ci.PubKey, error) {
2017-03-24 09:57:39 +08:00
return ci.GenerateKeyPairWithReader(ci.RSA, 512, rand.New(rand.NewSource(seed)))
}