I thought doing the Cryptopals exercises in Nim would be a nice way to learn the language. On set 1 exercise 2, I'm getting an unexpected result on the xor
part. The exercise:
If your function works properly, then when you feed it the string:
1c0111001f010100061a024b53535009181c
... after hex decoding, and when XOR'd against:
686974207468652062756c6c277320657965
... should produce:
746865206b696420646f6e277420706c6179
The Python solution is very obvious. I've been doing the exercises in Node.js and it's a no-brainer as well.
My solution in Nim:
import strutils as strutils
var a, b = 0
a = toU16(strutils.parseHexInt("1c0111001f010100061a024b53535009181c"))
b = toU16(strutils.parseHexInt("686974207468652062756c6c277320657965"))
let result = toHex(a xor b)
The outputs are:
- a: 165317428619319324
- b: 7812662828999211365
- (a xor b): 7937440550937715065
- hex(a xor b): 0000000000006179
This is not at all what I'd expect. Am I parsing hexadecimals and/or binary incorrectly?