mirror of
https://github.com/ScuroNeko/mtg.git
synced 2026-08-31 15:04:03 +03:00
Reuse buffers for stream cipher
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
package wrappers
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"crypto/cipher"
|
||||
"net"
|
||||
|
||||
@@ -28,10 +29,17 @@ func (s *StreamCipher) Read(p []byte) (int, error) {
|
||||
}
|
||||
|
||||
func (s *StreamCipher) Write(p []byte) (int, error) {
|
||||
encrypted := make([]byte, len(p))
|
||||
s.encryptor.XORKeyStream(encrypted, p)
|
||||
buf := streamCipherBufferPool.Get().(*bytes.Buffer)
|
||||
defer streamCipherBufferPool.Put(buf)
|
||||
|
||||
return s.conn.Write(encrypted)
|
||||
buf.Reset()
|
||||
buf.Grow(len(p))
|
||||
buf.Write(p)
|
||||
|
||||
data := buf.Bytes()
|
||||
s.encryptor.XORKeyStream(data, data)
|
||||
|
||||
return s.conn.Write(data)
|
||||
}
|
||||
|
||||
// Logger returns an instance of the logger for this wrapper.
|
||||
|
||||
@@ -0,0 +1,16 @@
|
||||
package wrappers
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"sync"
|
||||
)
|
||||
|
||||
var streamCipherBufferPool sync.Pool
|
||||
|
||||
func init() {
|
||||
streamCipherBufferPool = sync.Pool{
|
||||
New: func() interface{} {
|
||||
return &bytes.Buffer{}
|
||||
},
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user