troubleshooting Question

HMAC Conversion from PHP to C#

Avatar of Des Kelly
Des KellyFlag for Ireland asked on
2 Comments1 Solution2783 ViewsLast Modified:

I have a script in PHP which generates a signed string for an API, and I need to convert it to C#.  The output is not matching somehow though, and so the API is returning a 401.  What am I missing?

signature = hash_hmac('sha256', 'GET' . _url . hash('sha256', "") . timestamp . nonce, $secret_key);

var signature = SignWithHMAC("GET" + _url + CreateHash("") + timestamp + nonce, secret_key);

 public static string SignWithHMAC(byte[] dataToSign, byte[] keyBody)
            var enc = Encoding.ASCII;
            HMACSHA256 hmac = new HMACSHA256(keyBody);

            byte[] buffer = dataToSign;
            return Convert.ToBase64String(hmac.ComputeHash(buffer)).Replace("-", "");

 public static byte[] CreateHash(byte[] data)
            SHA256 shaM = new SHA256Managed();
            byte[] result = shaM.ComputeHash(data);
            return result;

Is there any reason the two different hashing and hmac methods would return different results?

Join our community to see this answer!
Unlock 1 Answer and 2 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 2 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros