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

  const tag = lineTag(line.slice(0, -2));
  const bodyWithTilde = line[line.length - 2] === '~' ? line : `${line}~`;

  if (bodyWithTilde[bodyWithTilde.length - 1] !== 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) % 256;
  return sum.toString(16).padStart(2, '0');
}
```