add util functions to convert p2p private keys to/from base64 string

Signed-off-by: Leo Chen <leo@harmony.one>
pull/347/head
Leo Chen 6 years ago
parent 4c9e421932
commit bbc1192cf6
  1. 30
      internal/utils/utils.go
  2. 40
      internal/utils/utils_test.go

@ -4,6 +4,7 @@ import (
"bytes" "bytes"
"encoding/binary" "encoding/binary"
"encoding/json" "encoding/json"
"fmt"
"io" "io"
"log" "log"
mrand "math/rand" mrand "math/rand"
@ -153,3 +154,32 @@ func Load(path string, v interface{}) error {
defer f.Close() defer f.Close()
return Unmarshal(f, v) return Unmarshal(f, v)
} }
// LoadPrivateKey parses the key string in base64 format and return PrivKey
func LoadPrivateKey(key string) (*p2p_crypto.PrivKey, error) {
if key != "" {
k1, err := p2p_crypto.ConfigDecodeKey(key)
if err != nil {
return nil, fmt.Errorf("failed to decode key: %v", err)
}
priKey, err := p2p_crypto.UnmarshalPrivateKey(k1)
if err != nil {
return nil, fmt.Errorf("failed to unmarshal private key: %v", err)
}
return &priKey, nil
}
return nil, fmt.Errorf("empty key string")
}
// SavePrivateKey convert the PrivKey to base64 format and return string
func SavePrivateKey(key *p2p_crypto.PrivKey) (string, error) {
if key != nil {
b, err := p2p_crypto.MarshalPrivateKey(*key)
if err != nil {
return "", fmt.Errorf("failed to marshal private key: %v", err)
}
str := p2p_crypto.ConfigEncodeKey(b)
return str, nil
}
return "", fmt.Errorf("key is nil")
}

@ -99,3 +99,43 @@ func TestGetUniqueIDFromPeer(t *testing.T) {
func TestGetUniqueIDFromIPPort(t *testing.T) { func TestGetUniqueIDFromIPPort(t *testing.T) {
assert.Equal(t, GetUniqueIDFromIPPort("1.1.1.1", "123"), uint32(1111123), "should be equal to 1111123") assert.Equal(t, GetUniqueIDFromIPPort("1.1.1.1", "123"), uint32(1111123), "should be equal to 1111123")
} }
var (
savedKey = "CAASqQkwggSlAgEAAoIBAQCwKJVWOIbfLXNKxK8VetvHS7eEOtC8VJ/GoyeWsaaGVknLyDGYkvWYpeYpu5hsKYfq6yPPBOjKq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QOxMK0MdIBIot490mgV3XufQv2XFL1cx6rARzVtRpmgI6gl8Yv1NRvzDKzknl3zuMzTgr8hrhQKBgQDNlqWZanvnaN8d7Vh4BXyQpaoRczybHqQPnmHUeI0gxoGVy5Mgp8qff2lD84hnvntjWNjkMw16/PvWwEayLbsUS9byRTiBvX3wtNQgi+0lbd3dMuEW+WgE9Ij0VoD6F4m1O0j04pWuPtVWwclrNWuyKtZJvgqQQdRrYGsMyQj+VQKBgQCC15mFp1nh3/KfB07JU3w7pcqalvq5ZrkZDK9NrgwXNoXMWxgQ/2BfoX2rETM2HbfCIN5rLSt2paElkxIn8fF2NV/e4Hdk3WMfd0OFhMZ4hJCscx2DukMSW2qJ4sp97RudplsK9eW0y8OIhY1UF8eME9bOdWD0bU10QiMNwVzOWQ=="
)
// Test for SavePrivateKey function
func TestSavePrivateKey(t *testing.T) {
pk, _, err := GenKeyP2P("127.0.0.1", "8888")
if err != nil {
t.Fatalf("failed to generate p2p key: %v", err)
}
str, err := SavePrivateKey(&pk)
if err != nil {
t.Fatalf("failed to save private key: %v", err)
}
if str != savedKey {
t.Errorf("key is not right")
t.Errorf("got: %s", str)
t.Errorf("expecting: %s", savedKey)
}
}
// Test for LoadPrivateKey function
func TestLoadPrivateKey(t *testing.T) {
pk, _, err := GenKeyP2P("127.0.0.1", "8888")
if err != nil {
t.Fatalf("failed to generate p2p key: %v", err)
}
pk1, err := LoadPrivateKey(savedKey)
if err != nil {
t.Fatalf("failed to load key: %v", err)
}
if !crypto.KeyEqual(pk, *pk1) {
t.Errorf("loaded key is not right")
b1, _ := pk.Bytes()
b2, _ := (*pk1).Bytes()
t.Errorf("expecting pk: %v\n", b1)
t.Errorf("got pk1: %v\n", b2)
}
}

Loading…
Cancel
Save