如果在PHP中实现hash算法

  • A+
所属分类:PHP

如下代码:

> 24);
$hash &= ~$x;
}
}
return $hash % 701819;
}

function JSHash($str) // 0.23
{
$hash = 0;
$n = strlen($str);
for ($i = 0; $i <$n; $i++) { $hash ^= (($hash <<5) + ord($str[$i]) + ($hash>> 2));
}
return $hash % 701819;
}

function SDBMHash($str) // 0.23
{
$hash = 0 ;
$n = strlen($str);
for ($i = 0; $i <$n; $i++) { $hash = ord($str[$i]) + ($hash <<6 ) + ($hash <<16 ) - $hash; } return $hash % 701819; } function APHash($str) // 0.30 { $hash = 0 ; $n = strlen($str); for ($i = 0; $i <$n; $i++) { if (($i & 1 ) == 0 ) { $hash ^= (($hash <<7 ) ^ ord($str[$i]) ^ ($hash>> 3 ));
}
else
{
$hash ^= ( ~ (($hash <<11 ) ^ ord($str[$i]) ^ ($hash>> 5)));
}
}
return $hash % 701819;
}

function DEKHash($str) // 0.23
{
$n = strlen($str);
$hash = $n;
for ($i = 0; $i <$n; $i++) { $hash = (($hash <<5) ^ ($hash>> 27)) ^ ord($str[$i]);
}
return $hash % 701819;
}

function FNVHash($str) // 0.31
{
$hash = 0;
$n = strlen($str);
for ($i = 0; $i <$n; $i++) { $hash *= 0x811C9DC5; $hash ^= ord($str[$i]); } return $hash % 701819; } function PJWHash($str) // 0.33 { $hash = $test = 0; $n = strlen($str); for ($i = 0; $i <$n; $i++) { $hash = ($hash <<4) + ord($str[$i]); if(($test = $hash & -268435456) != 0) { $hash = (( $hash ^ ($test>> 24)) & (~-268435456));
}
}
return $hash % 701819;
}

function PHPHash($str) // 0.34
{
$hash = 0;
$n = strlen($str);
for ($i = 0; $i <$n; $i++) { $hash = ($hash <<4) + ord($str[$i]); if (($g = ($hash & 0xF0000000))) { $hash = $hash ^ ($g>> 24);
$hash = $hash ^ $g;
}
}
return $hash % 701819;
}

function OpenSSLHash($str) // 0.22
{
$hash = 0;
$n = strlen($str);
for ($i = 0; $i <$n; $i++) { $hash ^= (ord($str[$i]) <<($i & 0x0f)); } return $hash % 701819; } function MD5Hash($str) // 0.050 { $hash = md5($str); $hash = $hash[0] | ($hash[1] <<8 ) | ($hash[2] <<16) | ($hash[3] <<24) | ($hash[4] <<32) | ($hash[5] <<40) | ($hash[6] <<48) | ($hash[7] <<56); return $hash % 701819; } ?>

原作者说md5hash的效率最高。

avatar

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

目前评论:5   其中:访客  5   博主  0

    • avatar hypnosis downloads 0

      Hi there, – discovered your current webpage by chance when wandering around the internet this morning, and happy that I did! I do like the structure and colors, but I need to point out that I’m having difficulties any time it loads. I’m using Flock 2 web browser, and the header fails to line up the right way. Seems OK in Shiira 2 though.

      • avatar hypnosis downloads 0

        I’m wondering if we are able to discuss your site’s figures – amount of searches, etc, as I am just hoping to contact internet websites I can buy adspace with – let me know if we can speak about rates and other bits and pieces. Take care, you happen to be carrying out a terrific job.

        • avatar gaz obronny 0

          Extremely superior content. We basically discovered a site not to mention desired towards mention who We have definitely liked looking through a website not to mention blogposts. Anyways I’ll turn out to be following a give food not to mention I just desire towards study a website yet again.

          • avatar Loren Corping 0

            Rather good posting. I just stumbled upon the webpage and also wanted to be able to point out in which I have actually enjoyed analyzing the blog page and also content. Anyway I’ll become subscribing the nourish and also My partner and i expect to be able to read the blog page all over again.

            • avatar Lamonica Pagdanganan 0

              When i seriously prefer whatever you post in this article. Incredibly useful in addition to smart. Just one challenge though. I’m running Internet explorer having Debian in addition to elements of this present page layout portions undoubtedly are a very little wonky. When i understand it’s an excellent common established. Yet still it’s something to help retain in view. When i trust so it can guide in addition to always keep in the very best excellent publishing.