Esempio n. 1
0
// echo performs a public-key handshake with a provided network connection and
// uses a secure reader and writer to unencrypt and re-encrypt any message sent
// on the connection, echoing the message back.
func echo(c net.Conn, priv, pub *[32]byte, errc chan error) {
	peersPub, err := receivePublic(c)
	if err != nil {
		c.Write([]byte("error receiving public key"))
		return
	}
	if err := sendPublic(c, pub); err != nil {
		errc <- err
	}

	secureR := secureio.NewSecureReader(c, priv, peersPub)
	secureW := secureio.NewSecureWriter(c, priv, peersPub)
	io.Copy(secureW, secureR)
}
Esempio n. 2
0
func TestReadWriterPing(t *testing.T) {
	priv, pub := &[32]byte{'p', 'r', 'i', 'v'}, &[32]byte{'p', 'u', 'b'}

	r, w := io.Pipe()
	defer w.Close()
	secureR := secureio.NewSecureReader(r, priv, pub)
	secureW := secureio.NewSecureWriter(w, priv, pub)

	// Encrypt hello world
	go fmt.Fprintf(secureW, "hello world\n")

	// Decrypt message
	buf := make([]byte, 1024)
	n, err := secureR.Read(buf)
	if err != nil {
		t.Fatal(err)
	}
	buf = buf[:n]

	// Make sure we have hello world back
	if res := string(buf); res != "hello world\n" {
		t.Fatalf("Unexpected result: %s != %s", res, "hello world")
	}
}