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 (
"fmt"
"net"
)

View File

@ -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

View File

@ -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())