I’m struggling with this for a while now. I scanned pretty much all there is on google but I just can’t find the right script, how to create a HMAC-SHA-256 algorithm with a 128-bit key.
It should be as simple as this:
$algo = 'sha256';
$file = 'file.txt';
$key = '1234567890abcdef1234567890abcdef';
echo hash_hmac_file ($algo, $file, $key );
file.txt contains only 8 zeros ( 00000000 ).
The output has to be FF365893D899291C3BF505FB3175E880, but I just can’t understand how?
I have tried multiple hashing algorithms, but none of them return the necessary value. What am I doing wrong here and how to fix it?
I tried also this solution, but it still comes out wrong.
I need this for connection with a bank and they provide these test data to start with.
Final answer:
echo 'FF365893D899291C3BF505FB3175E880<br />';
$algo = 'sha256'; $file = 'file.txt'; $key = '1234567890abcdef1234567890abcdef';
echo substr( strtoupper( hash_hmac_file( $algo, $file, pack("H*" , $key) ) ), 0, 32 );
Your key needs to be converted from hexadecimal to binary data. Add:
And also make sure that you
file.txtdoes not contain any trailing character like new line or carriage return.Edit: The function hex2bin was introduced in PHP 5.4.0. For older versions you can use instead