简体   繁体   English

ColdFusion相当于PHP hash_hmac

[英]ColdFusion equivalent to PHP hash_hmac

$key = "12345678876543211234567887654321";
$iv = "1234567887654321";
$plaindata = "This is a test string";

$enc = base64_encode(mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $key, $plaindata, MCRYPT_MODE_CBC, $iv));

$str = hash_hmac("sha256", utf8_encode($iv . '.' . $enc), utf8_encode($key));

echo($str);

This gives me e63d4ab83f90cfec1acdaf831091b6394167ae728b657e44afad1e7553843eeb 这给了我e63d4ab83f90cfec1acdaf831091b6394167ae728b657e44afad1e7553843eeb

How can I get the same result in ColdFusion9 Development Edition? 如何在ColdFusion9开发版中获得相同的结果?

I found a solution on this page http://www.isummation.com/blog/calculate-hmac-sha256-digest-using-user-defined-function-in-coldfusion/ 我在这个网页上找到了一个解决方案http://www.isummation.com/blog/calculate-hmac-sha256-digest-using-user-defined-function-in-coldfusion/

Your need to call the function like this 你需要像这样调用这个函数

<cfoutput>#LCase(HMAC_SHA256(iv & "." & Encrypted_Data, key))#</cfoutput>

Worked like a charm. 工作就像一个魅力。

Don't have the time to work it out entirely, but I think that HMAC SHA1 ColdFusion should be able to get you there. 没有时间完全解决,但我认为HMAC SHA1 ColdFusion应该能够让你到那里。 If for some reason you can't get it perfectly right (had that once) you might also consider using php-cli with cfexecute. 如果由于某种原因你不能完全正确(有一次)你也可以考虑使用php-cli和cfexecute。 It's not the fastest solution, but if necessary it allows you to use the php functions. 它不是最快的解决方案,但如果有必要,它允许您使用PHP功能。

Edit: Copied the function from the previous answer and added a comment about the line that needed to be changed. 编辑:复制上一个答案中的功能,并添加有关需要更改的行的注释。

<cffunction name="hmacEncrypt" returntype="binary" access="public" output="false">
 <cfargument name="signKey" type="string" required="true" />
 <cfargument name="signMessage" type="string" required="true" />

 <cfset var jMsg = JavaCast("string",arguments.signMessage).getBytes("iso-8859-1") />
 <cfset var jKey = JavaCast("string",arguments.signKey).getBytes("iso-8859-1") />

 <cfset var key = createObject("java","javax.crypto.spec.SecretKeySpec") />
 <cfset var mac = createObject("java","javax.crypto.Mac") />

 <!--- this line had to be changed to the 256 version --->
 <cfset key = key.init(jKey,"hmacSHA256") />

 <cfset mac = mac.getInstance(key.getAlgorithm()) />
 <cfset mac.init(key) />
 <cfset mac.update(jMsg) />

<cfreturn mac.doFinal() />

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM