Feature #1: Validate Packet Structure

Implementing the "Validate Packet Structure" feature for our "Cyber Security" project.

Description

We use a network protocol that encrypts all packets using a proprietary scheme. The encryption scheme dictates the size of an individual packet, i.e., the packet size ranges from 1 to 4 bytes, and each packet follows a specific structure:

  • For a 1 byte packet, the first bit of the packet is 0, followed by the remaining bits of the packet’s content.
  • For an n-bytes packet, the first n bits are all 1s, the n + 1 bit is 0, followed by n - 1 bytes, with the most significant 2 bits being 10.

A sequence of packets is given as an array of integers representing the decimal values of each byte in the packets. We need to verify that the packet stream has not been tampered with.

The packet number range and its binary representation can be reviewed here:

Packet number range (hexadecimal) Octet sequence (binary)
0000 0000 - 0000 007F 0xxxxxxx
0000 0080 - 0000 07FF 110xxxxx 10xxxxxx
0000 0800 - 0000 FFFF 1110xxxx 10xxxxxx 10xxxxxx
0001 0000 - 0010 FFFF 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx

Note: The input array is in the form of integers. We only consider the 8 least significant bits of the integers used to represent the data received, which means that the integers represent only 1 byte of data.

Let’s review a few examples below:

Level up your interview prep. Join Educative to access 70+ hands-on prep courses.