<!---
<!---
Coldfusion sample for HMAC-MD5 or HMAC-SHA1 hash generation.
The hash obtained and the payment data are used in the POST form
(not shown here - see Hosted Checkout Integration Manual for details).
This is Verified on ColdFusion 8, Developer Edition, on Ubuntu 8.04/Hardy Heron
--->
---->
<cfoutput>
<!---set the fields required. note utc time (x_fp_timestamp) is a time in
seconds since Jan 1 1970 based on GMT timezone
--->
<cfset x_fp_timestamp = "1219853058">
<cfset x_transaction_key = "PLEASE REPLACE WITH CURRENT TRANSACTION KEY">
<cfset x_login = "WSP-TEST-01-01">
<cfset x_amount = "30">
<cfset x_currency_code = "CAD">
<cfset x_line_items = "">
<cfset x_fp_sequence = "344155">
<cfset x_invoice_num = "123">
<!---a number of the fields are strung together and then hashed into a
new hash field that is submitted in place of the password. This way the
password and merchant account are never both submitted --->
<cfset hmac_data = x_login>
<cfset hmac_data = hmac_data & "^" & x_fp_sequence>
<cfset hmac_data = hmac_data & "^" & x_fp_timestamp>
<cfset hmac_data = hmac_data & "^" & x_amount>
<cfset hmac_data = hmac_data & "^" & x_currency_code>
<!---now hash this data string with the transaction key --->
<cfset x_fp_hash = java_hmac(x_transaction_key, hmac_data)>
<h2>Hash obtained </h2>
<ul>
<b> #x_fp_hash# </b>
</ul>
</cfoutput>
<cffunction name="java_hmac" returntype="string" 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("UTF8") />
<cfset var jKey = JavaCast("string",arguments.signKey).getBytes("UTF8") />
<cfset var key = createObject("java","javax.crypto.spec.SecretKeySpec") />
<cfset var mac = createObject("java","javax.crypto.Mac") />
<!-- HMAC MD5 (uncomment to choose MD5, then comment SHA1)
<cfset key = key.init(jKey,"HmacMD5") />
-->
<cfset key = key.init(jKey,"HmacSHA1") />
<cfset mac = mac.getInstance(key.getAlgorithm()) />
<cfset mac.init(key) />
<cfset mac.update(jMsg) />
<cfreturn LCase(BinaryEncode(mac.doFinal(), 'Hex')) />
</cffunction>
The article has been updated successfully.