F
F
Fyodor2014-08-12 18:20:14
PHP
Fyodor, 2014-08-12 18:20:14

How to generate HMAC with SHA-256 in PHP correctly?

I deal with the bank - there is the following process - we get the secret key and collect the control line. then, based on this data, we generate an HMAC using the SHA-256 algorithm and thereby sign the payment form.
They issue the secret key in Base64, to generate HMAC it needs to be converted to HEX. HMAC must be sent in base64 (it can be in hex, but base64 is preferable).
In general, in php I wrote like this

$binary = base64_decode($secret);
$hex = strtoupper(bin2hex($binary));
$hmac =  base64_encode(hash_hmac('sha256', $stroka, $hex, true));

But the HMAC generated in this way is not suitable. The bank issues a utility where you can generate using javascript and there the HMAC is generated normally ... well, that is. accepted by the bank.
Where could it be wrong? In JS, I’m not strong enough to find the differences.
Here I deployed
the PHP variant
. The file code is as follows
$hmac =  hash_hmac('sha256', '1680024001;80024001;30;5.00', 'AF63D092A79416EC6CA99891EB8CF4E6AFEF5DCE9D6308CBA6A186D03A4B70F3', false);

echo strtoupper($hmac);

And here the option from the bank (removed) - at the very bottom, click HMAC HEX
, the lines I received do not match at all ...
the bank's technical support has not yet been able to help.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander Kubintsev, 2014-08-12
@Richard_Ferlow

You forgot to convert the key from hex to binary representation.
PS. It is better to remove the link to the test payments page, for sure you have some kind of NDA signed with the bank for such things.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question