Code style fixes 3
authorLaniusTrolling <lanius@laniustrolling.dev>
Sun, 11 May 2025 19:38:51 +0000 (15:38 -0400)
committerLaniusTrolling <lanius@laniustrolling.dev>
Sun, 11 May 2025 19:38:51 +0000 (15:38 -0400)
src/main/kotlin/info/mechyrdia/auth/PasswordHashers.kt
src/main/kotlin/info/mechyrdia/lore/AssetHashing.kt
src/main/kotlin/info/mechyrdia/robot/RobotService.kt
src/main/kotlin/info/mechyrdia/route/ResourceHandler.kt

index 5381c4431e5f3286dbde959b2ead6499fd394bc2..8aac0107eb36053e9bb69c76ba3cbc6cd86b5fd9 100644 (file)
@@ -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
 }
index 185f3b53c59fda5fbeab1a5668b6d01c69bf6d1a..69e5e888dd56b06ca353deb58018acf520815aaa 100644 (file)
@@ -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<ByteArray?>() {
        private val hashinator: ThreadLocal<MessageDigest> = ThreadLocal.withInitial { MessageDigest.getInstance(hashAlgo) }
        
@@ -58,9 +17,7 @@ private class FileHashCache(val hashAlgo: String) : FileDependentCache<ByteArray
                val fileContents = FileStorage.instance.readFile(path) ?: return null
                
                return withContext(Dispatchers.IO) {
-                       DigestingOutputStream(hashinator.get()).useAndGet { oStream ->
-                               oStream.write(fileContents)
-                       }
+                       hashinator.get().digest(fileContents)
                }
        }
 }
index 9d7eaeb2068cbe3d075455de77e6bc5b885e086d..c9e552e173462ec6ed1376db53a3d7244c08aab1 100644 (file)
@@ -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<String, RobotFileId> {
+               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,
index dafa43b124d912ef72b8fe2289c1e3fc0851e44f..efbcc85d147c48e9886bb9f98e7ab4cf3700c1da 100644 (file)
@@ -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 T : ResourceReceiver<P>, reified P : MultiPartPayload> Route
 
 val WebSocketResourceInstanceKey: AttributeKey<Any> = AttributeKey("WebSocketResourceInstance")
 
+@Suppress("FunctionName")
 inline fun <reified T : ResourceListener> WebSocketResourcePlugin() = createRouteScopedPlugin("WebSocketResourcePlugin") {
        val serializer = serializer<T>()
        on(CallSetup) { call ->