Commit c85dd281 authored by Jeromy's avatar Jeromy

add test for crypto handshake

parent 48865db1
......@@ -54,6 +54,7 @@ func Handshake(self, remote *peer.Peer, in, out chan []byte) error {
out <- encrypted
challenge := <-in
// Decrypt challenge and send plaintext to partner
plain, err := rsa.DecryptPKCS1v15(rand.Reader, self.PrivKey.(*rsa.PrivateKey), challenge)
if err != nil {
return err
......@@ -108,15 +109,15 @@ type KeyPair struct {
Priv crypto.PrivateKey
}
func GenKeypair() (*KeyPair, error) {
priv, err := rsa.GenerateKey(rand.Reader, 4096)
func GenKeypair(bits int) (*KeyPair, error) {
priv, err := rsa.GenerateKey(rand.Reader, bits)
if err != nil {
return nil, err
}
return &KeyPair{
Priv: priv,
Pub: priv.PublicKey,
Pub: &priv.PublicKey,
}, nil
}
......
package identify
import (
"testing"
"github.com/jbenet/go-ipfs/peer"
)
func TestHandshake(t *testing.T) {
kpa, err := GenKeypair(512)
if err != nil {
t.Fatal(err)
}
kpb, err := GenKeypair(512)
if err != nil {
t.Fatal(err)
}
cha := make(chan []byte, 5)
chb := make(chan []byte, 5)
ida, err := kpa.ID()
if err != nil {
t.Fatal(err)
}
pa := &peer.Peer{
ID: ida,
PubKey: kpa.Pub,
PrivKey: kpa.Priv,
}
idb, err := kpb.ID()
if err != nil {
t.Fatal(err)
}
pb := &peer.Peer{
ID: idb,
PubKey: kpb.Pub,
PrivKey: kpb.Priv,
}
go func() {
err := Handshake(pa, pb, cha, chb)
if err != nil {
t.Fatal(err)
}
}()
err = Handshake(pb, pa, chb, cha)
if err != nil {
t.Fatal(err)
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment