没有机会以64位进行安全散列.即使是160位的SHA-1也被认为在理论上被破坏了.如果您真的关心安全数字签名,则应使用SHA2-256.如果您不关心安全性并且只想要一个避免非对抗性冲突的哈希函数,那么只需使用以下内容即可:
constexpr uint64 P1 = 7;
constexpr uint64 P2 = 31;
uint64 hash = P1;
for (const char* p = s; *p != 0; p++) {
hash = hash * P2 + *p;
}
Run Code Online (Sandbox Code Playgroud)
正如 AndrewTomazos-Fathomling 所说,不可能以 64 位进行安全哈希,因此,如果这是您的意图,那么我的建议是停止,拿起一本书并阅读有关加密安全哈希的内容。
如果您不打算将其用作安全哈希,并且您不关心冲突或攻击,那么他给您的答案就很好,您可以根据需要调整素数 P1 和 P2。我将为您提供另一种选择,它允许您进行标记散列并将更多内容混合在一起。
// Disclaimer: I make no claims about the quality of this particular hash - it's
// certainly not a cryptographically secure hash, nor should it *ever* be
// construed as such.
unsigned long long quickhash64(const char *str, unsigned long long mix = 0)
{ // set 'mix' to some value other than zero if you want a tagged hash
const unsigned long long mulp = 2654435789;
mix ^= 104395301;
while(*str)
mix += (*str++ * mulp) ^ (mix >> 23);
return mix ^ (mix << 37);
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
10454 次 |
最近记录: |