function padString(input: string): string {
    const segmentLength = 4;
    const diff = input.length % segmentLength;
    if (!diff) return input;
    const padLength = segmentLength - diff;
    return input + '='.repeat(padLength);
}

/**
 * 将 Base64URL 字符串转换为 Base64 字符串
 * @param base64url Base64URL 字符串
 * @returns Base64 字符串
 */
export function toBase64(base64url: string): string {
    base64url = base64url.toString();
    return padString(base64url).replace(/-/g, '+').replace(/_/g, '/');
}

/**
 * 将 Base64 字符串转换为 Base64URL 字符串
 * @param base64 Base64 字符串
 * @returns Base64URL 字符串
 */
export function fromBase64(base64: string): string {
    return base64.replace(/=/g, '').replace(/\+/g, '-').replace(/\//g, '_');
}
