Advertisement
Guest User

Hash Algo

a guest
May 4th, 2015
35
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pascal 1.44 KB | None | 0 0
  1. function HashStringSensitive(const S: AnsiString; const Seed: LongWord=$9747b28c): LongWord;
  2. var
  3.     hash: LongWord;
  4.     len: LongWord;
  5.     k: LongWord;
  6.     data: Integer;
  7. const
  8.     // 'm' and 'r' are mixing constants generated offline.
  9.     // They're not really 'magic', they just happen to work well.
  10.     m = $5bd1e995;
  11.     r = 24;
  12. begin
  13.   {$Q-}
  14.   {$R-}
  15.     len := Length(S);
  16.  
  17.     //The default seed, $9747b28c, is from the original C library
  18.  
  19.     // Initialize the hash to a 'random' value
  20.     hash := seed xor len;
  21.  
  22.     // Mix 4 bytes at a time into the hash
  23.     data := 1;
  24.  
  25.     while(len >= 4) do
  26.     begin
  27.         k := PLongWord(@S[data])^;
  28.  
  29.         k := k*m;
  30.         k := k xor (k shr r);
  31.         k := k*m;
  32.  
  33.         hash := hash*m;
  34.         hash := hash xor k;
  35.  
  36.         data := data+4;
  37.         len := len-4;
  38.     end;
  39.  
  40.     {   Handle the last few bytes of the input array
  41.             S: ... $69 $18 $2f
  42.     }
  43.     Assert(len <= 3);
  44.     if len = 3 then
  45.         hash := hash xor (LongWord(s[data+2]) shl 16);
  46.     if len >= 2 then
  47.         hash := hash xor (LongWord(s[data+1]) shl 8);
  48.     if len >= 1 then
  49.     begin
  50.         hash := hash xor (LongWord(s[data]));
  51.         hash := hash * m;
  52.     end;
  53.  
  54.     // Do a few final mixes of the hash to ensure the last few
  55.     // bytes are well-incorporated.
  56.     hash := hash xor (hash shr 13);
  57.     hash := hash * m;
  58.     hash := hash xor (hash shr 15);
  59.  
  60.     Result := hash;
  61. end;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement