/** * Calculate the MAC for some given data. * * @param type The message type of the message. * @param message A byte-buffer containing the message. * @param offset The number of bytes to skip, before the message starts. * @param length The length of the message. * @return A new byte-buffer containing the MAC value. */ public byte[] calculateMac(long seqNo, short type, byte[] message, int offset, int length) { ProtocolVersion serverVersion = context.getServerVersion(); boolean isSSL = serverVersion.isSSL(); byte[] macHeader = new byte[isSSL ? 11 : 13]; TlsUtils.writeUint64(seqNo, macHeader, 0); TlsUtils.writeUint8(type, macHeader, 8); if (!isSSL) { TlsUtils.writeVersion(serverVersion, macHeader, 9); } TlsUtils.writeUint16(length, macHeader, macHeader.length - 2); mac.update(macHeader, 0, macHeader.length); mac.update(message, offset, length); byte[] result = new byte[mac.getMacSize()]; mac.doFinal(result, 0); return truncate(result); }
/** * Calculate the MAC for some given data. * * @param type The message type of the message. * @param message A byte-buffer containing the message. * @param offset The number of bytes to skip, before the message starts. * @param length The length of the message. * @return A new byte-buffer containing the MAC value. */ public byte[] calculateMac(long seqNo, short type, byte[] message, int offset, int length) { ProtocolVersion serverVersion = context.getServerVersion(); boolean isSSL = serverVersion.isSSL(); byte[] macHeader = new byte[isSSL ? 11 : 13]; TlsUtils.writeUint64(seqNo, macHeader, 0); TlsUtils.writeUint8(type, macHeader, 8); if (!isSSL) { TlsUtils.writeVersion(serverVersion, macHeader, 9); } TlsUtils.writeUint16(length, macHeader, macHeader.length - 2); mac.update(macHeader, 0, macHeader.length); mac.update(message, offset, length); byte[] result = new byte[mac.getMacSize()]; mac.doFinal(result, 0); return truncate(result); }