package node import "golang.org/x/crypto/nacl/box" type controlCipher struct { sharedKey [32]byte } func newControlCipher(privKey, pubKey []byte) *controlCipher { shared := [32]byte{} box.Precompute(&shared, (*[32]byte)(pubKey), (*[32]byte)(privKey)) return &controlCipher{shared} } func (cc *controlCipher) Encrypt(h xHeader, data, out []byte) []byte { const s = controlHeaderSize out = out[:s+controlCipherOverhead+len(data)] h.Marshal(out[:s]) box.SealAfterPrecomputation(out[s:s], data, (*[24]byte)(out[:s]), &cc.sharedKey) return out } func (cc *controlCipher) Decrypt(encrypted, out []byte) (data []byte, ok bool) { const s = controlHeaderSize return box.OpenAfterPrecomputation(out[:0], encrypted[s:], (*[24]byte)(encrypted[:s]), &cc.sharedKey) }