Skip to content

Commit

Permalink
Merge pull request #182 from ooni/fix-website-base64
Browse files Browse the repository at this point in the history
Fix website base64 encoding crash on iOS
  • Loading branch information
sdsantos authored Oct 15, 2024
2 parents 569ee0e + fcb4288 commit 78f51d4
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ package org.ooni.probe.shared

import kotlin.io.encoding.Base64

fun String?.encodeUrlToBase64() = Base64.UrlSafe.encode(orEmpty().encodeToByteArray())
private val BASE64 by lazy {
Base64.UrlSafe.withPadding(Base64.PaddingOption.ABSENT_OPTIONAL)
}

fun String?.decodeUrlFromBase64() = this?.ifEmpty { null }?.let { Base64.UrlSafe.decode(it).decodeToString() }
fun String?.encodeUrlToBase64() = BASE64.encode(orEmpty().encodeToByteArray())

fun String?.decodeUrlFromBase64() = this?.ifEmpty { null }?.let { BASE64.decode(it).decodeToString() }
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package org.ooni.probe.ui.shared

import org.ooni.probe.shared.decodeUrlFromBase64
import org.ooni.probe.shared.encodeUrlToBase64
import kotlin.test.Test
import kotlin.test.assertEquals

class StringExtTest {
@Test
fun base64Encoding() {
listOf(
null,
"http://example.org",
"https://ooni.io",
"https://dartcenter.org",
).forEach {
assertEquals(it, it.encodeUrlToBase64().decodeUrlFromBase64())
}
}
}

0 comments on commit 78f51d4

Please sign in to comment.