PHP4手册:函数库及函数(二十八) mhash 杂凑函式库


PHP4手册:函数库及函数(二十八) mhash 杂凑函式库--------------------------------------------------------------------------------
mhash 杂凑函式库
--------------------------------------------------------------------------------
本函式库共有 4 个函式
本函式库支援多种杂凑演算法,例如最出名的 MD5、SHA1 或 GOST,还有其它多种的杂凑演算法,列示如下:
MHASH_MD5
MHASH_SHA1
MHASH_HAVAL
MHASH_RIPEMD160
MHASH_RIPEMD128
MHASH_SNEFRU
MHASH_TIGER
MHASH_GOST
MHASH_CRC32
MHASH_CRC32B
欲使用本函式库要先下载 mhash-x.x.x.tar.gz,网址为 http://sasweb.de/mhash。当然还要编译 mhash 程式库,之后才能编译 PHP 程式,在编译 PHP 程式时,记得要加 --with-mhash 选项打开系统的 mhash 功能。
本函式库适合用来产生检查码 (checksums)、数位代讯息或者其它功能,如下例:
$input = "Let us meet at 9 o' clock at the secret place.";
$hash = mhash(MHASH_SHA1, $input);
print "杂凑值为 ".bin2hex($hash)."\n";
?>
在浏览器看到的字串是
杂凑值为 d3b85d710d8f6e4e5efd4d5e67d041f9cecedafe
mhash_get_hash_name: 取得杂凑演算法名称。
mhash_get_block_size: 取得杂凑方式的区块大小。
mhash_count: 取得杂凑 ID 的最大值。
mhash: 计算杂凑值。
--------------------------------------------------------------------------------
函式:mhash_get_hash_name()
--------------------------------------------------------------------------------
mhash 杂凑函式库
mhash_get_hash_name
取得杂凑演算法名称。
语法: string mhash_get_hash_name(int hash);
传回值: 字串
函式种类: 编码处理
内容说明
本函式取得杂凑演算法的名称。传回值为名称字串,若没有指定的杂凑演算法则传回 false 或输入的名称。
使用范例
下例传回的字串为 MD5。
$hash = MHASH_MD5;
print mhash_get_hash_name($hash);
?>
--------------------------------------------------------------------------------
函式:mhash_get_block_size()
--------------------------------------------------------------------------------
mhash 杂凑函式库
mhash_get_block_size
取得杂凑方式的区块大小。
语法: int mhash_get_block_size(int hash);
传回值: 整数
函式种类: 编码处理
内容说明
本函式用来取得杂凑演算的区块大小。参数为编码名称,传回整数值的单位为位元组 (byte)。
--------------------------------------------------------------------------------
函式:mhash_count()
--------------------------------------------------------------------------------
mhash 杂凑函式库
mhash_count
取得杂凑 ID 的最大值。
语法: int mhash_count(void);
传回值: 整数
函式种类: 编码处理
内容说明
本函式用来取得杂凑演算的最大 ID 值。在使用杂凑计算时,会从 0 开始计数到使用的数值。本函式不用输入参数。
使用范例
$nr = mhash_count();
for($i = 0; $i <= $nr; $i++) {
echo sprintf("杂凑 %s 的区块大小为 %d\n", mhash_get_hash_name($i), mhash_get_block_size($i));
}
?>
--------------------------------------------------------------------------------
函式:mhash()
--------------------------------------------------------------------------------
mhash 杂凑函式库
mhash
计算杂凑值。
语法: string mhash(int hash, string data);
传回值: 字串
函式种类: 编码处理
内容说明
本函式依指定的杂凑演算法计算杂凑值。参数 hash 为指定的杂凑演算法;参数 data 为欲计算的字串值。