go-libp2p-peerstore/test/test_utils.go
2018-09-02 12:10:55 +01:00

37 lines
805 B
Go

package test
import (
"io"
"math/rand"
"time"
ci "github.com/libp2p/go-libp2p-crypto"
"github.com/libp2p/go-libp2p-peer"
mh "github.com/multiformats/go-multihash"
)
func timeSeededRand() io.Reader {
return rand.New(rand.NewSource(time.Now().UnixNano()))
}
func RandPeerID() (peer.ID, error) {
buf := make([]byte, 16)
if _, err := io.ReadFull(timeSeededRand(), buf); err != nil {
return "", err
}
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) {
return ci.GenerateKeyPairWithReader(ci.RSA, bits, timeSeededRand())
}
func SeededTestKeyPair(seed int64) (ci.PrivKey, ci.PubKey, error) {
return ci.GenerateKeyPairWithReader(ci.RSA, 512, rand.New(rand.NewSource(seed)))
}