feat: use XXH128 for hashing

This commit is contained in:
Gauthier Roebroeck 2023-06-26 17:41:58 +08:00
parent c70cab4a73
commit 4f8dee77ed
3 changed files with 15 additions and 4 deletions

View file

@ -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")

View file

@ -0,0 +1,4 @@
delete from PAGE_HASH;
delete from PAGE_HASH_THUMBNAIL;
update BOOK set FILE_HASH = '';
update MEDIA_PAGE set FILE_HASH = '';

View file

@ -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')
}
}