hash() public method

8 hexits = 32bit, which also allows us to forego having to check whether it's over PHP_INT_MAX. The substring is converted to an int since hex strings sometimes get treated as ints if all digits are ints and this results in unexpected sorting order.
Author: Dom Morgan (dom@d3r.com)
public hash ( string $string ) : integer
$string string
return integer
Beispiel #1
0
 public function testMd5Hash()
 {
     $hasher = new Md5Hasher();
     $result1 = $hasher->hash('test');
     $result2 = $hasher->hash('test');
     $result3 = $hasher->hash('different');
     $this->assertEquals($result1, $result2);
     $this->assertNotEquals($result1, $result3);
     // fragile but worthwhile
 }
Beispiel #2
0
 public function testHasherSpeed()
 {
     $hashCount = 100000;
     $md5Hasher = new Md5Hasher();
     $crc32Hasher = new Crc32Hasher();
     $start = microtime(true);
     for ($i = 0; $i < $hashCount; ++$i) {
         $md5Hasher->hash("test{$i}");
     }
     $timeMd5 = microtime(true) - $start;
     $start = microtime(true);
     for ($i = 0; $i < $hashCount; ++$i) {
         $crc32Hasher->hash("test{$i}");
     }
     $timeCrc32 = microtime(true) - $start;
     $this->dump(sprintf('Hashers timed over %d hashes (MD5 / CRC32): %f / %f', $hashCount, $timeMd5, $timeCrc32));
 }