mirror of
https://github.com/ScuroNeko/mtg.git
synced 2026-08-31 21:34:02 +03:00
Reuse buffers for stream cipher
This commit is contained in:
@@ -1,6 +1,7 @@
|
|||||||
package wrappers
|
package wrappers
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
"crypto/cipher"
|
"crypto/cipher"
|
||||||
"net"
|
"net"
|
||||||
|
|
||||||
@@ -28,10 +29,17 @@ func (s *StreamCipher) Read(p []byte) (int, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (s *StreamCipher) Write(p []byte) (int, error) {
|
func (s *StreamCipher) Write(p []byte) (int, error) {
|
||||||
encrypted := make([]byte, len(p))
|
buf := streamCipherBufferPool.Get().(*bytes.Buffer)
|
||||||
s.encryptor.XORKeyStream(encrypted, p)
|
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.
|
// 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