#2124 how to calculate HmacSHA512?

Akcelisto Wed 3 Apr 2013

I try:

Str signByHmacSha512(Str data){
  secret := "70a27a76ae26721"
  mac := Mac.getInstance("HmacSHA512")
  key := SecretKeySpec(getBytes(secret),"HmacSHA512")
  mac.init(key)
  // here method 'toHex' throws UnsupportedErr
  return toBufFromBytes(mac.doFinal(getBytes(data))).toHex
}

private static Obj getBytes(Str str){
  Interop.toJava(str.toBuf).array
}

private static Buf toBufFromBytes(Obj arrByte){
  Interop.toFan(ByteBuffer.wrap(arrByte))
}

Throws:

sys::UnsupportedErr
  fan.sys.NioBuf.toHex (NioBuf.java:208)
  ...

How to workaround this error? How to convert java byte[] to hex string? How to calculate HmacSHA512?

brian Thu 4 Apr 2013

You don't need to use Java FFI, it is all built into Buf:

fansh> "foo".toBuf.hmac("SHA-512", "secret".toBuf).toHex

Login or Signup to reply.