Best Hash Algorithm for Cache purposes?

Viewed 1125

I need to implement a cache mechanism for my server where I will read the contents (bytes) of static resources and provide the clients with a Hash of them. This way, I want to manually cache the files if the Hash didn't change (even after version updates).

I was investigating SHA, MD5, etc but these hashes are designed to be expensive. I don't need a secure cryptographic hash, I just need a fast and deterministic hash that has small collisions.

Any idea of a hash algorithm that fits this need?

1 Answers

I think you do want a secure hash.

Secure hashes are designed such that any 2 distinct files should have a distinct hash. Collisions in MD5 and SHA1 are only produced by having 2 similar files, which are modified together to produce twins.

CRC by contrast is possible to compute simple changes in a file to come up with the any arbitrary CRC for a file, with only a few (4 or 8 I think) bytes being changed.

So a secure hash would allow you to see different files with almost 100% certainty. When last asked this question (at work), I suggested CRC for a hash, and found that CRC32 was slower than MD5.

This was due to the fact that MD5 round did operations on a wider block of data than CRC32, and was effectively faster.

If you want guaranteed difference finding, use a secure hash. Consider ignoring the broken hashes (MD5,MD4,MD3, MD2, SHA1, ...) as it could impact data integrity. Actually test performance to see the throughput on your hardware, as some CPUs have help instructions to speed HASH algorithms up.

Related