Не подтверждена Коммит 37c67245 создал по автору Florian Schäfer's avatar Florian Schäfer Зафиксировано автором GitHub
Просмотр файлов

Decode URL safe base64 correctly (#4721)



* Add unit test for encoding/decoding a string containing all base64 characters

This also checks decoding of URL-safe base64 strings, which currently does not work, but will be changed in a separate commit.

* Change base64 decoder, so it can also handle URL-safe base64 strings

* Fix code formatting using the `formatKotlin` task

---------

Co-authored-by: default avatarBruce Hamilton <150327496+bjhham@users.noreply.github.com>
владелец acbb25d1
......@@ -15,6 +15,10 @@ private const val BASE64_PAD = '='
private val BASE64_INVERSE_ALPHABET = IntArray(256) {
BASE64_ALPHABET.indexOf(it.toChar())
}.also {
// correctly decode URL-safe Base64
it['-'.code] = it['+'.code]
it['_'.code] = it['/'.code]
}
/**
......
......@@ -51,4 +51,22 @@ class Base64Test {
assertEquals(text, encodedText.decodeBase64String())
}
}
@Test
fun testEncodeDecodeAllCharacters() {
val commonChars: List<Char> = ('a'..'z') + ('A'..'Z') + ('0'..'9')
val allBase64Chars: String = (commonChars + '+' + '/').joinToString("")
val allUrlSafeBase64Chars: String = (commonChars + '-' + '_').joinToString("")
val expectedBytes: List<Byte> = listOf(
105, -73, 29, 121, -8, 33, -118, 57, 37, -102, 122, 41, -86, -69, 45, -70, -4, 49, -53, 48, 1, 8, 49, 5,
24, 114, 9, 40, -77, 13, 56, -12, 17, 73, 53, 21, 89, 118, 25, -45, 93, -73, -29, -98, -69, -13, -33, -65,
)
// Check encode
assertEquals(allBase64Chars, expectedBytes.toByteArray().encodeBase64())
// Check decode
assertEquals(expectedBytes, allBase64Chars.decodeBase64Bytes().toList())
assertEquals(expectedBytes, allUrlSafeBase64Chars.decodeBase64Bytes().toList())
}
}
Поддерживает Markdown
0% или .
You are about to add 0 people to the discussion. Proceed with caution.
Сначала завершите редактирование этого сообщения!
Пожалуйста, зарегистрируйтесь или чтобы прокомментировать