Skip to main content
Inspiring
Aug 21, 2023 1:13 PM UTC
Question

Hex object in CF

Hi,

I am trying to convert some java code (below) to Coldfusion but am running into an error "Variable HEX is undefined". How can I create a Hex object in CF?

 

 

<cfset input='{"id":7,"attempts":1,"notifications":[{"action":"UPDATE","message":{"id":0,"sender":"","body":"{\u0022TableId\u0022:930002,\u0022TableName\u0022:\u0022DEMOGRAPHIC\u0022,\u0022TableAction\u0022:\u0022UPDATE\u0022}"}}]}'>

<cfset key = "xxxx">

<cfscript>

secret = createObject('java', 'javax.crypto.spec.SecretKeySpec' ).Init(key.GetBytes("UTF-8"), 'HmacSHA256');
mac = createObject('java', "javax.crypto.Mac");
mac = mac.getInstance("HmacSHA256");
mac.init(secret);
digest = Hex.encodeHexString(mac.doFinal(input.GetBytes("UTF-8")));
</cfscript>

 

    This topic is closed to new replies. Start a new post to keep the conversation going.

    1 reply

    Legend
    Aug 21, 2023 1:18 PM UTC

    That looks to be a Hex object from the Apache library.  I believe you should be able to instantiate it like so:

    Hex = createObject('java', "org.apache.commons.codec.binary.Hex").init();
    Translate