当我使用这些代码手动检查多达 3-4 次迭代时,获得了正确的结果。但是当我使用蒙特卡洛测试向量时,结果是错误的。
#include "HASH.hpp"
string HexEncode(const string &data)
{
string result;
StringSource(data, true, new HexEncoder(new StringSink(result)));
return result;
}
string HexDecode(const string &data)
{
string result;
StringSource(data, true, new HexDecoder(new StringSink(result)));
return result;
}
template <class T>
string hashBase(string input, int iteration)
{
T hash;
string result;
for (size_t i = 0; i < iteration; i++)
{
hash.Update((const unsigned char *)input.data(), input.size());
result.resize(hash.DigestSize());
hash.Final((unsigned char *)&result[0]);
input = result;
}
return HexEncode(result);
}
string sha2_256(string input, int iteration)
{
return hashBase<SHA256>(input, iteration);
}
对于这个测试,结果应该是
45f72e1ab055e54f5c26aebe73bb7f4ec8f795c9d4f94e3f258ed130ab3d3347
。但是结果是7FEE6FC25FBBD67A3D513A0749A6CD45A689F0934BB21A30F59CCBA3F98799D2
.
int main(int argc, char *argv[])
{
int iteration = 99;
string input = "6cd4c0c5cb2ca2a0f1d1aecebac03b52e64ea03d1a1654372936545b92bbc5484a59db74bb60f9c40ceb1a5aa35a6fafe80349e14c253a4e8b1d77612ddd81ace926ae8b0af6e53176dbffcc2a6b88c6bd765f939d3d178a9bde9ef3aa131c61e31c1e42cdfaf4b4dcde579a37e150efbef5555b4c1cb40439d835a724e2fae7";
string result = sha2_256(input, iteration);
cout << "Iteration: " << iteration << endl;
cout << "Input: " << input << endl;
cout << "Result: " << result << endl;
return 0;
}