I want to turn the following C# code into PHP.
The C# is:
byte[] operation = UTF8Encoding.UTF8.GetBytes("getfaqs");
byte[] secret = UTF8Encoding.UTF8.GetBytes("Password");
var hmac = newHMACSHA256(secret);
byte[] hash = hmac.ComputeHash(operation);
Which I’ve turned into this:
$hash = hash_hmac( "sha256", utf8_encode("getfaqs"), utf8_encode("Password"));
Then I have:
var apiKey = "ABC-DEF1234";
var authInfo = apiKey + ":" + hash
//base64 encode the authorisation info
var authorisationHeader = Convert.ToBase64String(Encoding.UTF8.GetBytes(authInfo));
Which I think should be:
$authInfo = base64_encode($apiKey . ":" . $hash);
Or
$authInfo = base64_encode(utf8_encode($apiKey . ":" . $hash));
But not certain, notice this second encoding uses Encoding.UTF8, not UTF8Encoding.UTF8.
What should the PHP code look like?
PHP strings are already (kind of)
byte[], php doesn’t have any encoding awareness.utf8_encodeactually turns ISO-8859-1 to UTF-8, so it’s not needed here.If those strings are literals in your file, that file just needs to be saved in UTF-8 encoding.
Pass
truetohash_hmacas 4th parameter and remove thoseutf8_encodecalls:Also, string concatenation operator is
., so :