Skip to content

Commit

Permalink
Merge pull request #15 from libp2p/feat/update-msgio
Browse files Browse the repository at this point in the history
update msgio
  • Loading branch information
Stebalien authored Nov 20, 2017
2 parents b2e8111 + 9e983da commit 30a698d
Showing 1 changed file with 5 additions and 7 deletions.
12 changes: 5 additions & 7 deletions p2p/net/pnet/psk_conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,14 @@ import (
"io"

salsa20 "github.com/davidlazar/go-crypto/salsa20"
mpool "github.com/jbenet/go-msgio/mpool"
ipnet "github.com/libp2p/go-libp2p-interface-pnet"
tconn "github.com/libp2p/go-libp2p-transport"
mpool "github.com/libp2p/go-msgio/mpool"
)

// we are using buffer pool as user needs their slice back
// so we can't do XOR cripter in place
var (
bufPool = mpool.ByteSlicePool

errShortNonce = ipnet.NewError("could not read full nonce")
errInsecureNil = ipnet.NewError("insecure is nil")
errPSKNil = ipnet.NewError("pre-shread key is nil")
Expand All @@ -40,8 +38,8 @@ func (c *pskConn) Read(out []byte) (int, error) {
}

maxn := uint32(len(out))
in := bufPool.Get(maxn).([]byte) // get buffer
defer bufPool.Put(maxn, in) // put the buffer back
in := mpool.ByteSlicePool.Get(maxn).([]byte) // get buffer
defer mpool.ByteSlicePool.Put(maxn, in) // put the buffer back

in = in[:maxn] // truncate to required length
n, err := c.Conn.Read(in) // read to in
Expand Down Expand Up @@ -69,8 +67,8 @@ func (c *pskConn) Write(in []byte) (int, error) {
c.writeS20 = salsa20.New(c.psk, nonce)
}
n := uint32(len(in))
out := bufPool.Get(n).([]byte) // get buffer
defer bufPool.Put(n, out) // put the buffer back
out := mpool.ByteSlicePool.Get(n).([]byte) // get buffer
defer mpool.ByteSlicePool.Put(n, out) // put the buffer back

out = out[:n] // truncate to required length
c.writeS20.XORKeyStream(out, in) // encrypt
Expand Down

0 comments on commit 30a698d

Please sign in to comment.