I've looked at numerous questions around this but can't see the mistake I'm making. I'm trying to generate a Shared Access Signature to access an Azure SB Queue.
My C# code is working correctly:
var expiry = 1499177142;// GetExpiry();
string stringToSign = HttpUtility.UrlEncode(resourceUri);// + "\n" + expiry;
HMACSHA256 hmac = new HMACSHA256(Encoding.UTF8.GetBytes(key));
Console.WriteLine(Encoding.UTF8.GetBytes(stringToSign));
var signature = Convert.ToBase64String(hmac.ComputeHash(Encoding.UTF8.GetBytes(stringToSign)));
Console.WriteLine(signature);
var sasToken = String.Format(CultureInfo.InvariantCulture, "SharedAccessSignature sr={0}&sig={1}&se={2}&skn={3}",
HttpUtility.UrlEncode(resourceUri), HttpUtility.UrlEncode(signature), expiry, keyName);
return sasToken;
// returns slXBw0u7Dt/YKS1Y+Wot02z730YXJ9NkS599JRzvDQI= for the signature element
My PHP gives a very different result:
$stringToSign = rawurlencode($resourceURI);// . "%0A" . $expiry;
$sig = hash_hmac("sha256",utf8_encode($stringToSign),utf8_encode($key),false);
echo $sig."<br>";
$token = "SharedAccessSignature sr=".urlencode($resourceURI)
."&sig=".rawurlencode(base64_encode($sig))."&se=".$expiry."&skn=".$keyName;
return $token; //returns MGNlZWViYWRmMjE2NWJhZGRjNWNhNDZkYWRlOTQyMzc3ODBhMWM2ZjA1OTk4MjI0MGUzMzllZmY4ZTk2OGUxNA==
I've tried encoding $stringToSign
and $key
and not in the hash, strtoupper
and strtolower
on the $stringToSign
but can't get the result to be the same as C#