XOREncryption icon indicating copy to clipboard operation
XOREncryption copied to clipboard

GoLang XOR function works heavily

Open sting8k opened this issue 5 years ago • 0 comments

Source code: https://github.com/KyleBanks/XOREncryption/blob/master/Go/xor.go

The string concat operator output += will make this XOR function run heavily. I tested input with ~500k characters and it took about 22s to complete. Here is my suggestion:

func Xor(input, key string) (output string) {
	var tmp []string

	for i := 0; i < len(input); i++ {
		tmp = append(tmp, string(input[i]^key[i%len(key)]))
	}
	output = strings.Join(tmp, "")
	return output
}

By doing this, input with ~500k characters took ~0.1s to complete. Much better.

sting8k avatar Sep 30 '20 03:09 sting8k