Not yet rated

Problem

Used for integration of Linkshare affiliate program in Coldfusion.

Solution

Encrypted message signatures via JAVA in CF

Detailed explanation

<cffunction name= "HMACMD5" returntype= "string" access= "public" hint= "Returns an MD5 encrypted message">
         <!--- You may choose the format of your output --->
<cfargument name= "message" type= "String" required= "true">
         <cfargument name= "key" type= "String" required= "true">
         <cfargument name= "outputFormat" type= "string" required= "false" default= "String" hint= "String,base64,uBase64">

         <cfset var myMessageString = arguments.message >
         <cfset var myMessageByteArray = myMessageString.getBytes()>

         <cfset var myKey = arguments.key>
         <cfset var myKeyByteArray = myKey.getBytes()>

         <cfscript>

        key = CreateObject( "java", "javax.crypto.spec.SecretKeySpec");
        key.init(myKeyByteArray, "HmacMD5");

        hmac = CreateObject( "java", "javax.crypto.Mac");
        hmac = hmac.getInstance( "HmacMd5");
        hmac.init(key);

        hmac.update(myMessageByteArray);

        hmac = hmac.doFinal();

        
</cfscript>
        
         <cfswitch expression= "#arguments.outputFormat#">
             <cfcase value= "base64">
                 <cfreturn tobase64(hmac)>
             </cfcase>
             <cfcase value= "ubase64">
                 <cfreturn URLEncodedFormat(tobase64(hmac))>
             </cfcase>
             <cfdefaultcase>
                 <cfreturn toString(hmac)>
             </cfdefaultcase>
         </cfswitch>

</cffunction>


+
This work is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License. Permissions beyond the scope of this license, pertaining to the examples of code included within this work are available at Adobe.

Report abuse

Related recipes