Let's check original C and C# code:
Code:
unsigned jen_hash ( unsigned char *k, unsigned length, unsigned initval )
{
unsigned a, b;
unsigned c = initval;
unsigned len = length;
a = b = 0x9e3779b9;
while ( len >= 12 ) {
a += ( k[0] + ( (unsigned)k[1] << 8 )
+ ( (unsigned)k[2] << 16 )
+ ( (unsigned)k[3] << 24 ) );
b += ( k[4] + ( (unsigned)k[5] << 8 )
+ ( (unsigned)k[6] << 16 )
+ ( (unsigned)k[7] << 24 ) );
c += ( k[8] + ( (unsigned)k[9] << 8 )
+ ( (unsigned)k[10] << 16 )
+ ( (unsigned)k[11] << 24 ) );
mix ( a, b, c ); // this is macro with basic bit math
k += 12;
len -= 12;
}
c += length;
switch ( len ) {
case 11: c += ( (unsigned)k[10] << 24 );
case 10: c += ( (unsigned)k[9] << 16 );
case 9 : c += ( (unsigned)k[8] << 8 );
case 8 : b += ( (unsigned)k[7] << 24 );
case 7 : b += ( (unsigned)k[6] << 16 );
case 6 : b += ( (unsigned)k[5] << 8 );
case 5 : b += k[4];
case 4 : a += ( (unsigned)k[3] << 24 );
case 3 : a += ( (unsigned)k[2] << 16 );
case 2 : a += ( (unsigned)k[1] << 8 );
case 1 : a += k[0];
}
mix ( a, b, c );
return c;
}
Input is string. I made:
Delphi-Quellcode:
while L >= 12
do
begin
Inc(A, (Ord(AValue[K + 1]) + (Ord(AValue[K + 2])
shl 8) + (Ord(AValue[K + 3])
shl 16) + (Ord(AValue[K + 4])
shl 24)));
Inc(B, (Ord(AValue[K + 5]) + (Ord(AValue[K + 6])
shl 8) + (Ord(AValue[K + 7])
shl 16) + (Ord(AValue[K + 8])
shl 24)));
Inc(C, (Ord(AValue[K + 9]) + (Ord(AValue[K + 10])
shl 8) + (Ord(AValue[K + 11])
shl 16) + (Ord(AValue[K + 12])
shl 24)));
Mix(A, B, C);
Inc(K, 12);
Dec(L, 12);
end;
case L
of
11: Inc(C, Ord(AValue[11])
shl 24);
10: Inc(C, Ord(AValue[10])
shl 16);
9: Inc(C, Ord(AValue[9])
shl 8);
And another function:
Code:
public override HashResult ComputeBytes(byte[] a_data)
{
int length = a_data.Length;
if (length == 0)
return new HashResult(0);
uint hash = (UInt32)length;
int currentIndex = 0;
while (length >= 4)
{
hash += (ushort)(a_data[currentIndex++] | a_data[currentIndex++] << 8);
uint tmp = (uint)((uint)(a_data[currentIndex++] | a_data[currentIndex++] << 8) << 11) ^ hash;
hash = (hash << 16) ^ tmp;
hash += hash >> 11;
length -= 4;
}
switch (length)
{
case 3:
hash += (ushort)(a_data[currentIndex++] | a_data[currentIndex++] << 8);
hash ^= hash << 16;
hash ^= ((uint)a_data[currentIndex]) << 18;
hash += hash >> 11;
break;
case 2:
hash += (ushort)(a_data[currentIndex++] | a_data[currentIndex] << 8);
hash ^= hash << 11;
hash += hash >> 17;
break;
case 1:
hash += a_data[currentIndex];
hash ^= hash << 10;
hash += hash >> 1;
break;
default:
break;
}
hash ^= hash << 3;
hash += hash >> 5;
hash ^= hash << 4;
hash += hash >> 17;
hash ^= hash << 25;
hash += hash >> 6;
return new HashResult(hash);
}
I made:
Delphi-Quellcode:
I := 1;
while L >= 4
do
begin
Inc(I);
Result := Result + (Ord(AValue[I])
and Ord(AValue[I + 1])
shl 8);
Result := (Result
shl 16)
xor (((Ord(AValue[I + 2])
and Ord(AValue[I + 3])
shl 8)
shl 11)
xor Result);
Result := Result + (Result
shr 11);
Inc(I, 4);
Dec(L, 4);
end;
case L
of
3:
begin
Result := Result + (Ord(AValue[I])
and Ord(AValue[I + 1])
shl 8);
Result := Result
xor (Result
shl 16);
Result := Result
xor (Ord(AValue[I + 2])
shl 18);
Result := Result + (Result
shr 11);
end;
I did oke or not? Must multiply by 256? I still don't understand why?
BTW: "|" mean
"and" "or"?