From a93672449b5647ff9904510d6fafc9f42836d4ce Mon Sep 17 00:00:00 2001 From: Leo Chen Date: Fri, 18 Jan 2019 07:15:55 +0000 Subject: [PATCH] add functions to generate keypairs for libp2p add two functions and the tests Signed-off-by: Leo Chen --- internal/utils/utils.go | 14 ++++++++++++++ internal/utils/utils_test.go | 30 ++++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+) diff --git a/internal/utils/utils.go b/internal/utils/utils.go index 4b30d1064..775bf4269 100644 --- a/internal/utils/utils.go +++ b/internal/utils/utils.go @@ -6,11 +6,14 @@ import ( "encoding/json" "io" "log" + mrand "math/rand" "os" "regexp" "strconv" "sync" + p2p_crypto "github.com/libp2p/go-libp2p-crypto" + "github.com/dedis/kyber" "github.com/harmony-one/harmony/crypto" "github.com/harmony-one/harmony/crypto/pki" @@ -70,6 +73,17 @@ func GenKey(ip, port string) (kyber.Scalar, kyber.Point) { return priKey, pubKey } +// GenKeyP2P generates a pair of RSA keys used in libp2p host +func GenKeyP2P(ip, port string) (p2p_crypto.PrivKey, p2p_crypto.PubKey, error) { + r := mrand.New(mrand.NewSource(int64(GetUniqueIDFromIPPort(ip, port)))) + return p2p_crypto.GenerateKeyPairWithReader(p2p_crypto.RSA, 2048, r) +} + +// GenKeyP2PRand generates a pair of RSA keys used in libp2p host, using random seed +func GenKeyP2PRand() (p2p_crypto.PrivKey, p2p_crypto.PubKey, error) { + return p2p_crypto.GenerateKeyPair(p2p_crypto.RSA, 2048) +} + // AllocateShard uses the number of current nodes and number of shards // to return the shardNum a new node belongs to, it also tells whether the node is a leader func AllocateShard(numOfAddedNodes, numOfShards int) (int, bool) { diff --git a/internal/utils/utils_test.go b/internal/utils/utils_test.go index 275b33c56..cd5e5d921 100644 --- a/internal/utils/utils_test.go +++ b/internal/utils/utils_test.go @@ -4,6 +4,7 @@ import ( "testing" "github.com/harmony-one/harmony/p2p" + crypto "github.com/libp2p/go-libp2p-crypto" "github.com/stretchr/testify/assert" ) @@ -47,6 +48,35 @@ func TestGenKey(t *testing.T) { GenKey("3.3.3.3", "3456") } +// Test for GenKeyP2P +func TestGenKeyP2P(t *testing.T) { + pi, pb, err := GenKeyP2P("127.0.0.1", "8888") + if err != nil { + t.Errorf("GenKeyP2p Error: %v", err) + } + kpi, _ := crypto.MarshalPrivateKey(pi) + kpb, _ := crypto.MarshalPublicKey(pb) + if len(kpi) != 1198 { + t.Errorf("Length of Private Key Error: %v, expected 1198", len(kpi)) + } + if len(kpb) != 299 { + t.Errorf("Length of Public Key Error: %v, expected 299", len(kpb)) + } +} + +// Test for GenKeyP2PRand, noted the length of private key can be random +// thus we don't test it here. +func TestGenKeyP2PRand(t *testing.T) { + _, pb, err := GenKeyP2PRand() + if err != nil { + t.Errorf("GenKeyP2PRand Error: %v", err) + } + kpb, _ := crypto.MarshalPublicKey(pb) + if len(kpb) != 299 { + t.Errorf("Length of Public Key Error: %v, expected 299", len(kpb)) + } +} + // Test for GetUniqueIDFromPeer func TestGetUniqueIDFromPeer(t *testing.T) { peer := p2p.Peer{IP: "1.1.1.1", Port: "123"}