All Downloads are FREE. Search and download functionalities are using the official Maven repository.

cryptostring.CryptostringModule.kt Maven / Gradle / Ivy

The newest version!
package se.wollan.crypto.cryptostring

import org.slf4j.Logger
import se.wollan.crypto.*
import se.wollan.crypto.AesGcmEncryptor
import se.wollan.crypto.KeyStretcher
import se.wollan.crypto.LoggingKeyStretcherDecorator
import se.wollan.crypto.PBKDF2KeyStretcher
import se.wollan.crypto.SymmetricEncryptor
import se.wollan.crypto.keyvault.KeyVaultImpl
import se.wollan.crypto.keyvault.KeyVaultRepo
import se.wollan.crypto.keyvault.SqliteKeyVaultRepo
import se.wollan.crypto.keyvault.KeyVault
import se.wollan.datascope.SqliteDatabase
import se.wollan.time.HybridLogicalClock
import kotlin.random.Random

open class CryptostringModule(
    private val resolveRandom: () -> Random,
    private val resolveSqliteDatabase: () -> SqliteDatabase,
    private val resolveKeyStretchIterations: () -> KeyStretchIterations,
    private val resolveClock: () -> HybridLogicalClock,
    private val resolveLogger: (String) -> Logger
) {
    private inline fun  resolveLogger(): Logger = resolveLogger(T::class.simpleName!!)

    private fun resolveSymmetricEncryptor(): SymmetricEncryptor = AesGcmEncryptor(resolveRandom())

    private fun resolveKeyStretcher(): KeyStretcher =
        LoggingKeyStretcherDecorator(PBKDF2KeyStretcher(), resolveLogger())

    open fun resolveDatabaseMigrator(): DatabaseMigrator = SqliteDatabaseMigrator(resolveSqliteDatabase())

    private fun resolveKeyVaultRepo(): KeyVaultRepo = SqliteKeyVaultRepo(resolveSqliteDatabase())

    open fun resolveKeyVault(): KeyVault = _keyVault
    private val _keyVault by lazy {
        KeyVaultImpl(
            keyVaultRepo = resolveKeyVaultRepo(),
            encryptor = resolveSymmetricEncryptor(),
            keyStretcher = resolveKeyStretcher(),
            random = resolveRandom(),
            dataScope = resolveSqliteDatabase(),
            keyStretchIterations = resolveKeyStretchIterations(),
            logger = resolveLogger(),
            clock = resolveClock()
        )
    }

    open fun resolveCryptostringer(): Cryptostringer =
        CryptostringerImpl(resolveSymmetricEncryptor(), resolveKeyVault())
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy