I need a quick checksum (as fast as possilbe) for small strings (20-500 chars).
I need the source code and that must be small! (about 100 LOC max)
If it could generate strings in Base32/64. (or something similar) it would be perfect. Basically the checksums cannot use any "bad" chars.. you know.. the usual (){}[].,;:/+-\| etc
Clarifications
It could be strong/weak, that really doesn't matter since it is only for behind-the-scenes purposes.
It need not contain all the data of the original string since I will be only doing comparison with generated checksums, I don't expect any sort of "decryption".
schnaader's implementation is indeed very fast. Here it is in Javascript:
Using Google Chrome, this function takes just 5ms to run for 1-megabyte strings, versus 330ms using a crc32 function.
Here's a Javascript implementation of CRC32:
Found at kevin.vanzonneveld.net
What you are looking for is an algorithm to create a hash code for a string. In C#:
Quick implementation in C, no copyrights from my side, so use it as you wish. But please note that this is a very weak "checksum", so don't use it for serious things :) - but that's what you wanted, isn't it?
This returns an 32-bit integer checksum encoded as an string containing its hex value. If the checksum function doesn't satisfy your needs, you can change the
chk += ((int)(str[i]) * (i + 1));
line to something better (f.e. multiplication, addition and bitwise rotating would be much better).EDIT: Following hughdbrown's advice and one of the answers he linked, I changed the
for
loop so it doesn't callstrlen
with every iteration.Pretty much any algorithm you could come up with would satisfy your criteria. E.g.
to make it "bad-char-safe"
An attempt that tries to reduce the number of collisions by increasing the output domain
A solution that further reduces the number of collisions by calculating different values for different permuations
In general, all these variations perform pretty well if the input is random enough and sparse enough (of course, "enough" differs in each case)
This may seem a bit late and partly off topic... I used joelpt's JS-implementation of schnaader's solution in an application based on JS and PHP. And, I assume that the PHP implementation might be helpful for others, as well.