Skip to content

Commit

Permalink
refactor: Use Kotlin's Base64-encoding
Browse files Browse the repository at this point in the history
Use Kotlin's built-in functionality as much as possible for portability.

Signed-off-by: Sebastian Schuberth <[email protected]>
  • Loading branch information
sschuberth committed Feb 20, 2024
1 parent c893f12 commit 16b4660
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 7 deletions.
1 change: 1 addition & 0 deletions buildSrc/src/main/kotlin/ort-kotlin-conventions.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ tasks.withType<KotlinCompile>().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")
Expand Down
7 changes: 3 additions & 4 deletions clients/fossid-webapp/src/main/kotlin/Extensions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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.
Expand Down Expand Up @@ -260,7 +259,7 @@ suspend fun FossIdRestService.listSnippets(
scanCode: String,
path: String
): PolymorphicResponseBody<Snippet> {
val base64Path = base64Encoder.encodeToString(path.toByteArray())
val base64Path = Base64.encode(path.toByteArray())
return listSnippets(
PostRequestBody(
"get_fossid_results",
Expand Down
7 changes: 4 additions & 3 deletions model/src/main/kotlin/Hash.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand All @@ -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.
Expand Down

0 comments on commit 16b4660

Please sign in to comment.