Реализован UnlockManager

This commit is contained in:
2025-01-18 21:24:41 +03:00
parent c5c0173391
commit b9e73cf197
25 changed files with 598 additions and 91 deletions

View File

@@ -3,13 +3,19 @@ package com.github.nullptroma.wallenc.domain.datatypes
import java.security.MessageDigest
import javax.crypto.spec.SecretKeySpec
class EncryptKey(val key: String) {
fun to32Bytes(): ByteArray {
class EncryptKey {
val bytes: ByteArray
constructor(password: String) {
val digest = MessageDigest.getInstance("SHA-256")
return digest.digest(key.toByteArray(Charsets.UTF_8))
bytes = digest.digest(password.toByteArray(Charsets.UTF_8))
}
constructor(key: ByteArray) {
this.bytes = key.clone()
}
fun toAesKey() : SecretKeySpec {
return SecretKeySpec(to32Bytes(), "AES")
return SecretKeySpec(bytes, "AES")
}
}

View File

@@ -6,29 +6,33 @@ import com.github.nullptroma.wallenc.domain.interfaces.ILogger
import com.github.nullptroma.wallenc.domain.interfaces.IStorage
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.DisposableHandle
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import java.util.UUID
class EncryptedStorage(
source: IStorage,
private val source: IStorage,
key: EncryptKey,
logger: ILogger,
ioDispatcher: CoroutineDispatcher,
override val uuid: UUID = UUID.randomUUID()
) : IStorage, DisposableHandle {
override val size: StateFlow<Long?>
get() = TODO("Not yet implemented")
get() = source.size
override val numberOfFiles: StateFlow<Int?>
get() = TODO("Not yet implemented")
override val uuid: UUID
get() = TODO("Not yet implemented")
get() = source.numberOfFiles
override val name: StateFlow<String>
get() = TODO("Not yet implemented")
override val isAvailable: StateFlow<Boolean>
get() = TODO("Not yet implemented")
override val encInfo: StateFlow<StorageEncryptionInfo>
get() = TODO("Not yet implemented")
get() = source.isAvailable
override val encInfo: StateFlow<StorageEncryptionInfo?>
get() = MutableStateFlow(
StorageEncryptionInfo(
isEncrypted = false,
encryptedTestData = null
)
)
override val accessor: EncryptedStorageAccessor =
EncryptedStorageAccessor(source.accessor, key, logger, ioDispatcher)
EncryptedStorageAccessor(source.accessor, key, ioDispatcher)
override suspend fun rename(newName: String) {
TODO("Not yet implemented")

View File

@@ -28,7 +28,6 @@ import kotlin.io.path.pathString
class EncryptedStorageAccessor(
private val source: IStorageAccessor,
key: EncryptKey,
private val logger: ILogger,
ioDispatcher: CoroutineDispatcher
) : IStorageAccessor, DisposableHandle {
private val _job = Job()

View File

@@ -8,8 +8,8 @@ interface IUnlockManager {
/**
* Хранилища, для которых есть ключ шифрования
*/
val openedStorages: StateFlow<Map<UUID, IStorage>>
val openedStorages: StateFlow<Map<UUID, IStorage>?>
fun open(storage: IStorage, key: EncryptKey)
fun close(storage: UUID)
suspend fun open(storage: IStorage, key: EncryptKey)
suspend fun close(storage: IStorage)
}

View File

@@ -6,5 +6,6 @@ interface IVaultsManager {
val localVault: IVault
val remoteVaults: StateFlow<List<IVault>>
val allStorages: StateFlow<List<IStorage>>
fun addYandexVault(email: String, token: String)
}

View File

@@ -1,8 +0,0 @@
package com.github.nullptroma.wallenc.domain.usecases
import com.github.nullptroma.wallenc.domain.interfaces.IVaultsManager
class GetAllRawStoragesUseCase(private val manager: IVaultsManager) {
val localStorages
get() = manager.localVault.storages
}

View File

@@ -0,0 +1,12 @@
package com.github.nullptroma.wallenc.domain.usecases
import com.github.nullptroma.wallenc.domain.interfaces.IStorageInfo
import com.github.nullptroma.wallenc.domain.interfaces.IUnlockManager
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.map
import java.util.UUID
class GetOpenedStoragesUseCase(private val unlockManager: IUnlockManager) {
val openedStorages: StateFlow<Map<UUID, IStorageInfo>?>
get() = unlockManager.openedStorages
}

View File

@@ -1,12 +1,24 @@
package com.github.nullptroma.wallenc.domain.usecases
import com.github.nullptroma.wallenc.domain.datatypes.EncryptKey
import com.github.nullptroma.wallenc.domain.encrypt.Encryptor
import com.github.nullptroma.wallenc.domain.interfaces.IStorageInfo
import com.github.nullptroma.wallenc.domain.interfaces.IUnlockManager
import com.github.nullptroma.wallenc.domain.interfaces.IVaultsManager
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.map
class ManageLocalVaultUseCase(private val manager: IVaultsManager) {
val localStorages
class ManageLocalVaultUseCase(private val manager: IVaultsManager, private val unlockManager: IUnlockManager) {
val localStorages: StateFlow<List<IStorageInfo>>
get() = manager.localVault.storages
suspend fun createStorage() {
manager.localVault.createStorage()
}
suspend fun createStorage(key: EncryptKey) {
val encInfo = Encryptor.generateEncryptionInfo(key)
val storage = manager.localVault.createStorage(encInfo)
unlockManager.open(storage, key)
}
}