Skip to content

Commit

Permalink
Improved writing of large byte slice
Browse files Browse the repository at this point in the history
  • Loading branch information
riobard committed Sep 22, 2018
1 parent 9979384 commit c25b1d1
Showing 1 changed file with 6 additions and 6 deletions.
12 changes: 6 additions & 6 deletions shadowaead/stream.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,18 +32,18 @@ func (w *Writer) Write(p []byte) (n int, err error) {
nonce := w.nonce[:w.NonceSize()]
tag := w.Overhead()
off := 2 + tag
for nr := 0; n < len(p) && err == nil; n += nr {
nr = payloadSizeMask
if n+nr > len(p) {
nr = len(p) - n
for nr := payloadSizeMask; n < len(p); n += nr { // write piecemeal in max payload size chunks
if tail := len(p) - n; tail < payloadSizeMask {
nr = tail
}
buf = buf[:off+nr+tag]
buf[0], buf[1] = byte(nr>>8), byte(nr) // big-endian payload size
w.Seal(buf[:0], nonce, buf[:2], nil)
increment(nonce)
w.Seal(buf[:off], nonce, p[n:n+nr], nil)
increment(nonce)
_, err = w.Writer.Write(buf)
if _, err = w.Writer.Write(buf[:off+nr+tag]); err != nil {
return
}
}
return
}
Expand Down

0 comments on commit c25b1d1

Please sign in to comment.