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
}
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) }
val fileContents = FileStorage.instance.readFile(path) ?: return null
return withContext(Dispatchers.IO) {
- DigestingOutputStream(hashinator.get()).useAndGet { oStream ->
- oStream.write(fileContents)
- }
+ hashinator.get().digest(fileContents)
}
}
}
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 {
RobotServiceLogger.info("Vector store creation is complete")
if (robotGlobals.assistantId == null)
- robotGlobals = robotGlobals.copy(
+ robotGlobals.copy(
assistantId = robotClient.createAssistant(
RobotCreateAssistantRequest(
model = config.assistantModel,
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.*
val WebSocketResourceInstanceKey: AttributeKey<Any> = AttributeKey("WebSocketResourceInstance")
+@Suppress("FunctionName")
inline fun <reified T : ResourceListener> WebSocketResourcePlugin() = createRouteScopedPlugin("WebSocketResourcePlugin") {
val serializer = serializer<T>()
on(CallSetup) { call ->