How to set hmacsha1 key to hex in PHP?

I can successfully get the HMAC SHA1 signature key using the following code:

echo hash_hmac('sha1','office:fred','AA381AC5E4298C23B3B3333333333333333333'); 

which gives:

 5e50e6458b0cdc7ee534967d113a9deffe6740d0 

However, the place I work in expects this:

 46abe81345b1da2f1a330bba3d6254e110cd9ad8 

I tried an online tool , and it seems that the difference between the two is that the people I work with are waiting for a HEX type signature key.

Is there something I need to add to my PHP for type HEX output?

+4
source share
1 answer

You will need to convert the hexadecimal string to binary data before passing it to hash_hmac:

 var_dump(hash_hmac("sha1", "office:fred", pack("H*", "AA381AC5E4298C23B3B3333333333333333333"))); 

The output of 46abe81345b1da2f1a330bba3d6254e110cd9ad8 is as expected.

+3
source

Source: https://habr.com/ru/post/1441288/


All Articles