From 70bf2827905b07d2abb465ff76070ca492de597d Mon Sep 17 00:00:00 2001 From: LaniusTrolling Date: Sun, 11 May 2025 15:38:51 -0400 Subject: [PATCH] Code style fixes 3 --- .../info/mechyrdia/auth/PasswordHashers.kt | 12 ++--- .../info/mechyrdia/lore/AssetHashing.kt | 45 +------------------ .../info/mechyrdia/robot/RobotService.kt | 4 +- .../info/mechyrdia/route/ResourceHandler.kt | 2 +- 4 files changed, 8 insertions(+), 55 deletions(-) diff --git a/src/main/kotlin/info/mechyrdia/auth/PasswordHashers.kt b/src/main/kotlin/info/mechyrdia/auth/PasswordHashers.kt index 5381c44..8aac010 100644 --- a/src/main/kotlin/info/mechyrdia/auth/PasswordHashers.kt +++ b/src/main/kotlin/info/mechyrdia/auth/PasswordHashers.kt @@ -3,22 +3,18 @@ package info.mechyrdia.auth import de.mkammerer.argon2.* import info.mechyrdia.* -private val argon2Instance = Argon2Factory.createAdvanced() - object Argon2Hasher { const val ITERATIONS = 3 const val MEMORY = 22 const val PARALLELISM = 1 + private val instance = Argon2Factory.create() + fun createHash(plaintext: String): String { - return Instance.hash(ITERATIONS, MEMORY, PARALLELISM, plaintext.toByteArray(Utf8)) + return instance.hash(ITERATIONS, MEMORY, PARALLELISM, plaintext.toByteArray(Utf8)) } fun verifyHash(hash: String, attempt: String): Boolean { - return Instance.verify(hash, attempt.toByteArray(Utf8)) + return instance.verify(hash, attempt.toByteArray(Utf8)) } - - object Instance : Argon2 by argon2Instance - - object Advanced : Argon2Advanced by argon2Instance } diff --git a/src/main/kotlin/info/mechyrdia/lore/AssetHashing.kt b/src/main/kotlin/info/mechyrdia/lore/AssetHashing.kt index 185f3b5..69e5e88 100644 --- a/src/main/kotlin/info/mechyrdia/lore/AssetHashing.kt +++ b/src/main/kotlin/info/mechyrdia/lore/AssetHashing.kt @@ -5,50 +5,9 @@ import io.ktor.http.content.* import io.ktor.server.application.* import io.ktor.server.http.content.* import kotlinx.coroutines.* -import java.io.* import java.security.* import java.util.* -private class DigestingOutputStream(stomach: MessageDigest) : OutputStream() { - private var stomachStore: MessageDigest? = stomach - - private val stomach: MessageDigest - get() = stomachStore ?: throw IOException("Attempt to use DigestingOutputStream after it has been closed") - - val isWritable: Boolean - get() = stomachStore != null - - private var resultStore: ByteArray? = null - - val result: ByteArray - get() = resultStore ?: throw IOException("Attempt to retrieve result of DigestingOutputStream before it has finished") - - val isDone: Boolean - get() = resultStore != null - - override fun write(b: Int) { - stomach.update(b.toByte()) - } - - override fun write(b: ByteArray) { - stomach.update(b) - } - - override fun write(b: ByteArray, off: Int, len: Int) { - stomach.update(b, off, len) - } - - override fun close() { - resultStore = stomach.digest() - stomachStore = null - } - - inline fun useAndGet(block: (DigestingOutputStream) -> Unit): ByteArray { - use(block) - return result - } -} - private class FileHashCache(val hashAlgo: String) : FileDependentCache() { private val hashinator: ThreadLocal = ThreadLocal.withInitial { MessageDigest.getInstance(hashAlgo) } @@ -58,9 +17,7 @@ private class FileHashCache(val hashAlgo: String) : FileDependentCache - oStream.write(fileContents) - } + hashinator.get().digest(fileContents) } } } diff --git a/src/main/kotlin/info/mechyrdia/robot/RobotService.kt b/src/main/kotlin/info/mechyrdia/robot/RobotService.kt index 9d7eaeb..c9e552e 100644 --- a/src/main/kotlin/info/mechyrdia/robot/RobotService.kt +++ b/src/main/kotlin/info/mechyrdia/robot/RobotService.kt @@ -134,7 +134,7 @@ class RobotService( private suspend fun updateFiles(prevGlobals: RobotGlobals?, onNewFileId: (suspend (RobotFileId) -> Unit)? = null): RobotGlobals { val robotGlobals = prevGlobals ?: RobotGlobals() - val fileIdMap = buildMap { + val fileIdMap = buildMap { putAll(robotGlobals.fileIdMap) val factbooks = robotGlobals.lastFileUpload?.let { @@ -190,7 +190,7 @@ class RobotService( RobotServiceLogger.info("Vector store creation is complete") if (robotGlobals.assistantId == null) - robotGlobals = robotGlobals.copy( + robotGlobals.copy( assistantId = robotClient.createAssistant( RobotCreateAssistantRequest( model = config.assistantModel, diff --git a/src/main/kotlin/info/mechyrdia/route/ResourceHandler.kt b/src/main/kotlin/info/mechyrdia/route/ResourceHandler.kt index dafa43b..efbcc85 100644 --- a/src/main/kotlin/info/mechyrdia/route/ResourceHandler.kt +++ b/src/main/kotlin/info/mechyrdia/route/ResourceHandler.kt @@ -3,7 +3,6 @@ package info.mechyrdia.route import io.ktor.http.* import io.ktor.resources.serialization.* import io.ktor.server.application.* -import io.ktor.server.application.call import io.ktor.server.application.hooks.* import io.ktor.server.plugins.* import io.ktor.server.request.* @@ -57,6 +56,7 @@ inline fun , reified P : MultiPartPayload> Route val WebSocketResourceInstanceKey: AttributeKey = AttributeKey("WebSocketResourceInstance") +@Suppress("FunctionName") inline fun WebSocketResourcePlugin() = createRouteScopedPlugin("WebSocketResourcePlugin") { val serializer = serializer() on(CallSetup) { call -> -- 2.25.1