Delicious Bookmark this on Delicious Share on Facebook SlashdotSlashdot It! Digg! Digg



PHP : Function Reference : hash Functions : hash

hash

Generate a hash value (message digest) (PHP 5 >= 5.1.2, PECL hash:1.1-1.5)
string hash ( string algo, string data [, bool raw_output] )

Example 872. A hash() example

<?php
echo hash('ripemd160', 'The quick brown fox jumped over the lazy dog.');
?>

The above example will output:

ec457d0a974c48d5685a7efa03d137dc8bbde7e3

Related Examples ( Source code ) » hash


Code Examples / Notes » hash

just me

The speed difference (as noted in a below posting) between md5() and hash() goes down to zero with strings longer than just a few bytes. With a string length of 1kB the difference is 10% advantage for hash() and shrinks further down to 3% with 10kB strings.

peter kelly

If the hash functions are not available to you at the moment, and you want to future proof your code, then the code below will emulate two of the important hashing functions, they will also be automatically replaced with the faster versions if available.
if(!function_exists('hash_algos'))
{
function hash_algos()
{
$algo[0] = "md5";
$algo[1] = "sha1";
$algo[2] = "crc32";
return($algo);
}
}
if(!function_exists('hash'))
{
function hash($algo, $data, $raw_output = 0)
{
if($algo == 'md5') return(md5($data));
if($algo == 'sha1') return(sha1($data));
if($algo == 'crc32') return(crc32($data));
}
}


dani88elx

Comparison between hash algorithms.
<?
$algos = hash_algos();
$word="hola";
foreach($algos as $algo)
{
echo $algo.": ";
$time=microtime(1);
echo hash($algo, $word);
echo "
".(microtime(1)-$time)."
<hr>";
}
?>


mysteryboy

but hash a big file ,
example:23KB:
hash
1.2369749546051: md4
1.5122809410095: md5
7.1646420955658: adler32
3.1666488647461: crc32
0.018635988235474: crc32b
2.4434490203857: sha1
=========>>
1.5402789115906: md5
2.4519650936127: sha1


mikael

<?php
$time=microtime(1);
for ($i=0;$i<100000;$i++)
  hash('md5', 'string');
echo microtime(1)-$time,': hash/md5
';
$time=microtime(1);
for ($i=0;$i<100000;$i++)
  md5('string');
echo microtime(1)-$time,': md5
';
$time=microtime(1);
for ($i=0;$i<100000;$i++)
  hash('sha1', 'string');
echo microtime(1)-$time,': hash/sha1
';
$time=microtime(1);
for ($i=0;$i<100000;$i++)
  sha1('string');
echo microtime(1)-$time,': sha1
';
?>------------------------
<?php
$time=microtime(1);
for ($i=0;$i<100000;$i++)
  hash('md5', $i);
echo microtime(1)-$time,': hash/md5
';
$time=microtime(1);
for ($i=0;$i<100000;$i++)
  md5($i);
echo microtime(1)-$time,': md5
';
$time=microtime(1);
for ($i=0;$i<100000;$i++)
  hash('sha1', $i);
echo microtime(1)-$time,': hash/sha1
';
$time=microtime(1);
for ($i=0;$i<100000;$i++)
  sha1($i);
echo microtime(1)-$time,': sha1
';
?>
Gives:
0.33311605453491: hash/md5
1.0671429634094: md5
0.383131980896: hash/sha1
1.3252220153809: sha1
------------------------
0.37684988975525: hash/md5
1.1258299350739: md5
0.43960785865784: hash/sha1
1.3876020908356: sha1


Change Language


Follow Navioo On Twitter
hash_algos
hash_file
hash_final
hash_hmac_file
hash_hmac
hash_init
hash_update_file
hash_update_stream
hash_update
hash
eXTReMe Tracker