From 4f8dee77ed45d97191c12a0102e2591cefed8d5b Mon Sep 17 00:00:00 2001 From: Gauthier Roebroeck Date: Mon, 26 Jun 2023 17:41:58 +0800 Subject: [PATCH] feat: use XXH128 for hashing --- komga/build.gradle.kts | 2 ++ .../migration/sqlite/V20230626150454__xxhash128.sql | 4 ++++ .../org/gotson/komga/infrastructure/hash/Hasher.kt | 13 +++++++++---- 3 files changed, 15 insertions(+), 4 deletions(-) create mode 100644 komga/src/flyway/resources/db/migration/sqlite/V20230626150454__xxhash128.sql diff --git a/komga/build.gradle.kts b/komga/build.gradle.kts index 0d783fc0b..930ace87f 100644 --- a/komga/build.gradle.kts +++ b/komga/build.gradle.kts @@ -84,6 +84,8 @@ dependencies { implementation("com.ibm.icu:icu4j:73.1") + implementation("com.appmattus.crypto:cryptohash:0.10.1") + implementation("org.apache.tika:tika-core:2.8.0") implementation("org.apache.commons:commons-compress:1.23.0") implementation("com.github.junrar:junrar:7.5.4") diff --git a/komga/src/flyway/resources/db/migration/sqlite/V20230626150454__xxhash128.sql b/komga/src/flyway/resources/db/migration/sqlite/V20230626150454__xxhash128.sql new file mode 100644 index 000000000..6c6c76fde --- /dev/null +++ b/komga/src/flyway/resources/db/migration/sqlite/V20230626150454__xxhash128.sql @@ -0,0 +1,4 @@ +delete from PAGE_HASH; +delete from PAGE_HASH_THUMBNAIL; +update BOOK set FILE_HASH = ''; +update MEDIA_PAGE set FILE_HASH = ''; diff --git a/komga/src/main/kotlin/org/gotson/komga/infrastructure/hash/Hasher.kt b/komga/src/main/kotlin/org/gotson/komga/infrastructure/hash/Hasher.kt index e8bcca94d..7f441c5a5 100644 --- a/komga/src/main/kotlin/org/gotson/komga/infrastructure/hash/Hasher.kt +++ b/komga/src/main/kotlin/org/gotson/komga/infrastructure/hash/Hasher.kt @@ -1,7 +1,7 @@ package org.gotson.komga.infrastructure.hash +import com.appmattus.crypto.Algorithm import mu.KotlinLogging -import org.apache.commons.codec.digest.XXHash32 import org.springframework.stereotype.Component import java.io.InputStream import java.nio.file.Path @@ -9,7 +9,7 @@ import kotlin.io.path.inputStream private val logger = KotlinLogging.logger {} -private const val DEFAULT_BUFFER_SIZE = 4096 +private const val DEFAULT_BUFFER_SIZE = 8192 private const val SEED = 0 @Component @@ -22,7 +22,7 @@ class Hasher { } fun computeHash(stream: InputStream): String { - val hash = XXHash32(SEED) + val hash = Algorithm.XXH3_128.Seeded(SEED.toLong()).createDigest() stream.use { val buffer = ByteArray(DEFAULT_BUFFER_SIZE) @@ -34,6 +34,11 @@ class Hasher { } while (len >= 0) } - return hash.value.toString(36) + return hash.digest().toHexString() + } + + @OptIn(ExperimentalUnsignedTypes::class) + private fun ByteArray.toHexString(): String = asUByteArray().joinToString("") { + it.toString(16).padStart(2, '0') } }