diff --git a/pkg/util/salt.go b/pkg/util/salt.go index 26a3fe3a4e..b97984e70c 100644 --- a/pkg/util/salt.go +++ b/pkg/util/salt.go @@ -1,8 +1,18 @@ package util +import ( + "io" +) + // SaltXOR xors bits of data with salt // repeating salt if necessary. -func SaltXOR(data, salt []byte) (result []byte) { +func SaltXOR(data, salt []byte) []byte { + return SaltXOROffset(data, salt, 0) +} + +// SaltXOROffset xors bits of data with salt starting from off byte +// repeating salt if necessary. +func SaltXOROffset(data, salt []byte, off int) (result []byte) { result = make([]byte, len(data)) ls := len(salt) if ls == 0 { @@ -11,7 +21,37 @@ func SaltXOR(data, salt []byte) (result []byte) { } for i := range result { - result[i] = data[i] ^ salt[i%ls] + result[i] = data[i] ^ salt[(i+off)%ls] } return } + +type saltWriter struct { + w io.Writer + + off int + + salt []byte +} + +// NewSaltingWriter returns io.Writer instance that applies +// salt to written data and write the result to w. +func NewSaltingWriter(w io.Writer, salt []byte) io.Writer { + if len(salt) == 0 { + return w + } + + return &saltWriter{ + w: w, + salt: salt, + } +} + +func (w *saltWriter) Write(data []byte) (int, error) { + if dataLen := len(data); dataLen > 0 { + data = SaltXOROffset(data, w.salt, w.off) + w.off += dataLen + } + + return w.w.Write(data) +} diff --git a/pkg/util/salt_test.go b/pkg/util/salt_test.go new file mode 100644 index 0000000000..4cee983a85 --- /dev/null +++ b/pkg/util/salt_test.go @@ -0,0 +1,34 @@ +package util_test + +import ( + "bytes" + "crypto/rand" + "testing" + + "github.com/nspcc-dev/neofs-node/pkg/util" + "github.com/stretchr/testify/require" +) + +func randData(sz int) []byte { + data := make([]byte, sz) + + _, _ = rand.Read(data) + + return data +} + +func TestSaltWriter_Write(t *testing.T) { + salt := randData(4) + data := randData(15) + buf := bytes.NewBuffer(nil) + + w := util.NewSaltingWriter(buf, salt) + + _, err := w.Write(data) + require.NoError(t, err) + + require.Equal(t, + buf.Bytes(), + util.SaltXOR(data, salt), + ) +}