PHP - funzioni mhash ()

Il mhash() la funzione può calcolare un hash.

Sintassi

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

La funzione mhash () può applicare una funzione hash specificata da hash ai dati.

La funzione mhash () può restituire l'hash risultante (chiamato anche digest) o HMAC come stringa o false in caso di errore.

Esempio 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;
?>

Produzione

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

Esempio 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;
?>

Produzione

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