// Serve starts a secure echo server on the given listener. func Serve(l net.Listener) error { pub, priv, err := secureio.GenerateKeyPair() if err != nil { return err } // Start the server for { conn, err := l.Accept() if err != nil { return err } go handleServerConnection(pub, priv, conn) } }
// Dial generates a private/public key pair, // connects to the server, perform the handshake // and return a reader/writer. func Dial(addr string) (io.ReadWriteCloser, error) { pub, priv, err := secureio.GenerateKeyPair() if err != nil { return nil, err } conn, err := net.Dial("tcp", addr) if err != nil { return nil, err } peerPub := new([32]byte) if err := exchangeKeys(pub, peerPub, conn); err != nil { return nil, err } return NewSecureReadWriteCloser(conn, priv, peerPub), nil }