XORing strings in Go

4,939 views
Skip to first unread message

Node-Raptor

unread,
Nov 10, 2012, 1:44:57 PM11/10/12
to golan...@googlegroups.com
Dear go-nuts group,

I'd like to XOR two strings and get the result per byte as hexadecimal output.
However, I already fail at XORing these.
I've already tried XORing the strings as such and as hexadecimal values.
Then I tried using binary.Write() to write the strings to two buffers and XOR the results when reading byte-per-byte, however, this also doesn't work.

Any help is appreciated, thanks in advance.

Evan Shaw

unread,
Nov 10, 2012, 1:49:30 PM11/10/12
to Node-Raptor, golang-nuts
On Sun, Nov 11, 2012 at 7:44 AM, Node-Raptor
<jan.toe...@node-raptor.com> wrote:
> Dear go-nuts group,
>
> I'd like to XOR two strings and get the result per byte as hexadecimal
> output.
> However, I already fail at XORing these.
> I've already tried XORing the strings as such and as hexadecimal values.
> Then I tried using binary.Write() to write the strings to two buffers and
> XOR the results when reading byte-per-byte, however, this also doesn't work.

Try this: http://play.golang.org/p/kPmk_r9MUl

Let us know if you have any questions about the code.

- Evan

Node-Raptor

unread,
Nov 10, 2012, 1:53:49 PM11/10/12
to golan...@googlegroups.com, Node-Raptor
Thanks you!
So apparently XORing strings DID work, I would've just needed to save the results in bytes.

Node-Raptor

unread,
Nov 10, 2012, 2:12:43 PM11/10/12
to golan...@googlegroups.com, Node-Raptor
Actually, I do have one more question now.
The result has double the length of the two inputs, which is unfortunate, as I will need to XOR another string with it.
Doing that results in the last half being len(b)/2 * 0.
I've used a simple
for i := 0; i < len(string3); i++ {
b2[i] = b[i] ^ string3[i]
}
for that.

P.S.: Thanks you! -> Thank you! in the previous mail, of course. ^^"

Evan Shaw

unread,
Nov 10, 2012, 2:33:35 PM11/10/12
to Node-Raptor, golang-nuts
On Sun, Nov 11, 2012 at 8:12 AM, Node-Raptor
<jan.toe...@node-raptor.com> wrote:
> Actually, I do have one more question now.
> The result has double the length of the two inputs, which is unfortunate, as
> I will need to XOR another string with it.

I'm not sure I understand. In my code above:

len(b) == len(string1) == len(string2)

If you're talking about the hex encoded string, it will always have a
length of 2*len(b).

- Evan

Node-Raptor

unread,
Nov 10, 2012, 2:45:26 PM11/10/12
to golan...@googlegroups.com, Node-Raptor
What I mean is that - when XORing two hexadecimal strings - b ends up being twice the length of the hex-representations of the strings, like this.
This prevents me from XORing it with another a string of the same length as string1 and string2.

Dustin Sallings

unread,
Nov 10, 2012, 2:46:34 PM11/10/12
to golan...@googlegroups.com
Node-Raptor <jan.toe...@node-raptor.com>
writes:
The error message was telling you that the function you were calling
returned two arguments and you were trying to pretend like it returned
one. You were really going about it the long way, though.

This seems a lot easier:

http://play.golang.org/p/lMg_Hrv2g-

--
dustin

Dustin Sallings

unread,
Nov 10, 2012, 3:00:09 PM11/10/12
to golan...@googlegroups.com
Node-Raptor <jan.toe...@node-raptor.com>
writes:

> Actually, I do have one more question now.
> The result has double the length of the two inputs, which is
> unfortunate, as I will need to XOR another string with it.
> Doing that results in the last half being len(b)/2 * 0.
> I've used a simple
> for i := 0; i < len(string3); i++ {
> b2[i] = b[i] ^ string3[i]
> }
> for that.

hex encoding doubles the length, but xoring doesn't. You can just xor
them all together at the same time either by doing something like this:

result := a ^ b ^ c

Or just make the function take an arbitrary number of []bytes to xor:

http://play.golang.org/p/mc0UicCXmV

--
dustin

Node-Raptor

unread,
Nov 10, 2012, 4:34:57 PM11/10/12
to golan...@googlegroups.com
Thank you, that method is pretty useful and refreshed the range-method of iterating I got to know in the Go tour.
Overall, the problem I had was mostly in my mind, so now that I got it, I can write it out:
When XORing hexadecimal representations of a string, the result of that is - despite being of type []byte - still the correct hexadecimal XOR and converting it to a string is fully sufficient. 
Calling hex.EncodeToString however is not appropiate then, which wasn't obvious to me until now.
Thanks to you two for helping me. ;)

Dustin Sallings

unread,
Nov 10, 2012, 6:02:25 PM11/10/12
to golan...@googlegroups.com
Node-Raptor <jan.toe...@node-raptor.com>
writes:

> Calling hex.EncodeToString however is not appropiate then, which wasn't
> obvious to me until now.
> Thanks to you two for helping me. ;)

That was just "how to make a hex string" which was the final step as I
understood it. You can also use %x in fmt functions (which makes sense,
but I didn't know before this thread -- why I read the list).

--
dustin

Sonia Keys

unread,
Nov 10, 2012, 6:50:35 PM11/10/12
to golan...@googlegroups.com
Also consider using big.Ints.  http://play.golang.org/p/_deCL4KVKM

bryon...@gmail.com

unread,
Jan 1, 2014, 5:05:37 PM1/1/14
to golan...@googlegroups.com
I tweaked this to be a pure variadic function insofar as it no longer requires a 'b1' and a 'bmore'.  This makes it nice for when you may have a set of slices to all be XOR'd, they can then be collected in a meta-slice and passed as the single '...'ed parameter to the variadic function. 

Extra examples are also included.  Thanks for a great base example to build on!

Reply all
Reply to author
Forward
0 new messages