PHP: hash - Manual

downloads | documentation | faq | getting help | mailing lists | reporting bugs | php.net sites | links | conferences | my php.net

search for in the

HTTP hash_update

Last updated: Sat, 27 Jan 2007

view this page in

# hash

(no version information, might be only in CVS)

hash -- Generate a hash value (message digest)

## Описание

string hash ( string algo, string data [, bool raw_output] )

## Список параметров

`algo`

  Name of selected hashing algorithm (i.e. "md5", "sha256", "haval160,4", etc..)

`data`

  Message to be hashed.

`raw_output`

  When set to `TRUE`, outputs raw binary data. Default value (`FALSE`) outputs lowercase hexits.

## Возвращаемые значения

Returns a string containing the calculated message digest as lowercase hexits unless `raw_output` is set to true in which case the raw binary representation of the message digest is returned.

## Примеры

Пример 1. A hash() example

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

Результат выполнения данного примера:

    ec457d0a974c48d5685a7efa03d137dc8bbde7e3

## Смотрите также

hash_file()
hash_hmac()
hash_init()

add a note User Contributed Notes
hash
mikael at webhost3 dot eu
10-Mar-2007 05:34
`<?php
$time=microtime(1);
for ($i=0;$i<100000;$i++)
hash('md5', 'string');
echo microtime(1)-$time,': hash/md5<br>';
$time=microtime(1);
for ($i=0;$i<100000;$i++)
md5('string');
echo microtime(1)-$time,': md5<br>';
$time=microtime(1);
for ($i=0;$i<100000;$i++)
hash('sha1', 'string');
echo microtime(1)-$time,': hash/sha1<br>';
$time=microtime(1);
for ($i=0;$i<100000;$i++)
sha1('string');
echo microtime(1)-$time,': sha1<br>';
?>------------------------<br><?php
$time=microtime(1);
for ($i=0;$i<100000;$i++)
hash('md5', $i);
echo microtime(1)-$time,': hash/md5<br>';
$time=microtime(1);
for ($i=0;$i<100000;$i++)
md5($i);
echo microtime(1)-$time,': md5<br>';
$time=microtime(1);
for ($i=0;$i<100000;$i++)
hash('sha1', $i);
echo microtime(1)-$time,': hash/sha1<br>';
$time=microtime(1);
for ($i=0;$i<100000;$i++)
sha1($i);
echo microtime(1)-$time,': sha1<br>';
?>
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`
Peter Kelly
29-Jan-2007 04:32
`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));
}
}`
anthony dot parsons at manx dot net
15-Dec-2006 07:08
`These functions are worth using, if only because they're about twice as fast as the built-in counterparts:
<?php
$time=microtime(1);
for ($i=0;$i<100000;$i++)
hash('md5', 'string');
echo microtime(1)-$time,': hash/md5<br>';
$time=microtime(1);
for ($i=0;$i<100000;$i++)
md5('string');
echo microtime(1)-$time,': md5<br>';
$time=microtime(1);
for ($i=0;$i<100000;$i++)
hash('sha1', 'string');
echo microtime(1)-$time,': hash/sha1<br>';
$time=microtime(1);
for ($i=0;$i<100000;$i++)
sha1('string');
echo microtime(1)-$time,': sha1<br>';
?>
Gives:
0.36409902572632: hash/md5
0.79715204238892: md5
0.46680307388306: hash/sha1
1.1121778488159: sha1`
add a note
HTTP hash_update

Last updated: Sat, 27 Jan 2007
show source | credits | stats | sitemap | contact | advertising | mirror sites
Copyright © 2001-2007 The PHP Group
All rights reserved.
This mirror generously provided by: Yahoo! Inc.
Last updated: Sat Mar 10 09:32:46 2007 PST