Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function HashStringSensitive(const S: AnsiString; const Seed: LongWord=$9747b28c): LongWord;
- var
- hash: LongWord;
- len: LongWord;
- k: LongWord;
- data: Integer;
- const
- // 'm' and 'r' are mixing constants generated offline.
- // They're not really 'magic', they just happen to work well.
- m = $5bd1e995;
- r = 24;
- begin
- {$Q-}
- {$R-}
- len := Length(S);
- //The default seed, $9747b28c, is from the original C library
- // Initialize the hash to a 'random' value
- hash := seed xor len;
- // Mix 4 bytes at a time into the hash
- data := 1;
- while(len >= 4) do
- begin
- k := PLongWord(@S[data])^;
- k := k*m;
- k := k xor (k shr r);
- k := k*m;
- hash := hash*m;
- hash := hash xor k;
- data := data+4;
- len := len-4;
- end;
- { Handle the last few bytes of the input array
- S: ... $69 $18 $2f
- }
- Assert(len <= 3);
- if len = 3 then
- hash := hash xor (LongWord(s[data+2]) shl 16);
- if len >= 2 then
- hash := hash xor (LongWord(s[data+1]) shl 8);
- if len >= 1 then
- begin
- hash := hash xor (LongWord(s[data]));
- hash := hash * m;
- end;
- // Do a few final mixes of the hash to ensure the last few
- // bytes are well-incorporated.
- hash := hash xor (hash shr 13);
- hash := hash * m;
- hash := hash xor (hash shr 15);
- Result := hash;
- end;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement