Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Lightweight 8 byte hash function algorithm

Tags:

c++

c

hash

digest

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:

  • writable in few lines of code, since the firmware has to be kept as little as possible;
  • low in resource consumption, expecially ram (preferably less than 100 bytes);
  • strong enough that changing a single character at any point of the string would change the overall digest.

I took a look at existing algorithms such as crc64 but they seems to be too heavy for my platform.

like image 222
etuardu Avatar asked Nov 10 '12 19:11

etuardu


People also ask

How many hashes are in 8 bit output?

#3 If you have an 8 bit hash output, how many possible hashes are there? There are 28 possibles hashes.

What is the fastest hashing algorithm?

SHA-1 is fastest hashing function with ~587.9 ms per 1M operations for short strings and 881.7 ms per 1M for longer strings. MD5 is 7.6% slower than SHA-1 for short strings and 1.3% for longer strings. SHA-256 is 15.5% slower than SHA-1 for short strings and 23.4% for longer strings.

What is the block size of SHA-512 algorithm?

The _update() functions for SHA-256 and SHA-512 are different and, even more importantly, operate on different block sizes: 64 bytes for SHA-256 and 128 bytes for SHA-512.

Which hash algorithm is best?

SHA-256: This hashing algorithm is a variant of the SHA2 hashing algorithm, recommended and approved by the National Institute of Standards and Technology (NIST). It generates a 256-bit hash value. Even if it's 30% slower than the previous algorithms, it's more complicated, thus, it's more secure.

What are the types of hashing algorithm?

Types of Hashing There are many different types of hash algorithms such as RipeMD, Tiger, xxhash and more, but the most common type of hashing used for file integrity checks are MD5, SHA-2 and CRC32. MD5 - An MD5 hash function encodes a string of information and encodes it into a 128-bit fingerprint.


2 Answers

There is no chance to do a secure hash in 64 bits. Even SHA-1 at 160 bit is considered theoretically broken. You should use SHA2-256 if you really care about secure digital signing. If you don't care about security and just want a hash function that avoids non-adversarial collisions just use the following, it is fine:

constexpr uint64 P1 = 7;
constexpr uint64 P2 = 31;

uint64 hash = P1;
for (const char* p = s; *p != 0; p++) {
    hash = hash * P2 + *p;
}
like image 108
Andrew Tomazos Avatar answered Sep 18 '22 08:09

Andrew Tomazos


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.

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);
}
like image 43
Nik Bougalis Avatar answered Sep 18 '22 08:09

Nik Bougalis