I need help! could someone help me out here. I need code to implement HMAC-MD5, encoded in UTF-8. Below is some sample code in C, C# and others. I want a Java or Lotus Script code I can run in an agent. I want to be able to pass a secret key and a predefined string to be hashed together.
I know lotus script, but very little java. Just enough to mess things up. So please not that.
I know there are a lot of wise programmers out there that can help.
Please note: x_fp_hash is the hash between the secret key and the predefined string. predefined_string = x_login^x_fp_sequence^x_fp_timestamp^x_amount^x_currency_code.
Thanks in advance.
To generate x_fp_hash using C please see the code below:
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
#include <openssl/hmac.h>
#include <stdio.h>
int main() {
const char key = “V0WX5fK~o6eEhr7hbs3ZeyxS”;
unsigned char result[EVP_MAX_MD_SIZE], hex_result[EVP_MAX_MD_SIZE*2];
char* p;
unsigned char data[100];
unsigned int datalen, resultlen, i;
HMAC_CTX ctx;
// HMAC MD5
// HMAC_Init(&ctx, key, sizeof(key), EVP_md5());
// HMAC SHA1
HMAC_Init(&ctx, key, sizeof(key), EVP_sha1());
// x_login^x_fp_sequence^x_fp_timestamp^x_amount^x_currency
datalen = sprintf((char *)data, “%s^%s^%s^%s^%s”, “WSP-ACTIV-70”, “123”, “1228774539”, “100.00”, “”);
HMAC_Update(&ctx, data, datalen);
HMAC_Final(&ctx, result, &resultlen);
p = (char *)hex_result;
for(i=0; i < resultlen; i++) {
p += sprintf(p, “%02x”, result[i]);
}
p = ‘\0’;
printf(“The hmac hash is %s\n”, hex_result);
HMAC_cleanup(&ctx);
return 0;
}
To generate x_fp_hash using C# please see the code below:
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
using System;
using System.Security;
using System.Security.Cryptography;
using System.Text;
class CalculateHash {
static void Main() {
StringBuilder sb = new StringBuilder();
// x_login^x_fp_sequence^x_fp_timestamp^x_amount^x_currency
String x_login = “WSP-ACTIV-70”;
String x_fp_sequence = “123”;
String x_fp_timestamp = “1228774539”;
String x_amount = “100.00”;
String x_currency = “”; // default empty
sb.Append(x_login)
.Append(“^”)
.Append(x_fp_sequence)
.Append(“^”)
.Append(x_fp_timestamp)
.Append(“^”)
.Append(x_amount)
.Append(“^”)
.Append(x_currency);
// Convert string to array of bytes.
byte data = Encoding.UTF8.GetBytes(sb.ToString());
// key
byte key = Encoding.UTF8.GetBytes(“V0WX5fK~o6eEhr7hbs3ZeyxS”);
// Create HMAC-MD5 Algorithm;
// HMACMD5 hmac = new HMACMD5(key);
// Create HMAC-SHA1 Algorithm;
HMACSHA1 hmac = new HMACSHA1(key);
// Compute hash.
byte hashBytes = hmac.ComputeHash(data);
// Convert to HEX string.
String x_fp_hash = System.BitConverter.ToString(hashBytes).Replace(“-”, “”);
String msg = String.Format(“x_login = {0}, x_fp_sequence = {1}, x_fp_timestamp = {2}, x_amount = {3}, x_currency= {4}.\n x_fp_hash = {5}”, x_login, x_fp_sequence, x_fp_timestamp, x_amount, x_currency, x_fp_hash);
System.Console.WriteLine(msg);
}
}
To generate x_fp_hash using Python please see the code below:
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
import hmac
import hashlib
Instantiate hmac with Transaction key (HMAC-MD5)
digest_maker = hmac.new(‘V0WX5fK~o6eEhr7hbs3ZeyxS’, ‘’, hashlib.md5)
Instantiate hmac with Transaction key (HMAC-SHA1)
digest_maker = hmac.new(‘V0WX5fK~o6eEhr7hbs3ZeyxS’, ‘’, hashlib.sha1)
format = ‘%(x_login)s^%(x_fp_sequence)s^%(x_fp_timestamp)s^%(x_amount)s^%(x_currency)s’
data = format % {‘x_login’ : ‘WSP-ACTIV-70’,
‘x_fp_sequence’ : ‘123’,
‘x_fp_timestamp’ : ‘1228774539’,
‘x_amount’ : ‘100.00’,
‘x_currency’ : ‘’}
digest_maker.update(data)
x_fp_hash = digest_maker.hexdigest()
print x_fp_hash
To generate x_fp_hash using Ocaml please see the code below:
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
(* Requres Ocaml Cryptokit http://caml.inria.fr/distrib/bazar-ocaml/cryptokit-1.3.tar.gz *)
(* then invoke as ‘ocaml unix.cma nums.cma cryptokit.cma calculate_hash.ml’ *)
open Printf;;
open Cryptokit;;
(* HMAC MD5
let calculate_hash hmac_key hmac_data =
(transform_string (Hexa.encode()) (hash_string (MAC.hmac_md5 hmac_key) hmac_data));;
*)
(* HMAC SHA1 *)
let calculate_hash hmac_key hmac_data =
(transform_string (Hexa.encode()) (hash_string (MAC.hmac_sha1 hmac_key) hmac_data));;
(* HMAC Key *)
let key = “V0WX5fK~o6eEhr7hbs3ZeyxS”;;
let data = Printf.sprintf “%s^%s^%s^%s^%s” “WSP-ACTIV-70” “123” “1228774539” “100.00” “”;;
Printf.printf “The data to hash is: %s\n” data;;
Printf.printf “The hash is: %s\n” (calculate_hash key data);;
To generate x_fp_hash using ColdFusion please see the code below:
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
<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>
<cfset x_fp_hash = java_hmac(x_transaction_key, hmac_data)>
Hash obtained
#x_fp_hash#
<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”) />
<cfset key = key.init(jKey,“HmacSHA1”) />
<cfset mac.init(key) />
<cfset mac.update(jMsg) />
<cfreturn LCase(BinaryEncode(mac.doFinal(), ‘Hex’)) />