public byte[] calculateMac(ProtocolVersion protocolVersion,ProtocolMessageType contentType,byte[] data){
  byte[] SQN=ArrayConverter.longToUint64Bytes(sequenceNumber);
  byte[] HDR=ArrayConverter.concatenate(contentType.getArrayValue(),protocolVersion.getValue(),ArrayConverter.intToBytes(data.length,2));
  writeMac.update(SQN);
  writeMac.update(HDR);
  writeMac.update(data);
  LOGGER.debug("The MAC was caluculated over the following data: {}",ArrayConverter.bytesToHexString(ArrayConverter.concatenate(SQN,HDR,data)));
  byte[] result=writeMac.doFinal();
  LOGGER.debug("MAC result: {}",ArrayConverter.bytesToHexString(result));
  sequenceNumber++;
  return result;
}

public byte [ ] calculateMac ( ProtocolVersion protocolVersion , ProtocolMessageType contentType , byte [ ] data ) { byte [ ] SQN = ArrayConverter . longToUint64Bytes ( sequenceNumber ) ; byte [ ] HDR = ArrayConverter . concatenate ( contentType . getArrayValue ( ) , protocolVersion . getValue ( ) , ArrayConverter . intToBytes ( data . length , 2 ) ) ; writeMac . update ( SQN ) ; writeMac . update ( HDR ) ; writeMac . update ( data ) ; LOGGER . debug ( "The MAC was caluculated over the following data: {}" , ArrayConverter . bytesToHexString ( ArrayConverter . concatenate ( SQN , HDR , data ) ) ) ; byte [ ] result = writeMac . doFinal ( ) ; LOGGER . debug ( "MAC result: {}" , ArrayConverter . bytesToHexString ( result ) ) ; sequenceNumber ++ ; return result ; }
from the lucky13 paper : an individual record r ( viewed as a byte sequence of length at least zero ) is processed as follows . the sender maintains an 8 - byte sequence number sqn which is incremented for each record sent , and forms a 5 - byte field hdr consisting of a 1 - byte type field , a 2 - byte version field , and a 2 - byte length field . it then calculates a mac over the bytes sqn | | hdr | | r .
