diff --git a/demo/clientp.go b/demo/clientp.go index ff8838e..4805bed 100644 --- a/demo/clientp.go +++ b/demo/clientp.go @@ -2,7 +2,7 @@ package main import ( "fmt" - + "net" ) diff --git a/demo/server.go b/demo/server.go index b4a03f3..180f911 100644 --- a/demo/server.go +++ b/demo/server.go @@ -44,7 +44,7 @@ func main() { go func(ch chan []byte, eCh chan error) { for { // try to read the data - data := make([]byte, 8) + data := make([]byte, 512) chN, err = c.Read(data) if err != nil { // send an error if it's encountered diff --git a/hkexnet.go b/hkexnet.go index 29658be..b6a24f3 100644 --- a/hkexnet.go +++ b/hkexnet.go @@ -126,13 +126,14 @@ func (hc Conn) Read(b []byte) (n int, err error) { if err != nil && err.Error() != "EOF" { panic(err) } - if n > 0 { - fmt.Printf(" ctext:%+v\n", b[:n]) - db := bytes.NewBuffer(b[:n]) - rs := &cipher.StreamReader{S: hc.r, R: db} - n, err = rs.Read(b) - fmt.Printf(" ptext:%+v\n", b[:n]) - } + fmt.Printf(" ctext:%+v\n", b[:n]) // print only used portion + db := bytes.NewBuffer(b[:n]) + // The StreamReader acts like a pipe, decrypting + // whatever is available and forwarding the result + // to the parameter of Read() as a normal io.Reader + rs := &cipher.StreamReader{S: hc.r, R: db} + n, err = rs.Read(b) + fmt.Printf(" ptext:%+v\n", b[:n]) return } @@ -140,6 +141,8 @@ func (hc Conn) Write(b []byte) (n int, err error) { fmt.Printf("[Encrypting...]\n") fmt.Printf(" ptext:%+v\n", b) var wb bytes.Buffer + // The StreamWriter acts like a pipe, forwarding whatever is + // written to it through the cipher, encrypting as it goes ws := &cipher.StreamWriter{S: hc.w, W: &wb} n, err = ws.Write(b) fmt.Printf(" ctext:%+v\n", wb.Bytes())