Skip to content

Commit

Permalink
refactor: Use hex coding from Kotlin's stdlib
Browse files Browse the repository at this point in the history
Reduce custom code.

Signed-off-by: Sebastian Schuberth <sebastian@doubleopen.org>
  • Loading branch information
sschuberth committed Aug 22, 2024
1 parent 0d3b21e commit 5228030
Show file tree
Hide file tree
Showing 9 changed files with 10 additions and 30 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 @@ -174,6 +174,7 @@ tasks.withType<KotlinCompile>().configureEach {
val hasSerializationPlugin = plugins.hasPlugin(libs.plugins.kotlinSerialization.get().pluginId)

val optInRequirements = listOfNotNull(
"kotlin.ExperimentalStdlibApi",
"kotlin.contracts.ExperimentalContracts",
"kotlin.io.encoding.ExperimentalEncodingApi",
"kotlin.io.path.ExperimentalPathApi",
Expand Down
7 changes: 2 additions & 5 deletions model/src/main/kotlin/Hash.kt
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,6 @@ import java.io.File

import kotlin.io.encoding.Base64

import org.ossreviewtoolkit.utils.common.decodeHex
import org.ossreviewtoolkit.utils.common.encodeHex

/**
* A class that bundles a hash algorithm with its hash value.
*/
Expand Down Expand Up @@ -60,7 +57,7 @@ data class Hash(
// Support Subresource Integrity (SRI) hashes, see
// https://w3c.github.io/webappsec-subresource-integrity/
Hash(
value = Base64.decode(splitValue.last()).encodeHex(),
value = Base64.decode(splitValue.last()).toHexString(),
algorithm = HashAlgorithm.fromString(splitValue.first())
)
} else {
Expand All @@ -83,7 +80,7 @@ data class Hash(
/**
* Return the hash in Support Subresource Integrity (SRI) format.
*/
fun toSri() = algorithm.name.lowercase() + "-" + Base64.encode(value.decodeHex())
fun toSri() = algorithm.name.lowercase() + "-" + Base64.encode(value.hexToByteArray())

/**
* Verify that the [file] matches this hash.
Expand Down
3 changes: 1 addition & 2 deletions model/src/main/kotlin/HashAlgorithm.kt
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ import java.io.InputStream
import java.security.MessageDigest

import org.ossreviewtoolkit.utils.common.calculateHash
import org.ossreviewtoolkit.utils.common.encodeHex

/**
* An enum of supported hash algorithms. Each algorithm has one or more [aliases] associated to it, where the first
Expand Down Expand Up @@ -159,5 +158,5 @@ enum class HashAlgorithm(vararg val aliases: String, val emptyValue: String, val
* closing the stream.
*/
private fun calculate(inputStream: InputStream, size: Long): String =
calculateHash(inputStream, getMessageDigest(size)).encodeHex()
calculateHash(inputStream, getMessageDigest(size)).toHexString()
}
3 changes: 1 addition & 2 deletions plugins/package-managers/bazel/src/main/kotlin/Bazel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ import org.ossreviewtoolkit.model.orEmpty
import org.ossreviewtoolkit.utils.common.CommandLineTool
import org.ossreviewtoolkit.utils.common.ProcessCapture
import org.ossreviewtoolkit.utils.common.collectMessages
import org.ossreviewtoolkit.utils.common.encodeHex
import org.ossreviewtoolkit.utils.common.withoutPrefix
import org.ossreviewtoolkit.utils.ort.createOrtTempFile
import org.ossreviewtoolkit.utils.ort.runBlocking
Expand Down Expand Up @@ -310,7 +309,7 @@ private fun ModuleMetadata.toVcsInfo() =

private fun ModuleSourceInfo.toRemoteArtifact(): RemoteArtifact {
val (algo, b64digest) = integrity.split("-", limit = 2)
val digest = Base64.getDecoder().decode(b64digest).encodeHex()
val digest = Base64.getDecoder().decode(b64digest).toHexString()

val hash = Hash(
value = digest,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ import java.net.URI
import org.ossreviewtoolkit.model.Issue
import org.ossreviewtoolkit.model.Severity
import org.ossreviewtoolkit.utils.common.calculateHash
import org.ossreviewtoolkit.utils.common.encodeHex
import org.ossreviewtoolkit.utils.spdx.SpdxModelMapper
import org.ossreviewtoolkit.utils.spdx.model.SpdxChecksum
import org.ossreviewtoolkit.utils.spdx.model.SpdxDocument
Expand Down Expand Up @@ -660,6 +659,6 @@ private fun File.toExternalReference(index: Int): SpdxExternalDocumentReference
return SpdxExternalDocumentReference(
externalDocumentId = referenceId(index),
spdxDocument = name,
checksum = SpdxChecksum(SpdxChecksum.Algorithm.SHA1, hash.encodeHex())
checksum = SpdxChecksum(SpdxChecksum.Algorithm.SHA1, hash.toHexString())
)
}
13 changes: 0 additions & 13 deletions utils/common/src/main/kotlin/Extensions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,6 @@ import java.util.Locale
*/
inline fun <T> T.alsoIfNull(block: (T) -> Unit): T = this ?: also(block)

/**
* Return a string of lowercase hexadecimal digits representing the bytes in the array.
*/
fun ByteArray.encodeHex(): String = joinToString("") { String.format(Locale.ROOT, "%02x", it) }

/**
* Return a map that associates duplicates as identified by [keySelector] with belonging lists of collection entries.
*/
Expand Down Expand Up @@ -303,14 +298,6 @@ fun String.collapseWhitespace() = trim().replace(CONSECUTIVE_WHITESPACE_REGEX, "

private val CONSECUTIVE_WHITESPACE_REGEX = Regex("\\s+")

/**
* Decode a hex-string and return the value as a [ByteArray].
*/
fun String.decodeHex(): ByteArray {
require(length % 2 == 0) { "The string must have an even number of characters." }
return chunked(2).map { it.toInt(16).toByte() }.toByteArray()
}

/**
* Return the string encoded for safe use as a file name or [emptyValue] encoded for safe use as a file name, if this
* string is empty. Throws an exception if [emptyValue] is empty.
Expand Down
2 changes: 1 addition & 1 deletion utils/common/src/test/kotlin/ExtensionsTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ import java.util.Locale
class ExtensionsTest : WordSpec({
"ByteArray.toHexString()" should {
"correctly convert a byte array to a string of hexadecimal digits" {
byteArrayOf(0xde.toByte(), 0xad.toByte(), 0xbe.toByte(), 0xef.toByte()).encodeHex() shouldBe "deadbeef"
byteArrayOf(0xde.toByte(), 0xad.toByte(), 0xbe.toByte(), 0xef.toByte()).toHexString() shouldBe "deadbeef"
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ import kotlin.script.experimental.jvm.dependenciesFromCurrentContext
import kotlin.script.experimental.jvm.jvm
import kotlin.script.experimental.jvmhost.CompiledScriptJarsCache

import org.ossreviewtoolkit.utils.common.encodeHex
import org.ossreviewtoolkit.utils.common.safeMkdirs
import org.ossreviewtoolkit.utils.ort.Environment
import org.ossreviewtoolkit.utils.ort.ortDataDirectory
Expand Down Expand Up @@ -90,5 +89,5 @@ private fun generateUniqueName(script: SourceCode, configuration: ScriptCompilat
digest.update(it.value.toString().toByteArray())
}

return digest.digest().encodeHex()
return digest.digest().toHexString()
}
5 changes: 2 additions & 3 deletions utils/spdx/src/main/kotlin/Utils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ import org.ossreviewtoolkit.utils.common.Os
import org.ossreviewtoolkit.utils.common.PATH_STRING_COMPARATOR
import org.ossreviewtoolkit.utils.common.VCS_DIRECTORIES
import org.ossreviewtoolkit.utils.common.calculateHash
import org.ossreviewtoolkit.utils.common.encodeHex
import org.ossreviewtoolkit.utils.common.isSymbolicLink
import org.ossreviewtoolkit.utils.common.realFile
import org.ossreviewtoolkit.utils.spdx.SpdxConstants.LICENSE_REF_PREFIX
Expand Down Expand Up @@ -62,7 +61,7 @@ val scanCodeLicenseTextDir by lazy {
fun calculatePackageVerificationCode(sha1sums: Sequence<String>, excludes: Sequence<String> = emptySequence()): String {
val sha1sum = sha1sums.sorted().fold(MessageDigest.getInstance("SHA-1")) { digest, sha1sum ->
digest.apply { update(sha1sum.toByteArray()) }
}.digest().encodeHex()
}.digest().toHexString()

return if (excludes.none()) {
sha1sum
Expand All @@ -78,7 +77,7 @@ fun calculatePackageVerificationCode(sha1sums: Sequence<String>, excludes: Seque
*/
@JvmName("calculatePackageVerificationCodeForFiles")
fun calculatePackageVerificationCode(files: Sequence<File>, excludes: Sequence<String> = emptySequence()): String =
calculatePackageVerificationCode(files.map { calculateHash(it).encodeHex() }, excludes)
calculatePackageVerificationCode(files.map { calculateHash(it).toHexString() }, excludes)

/**
* Calculate the [SPDX package verification code][1] for all files in a [directory]. If [directory] points to a file
Expand Down

0 comments on commit 5228030

Please sign in to comment.