1

This MD5 call has 3 arguments, "MD5", the string, and then the $transaction_key

hash_hmac("md5", $api_login_id . "^" . $fp_sequence . "^" . $fp_timestamp . "^" . $amount . "^", $transaction_key);

How would I reproduce this in Scala? I can't find an MD5 function that takes a "key".

user1491739
  • 1,017
  • 2
  • 11
  • 16
  • have a look at http://stackoverflow.com/questions/1609899/java-equivalent-to-phps-hmac-sha1 it's for Java but the library used by Scala is Java's anyway. – fvu Jul 05 '12 at 19:11

1 Answers1

9

Here's a Scala version derived from this Java answer:

def encode(message: String, key: String) = {
  val mac = javax.crypto.Mac.getInstance("HmacMD5")
  mac.init(new javax.crypto.spec.SecretKeySpec(key.getBytes, "HmacMD5"))
  mac.doFinal(message.getBytes).map("%02x".format(_)).mkString
}

It produces the same output as PHP's hash_hmac for me on a couple of quick tests.

Community
  • 1
  • 1
Travis Brown
  • 138,631
  • 12
  • 375
  • 680