mirror of
https://github.com/libp2p/go-libp2p-peerstore.git
synced 2025-01-04 00:50:12 +08:00
37 lines
814 B
Go
37 lines
814 B
Go
package testutil
|
|
|
|
import (
|
|
"io"
|
|
"math/rand"
|
|
"time"
|
|
|
|
ci "github.com/libp2p/go-libp2p-crypto"
|
|
peer "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)))
|
|
}
|