我目前正在使用 Shopify 并使用他们的 webhook 通知,这样我就可以将东西保存到我们的数据库中。
在他们的 webhook header 中,他们提供了一个 header :X-Shopify-Hmac-Sha256
这是:
Each Webhook request includes a X-Shopify-Hmac-SHA256 header which is generated using the app's shared secret (looks like: '267bb1719a8e6ff75c4f2d709be0ca11'), along with the data sent in the request.
这真是太棒了;但是,我真的很难计算 X-Shopify-Hmac-Sha256
的值。
我有一个 .cfm
页面,webhook 命中该页面,我通过 getHTTPRequestData
传递给这样的函数:
<cfscript>
variables.stArgs = {};
variables.stArgs.stWebHookData = getHTTPRequestData();
application.stObj.stShopify.oShopifyWebHookBusiness.receiveWebHook(argumentCollection=variables.stArgs);
</cfscript>
然后我从 StackOverflow 和 Ben Nadel 那里窃取了一些代码,但似乎都没有给我想要的值(value)。目前我正在使用 Ben Nadels 这样的代码:
local.data = arguments.stWebHookData.toString();
local.macClass = createObject( "java", "javax.crypto.Mac" );
local.secretkeySpec = createObject( "java", "javax.crypto.spec.SecretKeySpec" ).init(toBinary(toBase64(arguments.key)),'HmacSHA256');
local.mac = local.macClass.getInstance('HmacSHA256');
local.mac.init(local.secretkeySpec );
local.hashedBytes = local.mac.doFinal(toBinary(toBase64(local.data)));
return lcase( binaryEncode( local.hashedBytes, "base64" ) );
(arguments.key
是共享 key )
一路走来,我错了。我是否完全误解了我的意思。这在 PHP 中看起来很简单。
最佳答案
所以,getHTTPRequestData() returns a struct with a number of members .我们感兴趣的是内容,它将是一个字节数组。
MAC 类的 doFinal()方法需要一个字节数组(在我们的例子中是 HTTP 请求的内容)并返回一个字节数组(内容的 HMac)
返回的字节数组需要进行 base-64 编码,以便将其与 Shopify 放入 webhook header 中的字节数组进行比较。 toBase64()会为我们做的。
将所有内容放在一起,您会得到:
toBase64(local.mac.doFinal(getHTTPRequestData().content))
关于java - coldfusion 计算 getHTTPRequestData 的 HMAC256,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21333117/