LXXIII. Mhash Functions

简介

These functions are intended to work with mhash. Mhash can be used to create checksums, message digests, message authentication codes, and more.

This is an interface to the mhash library. mhash supports a wide variety of hash algorithms such as MD5, SHA1, GOST, and many others. For a complete list of supported hashes, refer to the documentation of mhash. The general rule is that you can access the hash algorithm from PHP with MHASH_HASHNAME. For example, to access TIGER you use the PHP constant MHASH_TIGER.

需求

To use it, download the mhash distribution from its web site and follow the included installation instructions.

安装

You need to compile PHP with the --with-mhash[=DIR] parameter to enable this extension. DIR is the mhash install directory.

运行时配置

本扩展模块在 php.ini 中未定义任何配置选项。

资源类型

本扩展模块未定义任何资源类型。

预定义常量

以下常量由本扩展模块定义,因此只有在本扩展模块被编译到 PHP 中,或者在运行时被动态加载后才有效。

Here is a list of hashes which are currently supported by mhash. If a hash is not listed here, but is listed by mhash as supported, you can safely assume that this documentation is outdated.

  • MHASH_ADLER32

  • MHASH_CRC32

  • MHASH_CRC32B

  • MHASH_GOST

  • MHASH_HAVAL128

  • MHASH_HAVAL160

  • MHASH_HAVAL192

  • MHASH_HAVAL256

  • MHASH_MD4

  • MHASH_MD5

  • MHASH_RIPEMD160

  • MHASH_SHA1

  • MHASH_SHA256

  • MHASH_TIGER

  • MHASH_TIGER128

  • MHASH_TIGER160

范例

例子 1. Compute the MD5 digest and hmac and print it out as hex

<?php
$input
= "what do ya want for nothing?";
$hash = mhash(MHASH_MD5, $input);
echo
"The hash is " . bin2hex($hash) . "<br />\n";
$hash = mhash(MHASH_MD5, $input, "Jefe");
echo
"The hmac is " . bin2hex($hash) . "<br />\n";
?>

This will produce:
The hash is d03cb659cbf9192dcd066272249f8412 
The hmac is 750c783e6ab0b503eaa86e310a5db738

目录
mhash_count -- Get the highest available hash id
mhash_get_block_size -- Get the block size of the specified hash
mhash_get_hash_name -- Get the name of the specified hash
mhash_keygen_s2k -- Generates a key
mhash -- Compute hash

add a note add a note User Contributed Notes
brentdothansenatgmaildotcom
12-Aug-2005 07:43
Since it seems that the tiger hash bug has been labeled "bogus" here is a fix to give a correct result.  I'm not a binary expert so if you come up with a better fix please let us know. Just do your MHASH_TIGER as normal then send the unaltered binary into tigerfix and you get a proper HEX return.

function tigerfix ($binary_hash) {
     $my_split = str_split($binary_hash,8);
     $my_tiger ="";
     foreach($my_split as $key => $value) {
       $my_split[$key] = strrev($value);
       $my_tiger .= $my_split[$key];
     }
   $my_tiger_hex = bin2hex($my_tiger);
   return $my_tiger_hex;
}
Keamos at gmail dot com
26-Mar-2005 09:16
The Tiger implementation in Mhash is broken--

http://pastebin.ca/8272

Cryptopp-PHP passes the test vectors (found at http://tinyurl.com/4g9su) but Mhash appears to not do so--use Cryptopp-PHP for now if you need to use Tiger.
m1tk4 at hotmail dot com
22-Jun-2004 01:43
To enable mhash on RHEL/Fedora Core/other RPM-based Linuxes without rebuilding PHP, get the php-mhash and mhash RPMs at http://phprpms.sourceforge.net/mhash
01-Aug-2001 10:13
MHASH_HAVAL256 , MHASH_HAVAL192, etc...
refers to the HAVAL hash with 3 rounds.

To use HAVAL with 4 or 5 rounds, you have to
recompile the mhash library and either add
new hash names, or just change in mhash.c
the definitions of MHASH_HAVAL256,...