From 76a2057ef2c853c1717e2946562e872a0307026f Mon Sep 17 00:00:00 2001 From: Sebastian Schuberth Date: Tue, 20 Feb 2024 15:02:03 +0100 Subject: [PATCH] refactor: Use Kotlin's Base64-encoding Use Kotlin's built-in functionality as much as possible for portability. Signed-off-by: Sebastian Schuberth --- .../kotlin/ort-kotlin-conventions.gradle.kts | 1 + .../src/main/kotlin/Extensions.kt | 19 +++++++++---------- model/src/main/kotlin/Hash.kt | 7 ++++--- 3 files changed, 14 insertions(+), 13 deletions(-) diff --git a/buildSrc/src/main/kotlin/ort-kotlin-conventions.gradle.kts b/buildSrc/src/main/kotlin/ort-kotlin-conventions.gradle.kts index 2e0165e75a8ff..07fe1fe6b4aa5 100644 --- a/buildSrc/src/main/kotlin/ort-kotlin-conventions.gradle.kts +++ b/buildSrc/src/main/kotlin/ort-kotlin-conventions.gradle.kts @@ -161,6 +161,7 @@ tasks.withType().configureEach { val customCompilerArgs = buildList { add("-opt-in=kotlin.contracts.ExperimentalContracts") + add("-opt-in=kotlin.io.encoding.ExperimentalEncodingApi") add("-opt-in=kotlin.io.path.ExperimentalPathApi") add("-opt-in=kotlin.time.ExperimentalTime") if (hasSerialization) add("-opt-in=kotlinx.serialization.ExperimentalSerializationApi") diff --git a/clients/fossid-webapp/src/main/kotlin/Extensions.kt b/clients/fossid-webapp/src/main/kotlin/Extensions.kt index 2c22a7d4a3998..54fd0a0f6a53b 100644 --- a/clients/fossid-webapp/src/main/kotlin/Extensions.kt +++ b/clients/fossid-webapp/src/main/kotlin/Extensions.kt @@ -22,7 +22,8 @@ package org.ossreviewtoolkit.clients.fossid import java.io.File -import java.util.Base64 + +import kotlin.io.encoding.Base64 import okio.buffer import okio.sink @@ -41,8 +42,6 @@ internal const val SCAN_GROUP = "scans" private const val FILES_AND_FOLDERS_GROUP = "files_and_folders" private const val PROJECT_GROUP = "projects" -private val base64Encoder = Base64.getEncoder() - /** * Verify that a request for the given [operation] was successful. [operation] is a free label describing the operation. * If [withDataCheck] is true, also the payload data is checked, otherwise that check is skipped. @@ -260,7 +259,7 @@ suspend fun FossIdRestService.listSnippets( scanCode: String, path: String ): PolymorphicResponseBody { - val base64Path = base64Encoder.encodeToString(path.toByteArray()) + val base64Path = Base64.encode(path.toByteArray()) return listSnippets( PostRequestBody( "get_fossid_results", @@ -285,7 +284,7 @@ suspend fun FossIdRestService.listMatchedLines( path: String, snippetId: Int ): EntityResponseBody { - val base64Path = base64Encoder.encodeToString(path.toByteArray()) + val base64Path = Base64.encode(path.toByteArray()) return listMatchedLines( PostRequestBody( "get_matched_lines", @@ -463,7 +462,7 @@ suspend fun FossIdRestService.markAsIdentified( path: String, isDirectory: Boolean ): EntityResponseBody { - val base64Path = base64Encoder.encodeToString(path.toByteArray()) + val base64Path = Base64.encode(path.toByteArray()) val directoryFlag = if (isDirectory) "1" else "0" return markAsIdentified( PostRequestBody( @@ -488,7 +487,7 @@ suspend fun FossIdRestService.unmarkAsIdentified( path: String, isDirectory: Boolean ): EntityResponseBody { - val base64Path = base64Encoder.encodeToString(path.toByteArray()) + val base64Path = Base64.encode(path.toByteArray()) val directoryFlag = if (isDirectory) "1" else "0" return unmarkAsIdentified( PostRequestBody( @@ -515,7 +514,7 @@ suspend fun FossIdRestService.addLicenseIdentification( identificationOn: LicenseMatchType, isDirectory: Boolean ): EntityResponseBody { - val base64Path = base64Encoder.encodeToString(path.toByteArray()) + val base64Path = Base64.encode(path.toByteArray()) val directoryFlag = if (isDirectory) "1" else "0" return addLicenseIdentification( PostRequestBody( @@ -552,7 +551,7 @@ suspend fun FossIdRestService.addComponentIdentification( isDirectory: Boolean, preserveExistingIdentifications: Boolean = true ): EntityResponseBody { - val base64Path = base64Encoder.encodeToString(path.toByteArray()) + val base64Path = Base64.encode(path.toByteArray()) val directoryFlag = if (isDirectory) "1" else "0" val preserveExistingIdentificationsFlag = if (preserveExistingIdentifications) "1" else "0" return addComponentIdentification( @@ -587,7 +586,7 @@ suspend fun FossIdRestService.addFileComment( isImportant: Boolean = false, includeInReport: Boolean = false ): EntityResponseBody { - val base64Path = base64Encoder.encodeToString(path.toByteArray()) + val base64Path = Base64.encode(path.toByteArray()) val isImportantFlag = if (isImportant) "1" else "0" val includeInReportFlag = if (includeInReport) "1" else "0" return addFileComment( diff --git a/model/src/main/kotlin/Hash.kt b/model/src/main/kotlin/Hash.kt index f529067dc6a64..4f5b6df55b2d2 100644 --- a/model/src/main/kotlin/Hash.kt +++ b/model/src/main/kotlin/Hash.kt @@ -23,7 +23,8 @@ import com.fasterxml.jackson.databind.annotation.JsonSerialize import com.fasterxml.jackson.databind.util.StdConverter import java.io.File -import java.util.Base64 + +import kotlin.io.encoding.Base64 import org.ossreviewtoolkit.utils.common.decodeHex import org.ossreviewtoolkit.utils.common.encodeHex @@ -59,7 +60,7 @@ data class Hash( // Support Subresource Integrity (SRI) hashes, see // https://w3c.github.io/webappsec-subresource-integrity/ Hash( - value = Base64.getDecoder().decode(splitValue.last()).encodeHex(), + value = Base64.decode(splitValue.last()).encodeHex(), algorithm = HashAlgorithm.fromString(splitValue.first()) ) } else { @@ -82,7 +83,7 @@ data class Hash( /** * Return the hash in Support Subresource Integrity (SRI) format. */ - fun toSri() = algorithm.name.lowercase() + "-" + Base64.getEncoder().encodeToString(value.decodeHex()) + fun toSri() = algorithm.name.lowercase() + "-" + Base64.encode(value.decodeHex()) /** * Verify that the [file] matches this hash.