From fca25779be67cffc74b68c6b480e9d38e6001560 Mon Sep 17 00:00:00 2001 From: Sebastian Schuberth Date: Wed, 17 Jul 2024 22:49:49 +0200 Subject: [PATCH] refactor(model): Teach hash algorithms about their value string size This allows to simply code a bit and to implement consistency checks for string lengths. Signed-off-by: Sebastian Schuberth --- model/src/main/kotlin/HashAlgorithm.kt | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/model/src/main/kotlin/HashAlgorithm.kt b/model/src/main/kotlin/HashAlgorithm.kt index e03927dc32fa8..3f6e032ae57e0 100644 --- a/model/src/main/kotlin/HashAlgorithm.kt +++ b/model/src/main/kotlin/HashAlgorithm.kt @@ -20,6 +20,7 @@ package org.ossreviewtoolkit.model import com.fasterxml.jackson.annotation.JsonCreator +import com.fasterxml.jackson.annotation.JsonIgnore import com.fasterxml.jackson.annotation.JsonValue import java.io.File @@ -113,18 +114,15 @@ enum class HashAlgorithm(vararg val aliases: String, val emptyValue: String, val */ fun create(value: String): HashAlgorithm { if (value.isBlank()) return NONE - - return when (value.length) { - 128 -> SHA512 - 96 -> SHA384 - 64 -> SHA256 - 40 -> SHA1 - 32 -> MD5 - else -> UNKNOWN - } + return HashAlgorithm.entries.find { it.size == value.length } ?: UNKNOWN } } + /** + * The size of a hexadecimal hash value string for this algorithm. + */ + val size = emptyValue.length + /** * Convert the hash algorithm to a string representation. */