Skip to main content
Inspiring
August 21, 2023
Question

Hex object in CF

  • August 21, 2023
  • 1 reply
  • 184 views
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 has been closed for replies.

    1 reply

    Legend
    August 21, 2023

    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();