Make encryption a bit more efficient

* don't copy chunksize but encode in-place
* reduce stack allocation of chunksize+seq buffer and reuse output
  buffer
This commit is contained in:
Sudhi Herle 2020-03-24 11:39:04 -07:00
parent e22fae05f7
commit 3fa0ce0c9c
2 changed files with 2 additions and 3 deletions

View file

@ -295,7 +295,6 @@ func fullwrite(buf []byte, wr io.Writer) error {
// modification attacks. The encoded length & block number is used as // modification attacks. The encoded length & block number is used as
// additional data in the AEAD construction. // additional data in the AEAD construction.
func (e *Encryptor) encrypt(buf []byte, wr io.Writer, i uint32, eof bool) error { func (e *Encryptor) encrypt(buf []byte, wr io.Writer, i uint32, eof bool) error {
var b [8]byte
var nonceb [32]byte var nonceb [32]byte
var z uint32 = uint32(len(buf)) var z uint32 = uint32(len(buf))
@ -304,6 +303,7 @@ func (e *Encryptor) encrypt(buf []byte, wr io.Writer, i uint32, eof bool) error
z |= _EOF z |= _EOF
} }
b := e.buf[:8]
binary.BigEndian.PutUint32(b[:4], z) binary.BigEndian.PutUint32(b[:4], z)
binary.BigEndian.PutUint32(b[4:], i) binary.BigEndian.PutUint32(b[4:], i)
@ -312,7 +312,6 @@ func (e *Encryptor) encrypt(buf []byte, wr io.Writer, i uint32, eof bool) error
h.Write(b[:]) h.Write(b[:])
nonce := h.Sum(nonceb[:0])[:e.ae.NonceSize()] nonce := h.Sum(nonceb[:0])[:e.ae.NonceSize()]
copy(e.buf[:4], b[:4])
cbuf := e.buf[4:] cbuf := e.buf[4:]
c := e.ae.Seal(cbuf[:0], nonce, buf, b[:]) c := e.ae.Seal(cbuf[:0], nonce, buf, b[:])

View file

@ -1 +1 @@
1.1.0 1.1.1