【发布时间】:2016-01-16 02:13:55
【问题描述】:
如何避免严格的别名规则违规,尝试修改char* sha256 函数的结果。
计算哈希值:
std::string sha = sha256("some text");
const char* sha_result = sha.c_str();
unsigned long* mod_args = reinterpret_cast<unsigned long*>(sha_result);
比获得 2 块 64 位:
unsigned long a = mod_args[1] ^ mod_args[3] ^ mod_args[5] ^ mod_args[7];
unsigned long b = mod_args[0] ^ mod_args[2] ^ mod_args[4] ^ mod_args[6];
比通过 concat 得到结果:
unsigned long long result = (((unsigned long long)a) << 32) | b;
【问题讨论】:
-
我真的希望您计算哈希的实际字符串比您显示的要长,否则在计算
a和b时,您的索引将超出范围。 -
@Joachim Pileborg:sha256 必须始终返回 32 字节的哈希值。
-
这仍然不意味着你可以超出输入数据的范围,你需要填充它。
-
@Joachim Pileborg:我该怎么做?
标签: c++ strict-aliasing