问题描述
我需要从一个可变长度的字符串中提取一个8字节的摘要,所以我在寻找这样一个算法,我将在c / c ++实现。这将是微控制器上的数字签名过程的一部分,因此它必须是:
I need to extract an 8 byte digest from a variable length string so I'm looking for such an algorithm that I will implement in c/c++. That will be part of a digital signature procedure on a microcontroller, so it has to be:
- 在几行代码中可写,固件必须保持尽可能小;
- 资源消耗低,特别是RAM(最好小于100字节);
我看了一下现有的算法,比如crc64,但是
I took a look at existing algorithms such as crc64 but they seems to be too heavy for my platform.
推荐答案
正如AndrewTomazos-Fathomling所说的,64位的安全哈希是不可能的位,所以如果这是你的意图,那么我的建议是停止,拿起一本书,阅读关于加密安全散列。
As AndrewTomazos-Fathomling said, it's impossible to do a secure hash in 64 bits, so if that's your intention then my advice is STOP, pick up a book and read about cryptographically secure hashing.
如果你不打算使用这个安全哈希,你不关心碰撞或攻击,那么他给你的答案很好,你可以根据需要调整素数P1和P2。我会给你另一个选择,允许你做标记哈希和混合的东西更多。
If you don't plan on using this as a secure hash and you do not care about collisions or attacks, then the answer he gave you works just fine and you can tweak the primes P1 and P2 as necessary. I will give you another alternative which allows you to do tagged hashing and mixes things up more.
// 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);
}
这篇关于轻量级8字节散列函数算法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!