PHP - mhash() 函数



mhash() 函数可以计算哈希值。

语法

string mhash( int $hash , string $data [, string $key ] )

mhash() 函数可以对数据应用由 hash 指定的哈希函数。

mhash() 函数可以返回生成的哈希值(也称为摘要)或 HMAC 作为字符串,或者在发生错误时返回 false。

示例 1

<?php
   $mes01 = "Water dropping day by day wears the hardest rock away.";

   $alg01 = MHASH_SHA256; // ALGO = 17

   $ctl01 = "6r0pp1n9";

   $hmacD = mhash($alg01, $mes01, $ctl01);
   $hmacH = bin2hex($hmacD);

   echo "ALGO ( " . $alg01 . " )ORIGINAL MESSAGE:" . $mes01 
        . "MHASH RAW-BINARY:" . $hmacD 
        . "MHASH HEXADECIMAL:" . $hmacH;
?> 

输出

ALGO ( 17 )ORIGINAL MESSAGE:Water dropping day by day wears the hardest rock away.MHASH RAW-BINARY:@�KLe.��!��ɧd�u����b��f���2�MHASH HEXADECIMAL:021340804b4c652ea4b121c0d5c9a764dd75f7bbfbfc0262e7dc66899cfa32ef

示例 2

<?php
   $mes02 = "Water dropping day by day wears the hardest rock away.";

   $nbrh = mt_rand(0, 33);

   $alg02 = $nbrh;
   $ctl02 = "6r0pp1n9";

   $hmacD = @mhash($alg02, $mes02, $ctl02);
   $hmacH = bin2hex($hmacD);

   echo "ALGO ( " . $alg02 . " )ORIGINAL MESSAGE:" . $mes02 
        . "MHASH RAW-BINARY:" . $hmacD 
        . "MHASH HEXADECIMAL:" . $hmacH;
?> 

输出

ALGO ( 11 )ORIGINAL MESSAGE:Water dropping day by day wears the hardest rock away.MHASH RAW-BINARY:���E�R^x�*���c|Jt��\KMHASH HEXADECIMAL:b7d4e00345cc525e780403cf2af593fd637c4a74c9e85c4b
php_function_reference.htm
广告