refactor-for-testability (#3)
Co-authored-by: jdl <jdl@desktop> Co-authored-by: jdl <jdl@crumpington.com> Reviewed-on: #3
This commit is contained in:
26
peer/cipher-control.go
Normal file
26
peer/cipher-control.go
Normal file
@@ -0,0 +1,26 @@
|
||||
package peer
|
||||
|
||||
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 header, 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)
|
||||
}
|
||||
Reference in New Issue
Block a user