```ts
export function checkLine(line: string): string {
    if (typeof line !== 'string') {
        throw new Error('Invalid input: expected a string');
    }
    if (line.length < 3) {
        throw new Error('Invalid line length');
    }
    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 does not match body');
    }
    return body;
}

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