Nim 中的 Cryptopals 1.2:XOR returns 一个意外的值

Cryptopals 1.2 in Nim: XOR returns an unexpected value

我认为在 Nim 中做 Cryptopals 练习是学习这门语言的好方法。在 set 1 exercise 2 上,我在 xor 部分得到了意外结果。练习:

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

Python solution 很明显。我一直在做 Node.js 的练习,这也很简单。

我在 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)

输出是:

这完全不是我所期望的。我是否错误地解析了十六进制 and/or 二进制?

异或运算的第二个参数的输入和输入都应该使用parseHexStr。然后你可以分别对每个字节进行异或并将结果存储在一个新数组中。

Nim 整数与 Nim 指针大小相同。您正在解析两个十六进制的大字节数组,而结果是一个较小的整数。或者,更具体地说,您得到的输入值和结果是 模 2 的 64 次幂,因为您可能正在使用 64 位 CPU。