Skip to content

Commit

Permalink
refactor(model): Teach hash algorithms about their value string size
Browse files Browse the repository at this point in the history
This allows to simply code a bit and to implement consistency checks for
string lengths.

Signed-off-by: Sebastian Schuberth <[email protected]>
  • Loading branch information
sschuberth committed Jul 18, 2024
1 parent efed39f commit fca2577
Showing 1 changed file with 7 additions and 9 deletions.
16 changes: 7 additions & 9 deletions model/src/main/kotlin/HashAlgorithm.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
package org.ossreviewtoolkit.model

import com.fasterxml.jackson.annotation.JsonCreator
import com.fasterxml.jackson.annotation.JsonIgnore

Check warning on line 23 in model/src/main/kotlin/HashAlgorithm.kt

View workflow job for this annotation

GitHub Actions / qodana-scan

Unused import directive

Unused import directive

Check warning

Code scanning / detekt

Unused Imports are dead code and should be removed. Warning

The import 'com.fasterxml.jackson.annotation.JsonIgnore' is unused.
import com.fasterxml.jackson.annotation.JsonValue

import java.io.File
Expand Down Expand Up @@ -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.
*/
Expand Down

0 comments on commit fca2577

Please sign in to comment.