我正在尝试在OAUTH请求期间验证Shopify HMAC,并且我生成的哈希与作为请求的一部分提供的哈希不匹配 .
我发现了一些其他线程,但它们是either outdated,因为文档现在声明它使用GET请求而不是POST,或者在java中使用unanswered .
我的C#代码如下:
string key = "mysecretkey";
string message = string.Format("shop={0}×tamp={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();
我'm guessing the message is being built incorrectly but I'跟着official documentation没有运气 .
有人可以帮忙吗?
3 回答
您在不使用密钥的情况下计算HMAC .
文档说明您应该使用共享密钥生成HMAC摘要 . 没有密钥的HMAC值的含义是什么?如果Shopify没有使用您和他们之间的预共享密钥来计算HMAC,任何人都可以模仿shopify服务器 .
以下代码块来自文档:
因此,在计算Hash之前尝试
cryptographer.Key = keyBytes;
好吧,Shopify的开发人员回答了我的答案 . 看来除了签名和hmac之外,你需要按字母顺序散列查询字符串的 entire 内容 . 我有自己的参数(rlr)我正在添加以及文档(状态)中未提及的参数 .
这现在有效 .
使用Guy Lowe的回答我最近得到了这个工作: