本文介绍了Coldfusion HMAC-SHA1加密的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这里有一个使用javascript的示例HMAC-SHA1。





文字加密



vibaHBXwUXFqVSg - + kTrqYJZEJkbVeqLc = bo.LlXGET12505351831husu9039http://api.tineye.com/rest/search/image_url=http%3a%2f%2ftineye.com%2fimages%2ftineye_logo_big.png& limit = 30& offset = 10





vibaHBXwUXFqVSg- + kTrqYJZEJkbVeqLc = bo.LlX





9e734661c9e8b6dc9b6b4b3def9769c00e8843b8



发行



然而,在Coldfusion中复制输出。我使用的是一个来自之前的Stackoverflow.com问题的函数

 < cffunction name =hmacEncryptreturntype = access =publicoutput =false> 
< cfargument name =signKeytype =stringrequired =true/>
< cfargument name =signMessagetype =stringrequired =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)/>

< cfset key = key.init(jKey,HmacSHA1)/>

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

< cfreturn mac.doFinal()/>
< / cffunction>

< cfset result = hmacEncrypt(vibaHBXwUXFqVSg- + kTrqYJZEJkbVeqLc = bo.LlX,vibaHBXwUXFqVSg- + kTrqYJZEJkbVeqLc = bo.LlXGET12505351831husu9039http://api.tineye.com/rest/search/image_url= http%3a%2f%2ftineye.com%2fimages%2ftineye_logo_big.png& limit = 30& offset = 10)>

< cfset x1 = toString(tobase64(result))>



这会返回



nnNGYcnottyba0s975dpwA6IQ7g = p>

任何帮助感谢。



解决方案

另一个函数是返回hex,而不是base64。除此之外,它似乎对我工作正常:

 < cfset x1 = binaryEncode(result,hex)> 


There is an example HMAC-SHA1 here that works in javascript

http://jssha.sourceforge.net/

Text to encrypt

vibaHBXwUXFqVSg-+kTrqYJZEJkbVeqLc=bo.LlXGET12505351831husu9039http://api.tineye.com/rest/search/image_url=http%3a%2f%2ftineye.com%2fimages%2ftineye_logo_big.png&limit=30&offset=10

Key

vibaHBXwUXFqVSg-+kTrqYJZEJkbVeqLc=bo.LlX

Output

9e734661c9e8b6dc9b6b4b3def9769c00e8843b8

Issue

I can't however duplicate the output in Coldfusion. I'm using a function from a previous Stackoverflow.com question

<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") />

   <cfset key = key.init(jKey,"HmacSHA1") />

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

   <cfreturn mac.doFinal() />
</cffunction>

<cfset result = hmacEncrypt("vibaHBXwUXFqVSg-+kTrqYJZEJkbVeqLc=bo.LlX", "vibaHBXwUXFqVSg-+kTrqYJZEJkbVeqLc=bo.LlXGET12505351831husu9039http://api.tineye.com/rest/search/image_url=http%3a%2f%2ftineye.com%2fimages%2ftineye_logo_big.png&limit=30&offset=10")>

<cfset x1 = toString(tobase64(result))>

This returns

nnNGYcnottyba0s975dpwA6IQ7g=

Any help appreciated.

解决方案

The other function is returning hex, not base64. Other than that it seems to work fine for me:

 <cfset x1 = binaryEncode(result, "hex")>

这篇关于Coldfusion HMAC-SHA1加密的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-27 15:08