Skip to content

proposal: bytes.Xor #30553

Closed
Closed
@kixelated

Description

@kixelated

Variation of #28113 (rejected proposal)

I would like to export the optimized xorBytes implementation in crypto/cipher. Unlike the above ticket, I think it should be a member of the bytes package as bytes.Xor.

Applying a XOR over multiple bytes is commonly used in crypto as well as parity generation. It is was relatively trivial to implement yourself but thanks to a recent contribution, the Go version is now very optimized.

package bytes

// Xor will xor each byte in a and b, writing the result to dst. 
// The destination should have enough space, otherwise Xor will panic.
// Returns the number of bytes written, which will be the minimum of len(a), and len(b).
func Xor(dst, a, b []byte) int

Currently, xorBytes will panic when dst is not large enough. It could follow copy semantics and write up to min(len(dst), len(a), len(b)) bytes. But it seems relatively common to panic in the crypto package when the dst buffer is not large enough, such as XORKeyStream.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions