import struct


def _compute_dc42_checksum(data):
  """Compute checksum DC42 uses to verify sector and tag data integrity.

  Args:
    data: data to compute a checksum for.

  Returns: a 32-bit (big endian) checksum as a 2-byte string.
  """

  def addl_rorl(uint, csum):
    """Add `uint` to `csum`; 32-bit truncate; 32-bit rotate right one bit."""
    csum += uint        # add uint
    csum &= 0xffffffff  # truncate
    rbit = csum & 0x1   # rotate part 1 (save low-order bit)
    csum >>= 1          # rotate part 2 (shift right)
    csum += rbit << 31  # rotate part 3 (prepend old low-order bit)
    return csum

  # Loop over all two-byte words in the data and include them in the checksum.
  checksum = 0
  for word_bytes in [data[i:i+2] for i in range(0, len(data), 2)]:
    word = struct.unpack('>H', word_bytes)[0]  # big endian word bytes to native
    checksum = addl_rorl(word, checksum)       # add to checksum

  # return result as a big-endian 32-bit word.
  return struct.pack('>I', checksum)