mhash equivalent

Discussion in 'PHP' started by cloudnthunder, Nov 30, 2009.

  1. #1
    Hi I want to check a string using the function mhash:

    mhash(MHASH_SHA256, $string)

    However my server does not have the mhash function installed.

    What is the alternative for mhash?

    Can I use this:

    hash("sha256", $string, "true")
     
    cloudnthunder, Nov 30, 2009 IP
  2. JAY6390

    JAY6390 Peon

    Messages:
    918
    Likes Received:
    31
    Best Answers:
    0
    Trophy Points:
    0
    #2
    They don't output the same values
    You will need to get some kind of a hex2bin function (which doesnt exist in php itself but you can find them). For example

    // hex2bin function by chaos79 taken from http://php.net/manual/en/function.bin2hex.php
    function hex2bin($h) {
    	if (!is_string($h)) return null;
    	$r = '';
    	for ($a = 0; $a < strlen($h); $a += 2) {
    		$r .= chr(hexdec($h{$a}.$h{($a + 1)}));
    	}
    	return $r;
    }
    
    $string = 'example';
    echo hex2bin(hash('sha256', $string));
    
    PHP:
    This would output the exact same as
    echo mhash(MHASH_SHA256, $string);
    PHP:
     
    JAY6390, Nov 30, 2009 IP
  3. cloudnthunder

    cloudnthunder Peon

    Messages:
    163
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    I see thanks.
     
    cloudnthunder, Nov 30, 2009 IP