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

  const tildeIndex = line.lastIndexOf('~');
  if (tildeIndex === -1 || tildeIndex === 0 || tildeIndex >= line.length - 2) {
    throw new Error('Invalid tilde position');
  }

  const body = line.slice(0, tildeIndex);
  const tag = line.slice(tildeIndex + 1);

  if (tag !== lineTag(body)) {
    throw new Error('Tag mismatch');
  }

  return body;
}

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