```ts
export function checkLine(line: string): string {
  if (typeof line !== 'string' || line.length < 3) {
    throw new Error('Invalid line');
  }

  const tag = lineTag(line);
  const [thirdFromLast, last] = [...line].slice(-2);
  const hasTilde = thirdFromLast === '~';

  if (!hasTilde || last !== tag) {
    throw new Error('Invalid line');
  }

  return line.slice(0, -3);
}

export function lineTag(body: string): string {
  const sum = body.split('').reduce(
    (acc, char) => acc + char.charCodeAt(0),
    0
  );
  return sum.toString(16).padStart(2, '0');
}
```