// SPDX-License-Identifier: MIT // Copyright (c) 2026 Thibault Ducray // // This file is part of MyPwdTool's open-source sync/encryption core — see // LICENSE-SYNC-CRYPTO.md at the repo root and https://tducray.fr/mypwdtool/open-source/. // The rest of this application is proprietary and NOT covered by this license. package fr.tducray.mypwdtool.relay import kotlin.io.encoding.Base64 import kotlin.io.encoding.ExperimentalEncodingApi /** * Port of the `base64urlEncode`/`base64urlDecode` free functions in * `Sync/RelayAPIClient.swift` — standard base64url without padding, same as the relay server * expects (see the E2EE Relay API spec). */ @OptIn(ExperimentalEncodingApi::class) object Base64Url { fun encode(data: ByteArray): String = Base64.UrlSafe.encode(data).trimEnd('=') fun decode(s: String): ByteArray? { val padded = s + "=".repeat((4 - s.length % 4) % 4) return try { Base64.UrlSafe.decode(padded) } catch (e: IllegalArgumentException) { null } } }