Bit of cleanup in hkex.Read(),Write() and server.go read bufsize to 512

This commit is contained in:
Russ Magee 2018-01-11 09:13:18 -08:00
parent c43b13989b
commit d4c9a1e456
3 changed files with 12 additions and 9 deletions

View File

@ -2,7 +2,7 @@ package main
import ( import (
"fmt" "fmt"
"net" "net"
) )

View File

@ -44,7 +44,7 @@ func main() {
go func(ch chan []byte, eCh chan error) { go func(ch chan []byte, eCh chan error) {
for { for {
// try to read the data // try to read the data
data := make([]byte, 8) data := make([]byte, 512)
chN, err = c.Read(data) chN, err = c.Read(data)
if err != nil { if err != nil {
// send an error if it's encountered // send an error if it's encountered

View File

@ -126,13 +126,14 @@ func (hc Conn) Read(b []byte) (n int, err error) {
if err != nil && err.Error() != "EOF" { if err != nil && err.Error() != "EOF" {
panic(err) panic(err)
} }
if n > 0 { fmt.Printf(" ctext:%+v\n", b[:n]) // print only used portion
fmt.Printf(" ctext:%+v\n", b[:n]) db := bytes.NewBuffer(b[:n])
db := bytes.NewBuffer(b[:n]) // The StreamReader acts like a pipe, decrypting
rs := &cipher.StreamReader{S: hc.r, R: db} // whatever is available and forwarding the result
n, err = rs.Read(b) // to the parameter of Read() as a normal io.Reader
fmt.Printf(" ptext:%+v\n", b[:n]) rs := &cipher.StreamReader{S: hc.r, R: db}
} n, err = rs.Read(b)
fmt.Printf(" ptext:%+v\n", b[:n])
return return
} }
@ -140,6 +141,8 @@ func (hc Conn) Write(b []byte) (n int, err error) {
fmt.Printf("[Encrypting...]\n") fmt.Printf("[Encrypting...]\n")
fmt.Printf(" ptext:%+v\n", b) fmt.Printf(" ptext:%+v\n", b)
var wb bytes.Buffer 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} ws := &cipher.StreamWriter{S: hc.w, W: &wb}
n, err = ws.Write(b) n, err = ws.Write(b)
fmt.Printf(" ctext:%+v\n", wb.Bytes()) fmt.Printf(" ctext:%+v\n", wb.Bytes())