The fletcher16 source code is given like this on wikipedia.
uint16_t Fletcher16( uint8_t* data, int count )
{
uint16_t sum1 = 0;
uint16_t sum2 = 0;
int index;
for( index = 0; index < count; ++index )
{
sum1 = (sum1 + data[index]) % 255;
sum2 = (sum2 + sum1) % 255;
}
return (sum2 << 8) | sum1;
}
From that example, I implemented the 64 bit version as follows.
unsigned long Fletcher64( unsigned int* data, int count )
{
unsigned long sum1 = 0;
unsigned long sum2 = 0;
int index;
for( index = 0; index < count; ++index )
{
sum1 = (sum1 + data[index]) % UINT_MAX; // UINT_MAX = 2^32
sum2 = (sum2 + sum1) % UINT_MAX;
}
return (sum2 << 32) | sum1;
}
Is my approach correct, or am I doing something wrong?
Most hash algorithms take parameters of a block of memory and the number of bytes (or sometimes bits) to hash. You have changed a byte count to a word count—a rather significant change.
Also, since you have changed the calculation size, the way sum2
is computed has changed. This is fine as long as you are not trying to replicate the original algorithm value-per-value, but if this is meant as a compatible optimization, then this is probably wrong.