computeMAC function

Uint8List computeMAC(
  1. HMac mac,
  2. int macLen,
  3. Uint8List m,
  4. int seq,
  5. Uint8List k,
  6. int prefix
)

Signs seq | m with k using mac.

Implementation

Uint8List computeMAC(
    HMac mac, int macLen, Uint8List m, int seq, Uint8List k, int prefix) {
  mac.init(KeyParameter(k));

  Uint8List buf = Uint8List(4);
  ByteData.view(buf.buffer).setUint32(0, seq, Endian.big);
  mac.update(buf, 0, buf.length);
  mac.update(m, 0, m.length);

  if (macLen != mac.macSize) throw FormatException();
  Uint8List ret = Uint8List(macLen);
  int finalLen = mac.doFinal(ret, 0);
  if (finalLen != macLen) throw FormatException();

  if (prefix != 0) {
    return viewUint8List(ret, 0, prefix);
  } else {
    return ret;
  }
}