PHP base64_decode does not work in equivalent Coldfusion code

81 Views Asked by At

I have this code in PHP:

<?php
$decodedSecret = base64_decode("OWOMg2gnaSx1nukAM6SN2vxedfY1yLPONvcTKbhDv7I");
echo base64_encode(hash_hmac("sha512", "1|2481632|1425387916|GET|/api/transaction/read?spaceId=12&id=1", $decodedSecret, true));
?>

and tried to translate it in Coldfusion like this:

local.secret = toBase64( 'OWOMg2gnaSx1nukAM6SN2vxedfY1yLPONvcTKbhDv7I' );
local.hmacHex = hmac( '1|2481632|1425387916|GET|/api/transaction/read?spaceId=12&id=1', local.secret, 'HMACSHA512' );
local.base64 = binaryEncode( binaryDecode( local.hmacHex, "hex"), "base64" );

However, for some reason, this is not working and I suspect that the problem is this part:

local.secret = toBase64( 'OWOMg2gnaSx1nukAM6SN2vxedfY1yLPONvcTKbhDv7I' );

that does not seem to be equivalent to this PHP code:

$decodedSecret = base64_decode("OWOMg2gnaSx1nukAM6SN2vxedfY1yLPONvcTKbhDv7I");

Any ideas what I might be missing here?

UPDATE:

After Alex's suggestion, I used the following code snippet to achieve the goal of replicating the PHP results in Coldfusion.

local.secret = toBinary( 'OWOMg2gnaSx1nukAM6SN2vxedfY1yLPONvcTKbhDv7I=' );
local.hmacHex = hmac( '1|2481632|1425387916|GET|/api/transaction/read?spaceId=12&id=1', local.secret, 'HMACSHA512', 'utf-8' );
local.strBase64 = toBase64( binaryDecode( local.hmacHex, "hex" ) );

Unfortunatelly, toString(toBinary('OWOMg2gnaSx1nukAM6SN2vxedfY1yLPONvcTKbhDv7I')) didn't work for the secret (that is why I used only toBinary()), as hmac would return a different result than the expected one. This probably means that hmac deals differently with binaries than with strings, for anyone interested out there.

1

There are 1 best solutions below

1
Will Belden On

Was just working with this stuff. You want toBinary() if you're wanting to create a binary from base 64 encoded string data.

<cfset bData = toBinary(form.base64Data) />
<cfcontent variable=bData type="application/pdf" />

I had just built a simple form to paste my Base64 data and just get the binary. (I know it's PDF content, hence the MIME type.)