phpencodesha1hmac

How to encode this from hex to hex in php ? hmac sha-1


I need to write THIS in php

https://cryptii.com/pipes/QiZmdA

I have tried those but don't produce the same result

<?php
    echo hash_hmac('sha1', '002590cd26da', "8544e3b47eca58f9583043f8");
 echo var_dump(hash_hmac("sha1", "002590cd26da", pack("H*", "8544e3b47eca58f9583043f8")));
?>

what am I doing wrong?


Solution

  • You need convert the hex string to binary first then use function hash_hmac with 'true' parameter to get binary output and last,convert the binary HMAC to a hex string.

    Here fixed example code :

    <?php
    $data = hex2bin("002590cd26da"); // Convert the hex string to binary
    $key = hex2bin("8544e3b47eca58f9583043f8"); // Convert the hex key to binary
    
    $hmac = hash_hmac('sha1', $data, $key, true); // Use 'true' to get binary output
    $hmac_hex = bin2hex($hmac); // Convert the binary HMAC to a hex string
    
    echo "HMAC: " . $hmac_hex;
    ?>
    

    The code will produce output : HMAC: 857ab7a94a4c103e3a8cc0442db0d4745064bb73

    Live test : https://3v4l.org/q4rJj