使用 Java 在 ColdFusion 中计算 HMAC-SHA256 摘要

2022-01-09 00:00:00 encryption hmac coldfusion java

我们正在尝试在 ColdFusion 中计算 HMAC-SHA256 摘要,并且我们正在使用 HMAC CFC,但在一种情况下,与以不同语言生成的摘要相比,它产生的摘要结果不同 - 尝试使用相同的数据红宝石&PHP 并得到预期的结果.我也尝试过它所基于的 CF_HMAC 自定义标签,并得到了相同的结果.

We are trying to calculate a HMAC-SHA256 digest in ColdFusion and we are using the HMAC CFC, but in one case it is producing a different result for the digest compared to ones generated in different languages - have tried the same data using Ruby & PHP and get the expected result. I have also tried the CF_HMAC custom tag it is based on and get the same results.

我了解到,从 CF8 encrypt() 开始支持 HMAC-SHA256,但它仅在 Enterprise 中可用(我们没有),甚至在开发者版本中也没有供我测试.

I understand that from CF8 encrypt() supports HMAC-SHA256, but it's only available in Enterprise (which we don't have) and isn't even available in developer version for me to test.

所以我的问题是我可以通过从 CF 访问 Java 来做到这一点吗?

So my question is can I do this by accessing Java from CF?

推荐答案

这就是我最终做的:

secret = createObject('java', 'javax.crypto.spec.SecretKeySpec' ).Init(my_key.GetBytes(), 'HmacSHA256');
mac = createObject('java', "javax.crypto.Mac");
mac = mac.getInstance("HmacSHA256");
mac.init(secret);
digest = mac.doFinal(my_data.GetBytes());

这将为您提供字节数组,然后您可以将其转换为字符串.

This gives you the byte array, which you can then convert to a string.

相关文章