Example of HMAC Computing

Computing String HMAC

This function accepts a string, a password, a hash function Component ID, and returns the string's HMAC. The provided hash function is used for both HMAC computing and password conversion.

public static byte[] ComputeStringHmac(String str,
                                       String password,
                                       UUID cidHashFunc) throws Exception {
  IKryptelComponent hmacComp = Loader.CreateComponent(CID_HMAC);
  IMacSetup hmacSetup = (IMacSetup)hmacComp.GetInterface(IID_IMacSetup);

  KeyRecord kr = ApiHelpers.PasswordToKeyRecord(password, cidHashFunc);
  hmacSetup.SetKey(kr.keyData, 0, kr.keyData.length);

  IMemoryBlockHash hmacMem = (IMemoryBlockHash)hmacComp.GetInterface(IID_IMemoryBlockHash);
  return hmacMem.HashWideString(str);

Interfaces used: IMacSetup, IMemoryBlockHash.

More about components: Components, Loader class, IKryptelComponent interface.

See Also: ApiHelpers class, KeyRecord structure.