From e013788e9674b91141a4a96354c536f9504881c9 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 | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/model/src/main/kotlin/HashAlgorithm.kt b/model/src/main/kotlin/HashAlgorithm.kt index e03927dc32fa8..f07bbb270a701 100644 --- a/model/src/main/kotlin/HashAlgorithm.kt +++ b/model/src/main/kotlin/HashAlgorithm.kt @@ -113,18 +113,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. */