I have the following function in C#, a hashing function which I need converted into PHP. I've tried a few things in PHP but I don't get the same results (I'm not at all good with .NET)
private static string GetSignature(string args, string privatekey)
{
var encoding = new System.Text.ASCIIEncoding();
byte[] key = encoding.GetBytes(privatekey);
var myhmacsha256 = new HMACSHA256(key);
byte[] hashValue = myhmacsha256.ComputeHash(encoding.GetBytes(args));
string hmac64 = Convert.ToBase64String(hashValue);
myhmacsha256.Clear();
return hmac64;
}
One (wrong) attempt in PHP is this:
function encode($data,$key)
{
return base64_encode( hash_hmac('sha256', $data, $key ) );
}
The ANSWER
A slight variation of what was suggested below by DampeS8N worked for me.
function encode($data,$key)
{
iconv_set_encoding("input_encoding", "ASCII");
iconv_set_encoding("internal_encoding", "ASCII");
iconv_set_encoding("output_encoding", "ASCII");
return base64_encode( hash_hmac('sha256', $data, $key, true ) );
}
Please also not the fourth parameter of hash_hmac - now set to true for raw output as binary data