Mariusz Głuch
Mariusz Głuch

Reputation: 19

SHA 1 hash_hmac wrong

I have a big problem with hash_hmac

function

function hmac($key, $data){
    $blocksize = 64;
    $hashfunc = 'sha1';
    if (strlen($key) > $blocksize)
        $key = pack('H*', $hashfunc($key));
    $key = str_pad($key, $blocksize, chr(0x00));
    $ipad = str_repeat(chr(0x36), $blocksize);
    $opad = str_repeat(chr(0x5c), $blocksize);
    $hmac = pack('H*', $hashfunc(($key ^ $opad) . pack('H*', $hashfunc(($key ^ $ipad) . $data))));
    return bin2hex($hmac);
} 

example is:

<?php
echo hmac('111111', '222222');//=1558ab6c5ab2b0d1cd129b9ad11527cf33486705

but my

$jeden = 111111;
$dwa =222222;   
$hashWiadomosci = hash_hmac('sha1', $jeden, $dwa);

is: 22f91d281349bb3081d3cec9f906572eec5c55b2

how i do wrong?

Upvotes: 1

Views: 264

Answers (1)

Tom Udding
Tom Udding

Reputation: 2294

You have your input variables in the wrong order. If you look at the example from the comment you got this from; you can see that it is hmacsha1($key, $data) and not hmacsha1($data, $key) like you are using it and how hash_hmac($algorithm, $data, $key) works.

echo hash_hmac('sha1', '111111', '222222'); // 22f91d2813...
echo hmacsha1('111111', '222222');          // 1558ab6c5a...

echo hash_hmac('sha1', '111111', '222222'); // 22f91d2813...
echo hmacsha1('222222', '111111');          // 22f91d2813...

Upvotes: 1

Related Questions