Shopify C# HMAC SHA256 OAuth 验证

Guy*_*owe 3 c# oauth sha256 hmac shopify

我正在尝试在 OAUTH 请求期间验证 Shopify HMAC,但我生成的哈希与作为请求的一部分提供的哈希不匹配。

我发现了一些其他线程,但它们要么已经过时,因为文档现在声明它使用 GET 请求而不是 POST,或者在 java 中没有得到答复

我的 C# 代码如下:

string key = "mysecretkey";

string message = string.Format("shop={0}&timestamp={1}", shop, timestamp);

System.Text.ASCIIEncoding encoding = new ASCIIEncoding();

byte[] keyBytes = encoding.GetBytes(key);

byte[] messageBytes = encoding.GetBytes(message);

System.Security.Cryptography.HMACSHA256 cryptographer = new System.Security.Cryptography.HMACSHA256(keyBytes);

byte[] bytes = cryptographer.ComputeHash(messageBytes);

string digest = BitConverter.ToString(bytes).Replace("-", "");

bool valid = digest == hmac.ToUpper();
Run Code Online (Sandbox Code Playgroud)

我猜消息的构建不正确,但我没有走运就遵循了官方文档

有人可以帮忙吗?

Guy*_*owe 5

好的,Shopify 的开发人员给了我答案。似乎您需要按字母顺序散列查询字符串的全部内容,但签名和 hmac 除外。我有我自己的参数(rlr),我正在附加以及文档(状态)中未提及的参数。

 string message = "";// "code=7af66fd73427a1634cee3103297230b8&rlr=9DFD5EA9-7747-4142-97D9-2D44BBA442F1&shop=appswiz.myshopify.com&state=fa992b8f-762e-4813-b707-6044e71ad3b5&timestamp=1448856806";
        message = "code=xxxxxxxx";
        message += "&rlr=xxxxx";
        message += "&shop=xxx.myshopify.com";
        message += "&state=xxxxxxxx";
        message += "&timestamp=1449111190";
        hmac = "xxxxxxx";
        System.Text.ASCIIEncoding encoding = new ASCIIEncoding();
        byte[] keyBytes = encoding.GetBytes(key);
        byte[] messageBytes = encoding.GetBytes(message);
        System.Security.Cryptography.HMACSHA256 cryptographer = new System.Security.Cryptography.HMACSHA256(keyBytes);

        byte[] bytes = cryptographer.ComputeHash(messageBytes);

        string digest = BitConverter.ToString(bytes).Replace("-", "");
        return digest == hmac.ToUpper();
Run Code Online (Sandbox Code Playgroud)

这现在有效。