Опциональное шифрование имён файлов
This commit is contained in:
@@ -2,7 +2,7 @@ package com.github.nullptroma.wallenc.domain.datatypes
|
||||
|
||||
data class StorageEncryptionInfo(
|
||||
val encryptedTestData: String,
|
||||
val pathIv: ByteArray
|
||||
val pathIv: ByteArray?
|
||||
) {
|
||||
override fun equals(other: Any?): Boolean {
|
||||
if (this === other) return true
|
||||
|
||||
@@ -74,7 +74,7 @@ class Encryptor(private var secretKey: SecretKey) : DisposableHandle {
|
||||
}
|
||||
|
||||
override fun dispose() {
|
||||
secretKey.destroy()
|
||||
//secretKey.destroy()
|
||||
}
|
||||
|
||||
companion object {
|
||||
@@ -83,13 +83,13 @@ class Encryptor(private var secretKey: SecretKey) : DisposableHandle {
|
||||
private const val TEST_DATA_LEN = 512
|
||||
|
||||
@OptIn(ExperimentalEncodingApi::class)
|
||||
fun generateEncryptionInfo(key: EncryptKey) : StorageEncryptionInfo {
|
||||
fun generateEncryptionInfo(key: EncryptKey, encryptPath: Boolean) : StorageEncryptionInfo {
|
||||
val encryptor = Encryptor(key.toAesKey())
|
||||
val testData = ByteArray(TEST_DATA_LEN)
|
||||
val encryptedData = encryptor.encryptBytes(testData)
|
||||
return StorageEncryptionInfo(
|
||||
encryptedTestData = Base64.Default.encode(encryptedData),
|
||||
pathIv = Random.nextBytes(IV_LEN)
|
||||
pathIv = if(encryptPath) Random.nextBytes(IV_LEN) else null
|
||||
)
|
||||
}
|
||||
|
||||
|
||||
@@ -10,6 +10,6 @@ interface IUnlockManager {
|
||||
*/
|
||||
val openedStorages: StateFlow<Map<UUID, IStorage>?>
|
||||
|
||||
suspend fun open(storage: IStorage, key: EncryptKey)
|
||||
suspend fun open(storage: IStorage, key: EncryptKey): IStorage
|
||||
suspend fun close(storage: IStorage)
|
||||
}
|
||||
@@ -17,12 +17,6 @@ class ManageLocalVaultUseCase(private val manager: IVaultsManager, private val u
|
||||
manager.localVault.createStorage()
|
||||
}
|
||||
|
||||
suspend fun createStorage(key: EncryptKey) {
|
||||
val encInfo = Encryptor.generateEncryptionInfo(key)
|
||||
val storage = manager.localVault.createStorage(encInfo)
|
||||
unlockManager.open(storage, key)
|
||||
}
|
||||
|
||||
suspend fun remove(storage: IStorageInfo) {
|
||||
when(storage) {
|
||||
is IStorage -> manager.localVault.remove(storage)
|
||||
|
||||
@@ -0,0 +1,27 @@
|
||||
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.IStorage
|
||||
import com.github.nullptroma.wallenc.domain.interfaces.IStorageInfo
|
||||
import com.github.nullptroma.wallenc.domain.interfaces.IUnlockManager
|
||||
|
||||
class ManageStoragesEncryptionUseCase(private val unlockManager: IUnlockManager) {
|
||||
suspend fun enableEncryption(storage: IStorageInfo, key: EncryptKey, encryptPath: Boolean) {
|
||||
when(storage) {
|
||||
is IStorage -> {
|
||||
if(storage.metaInfo.value.encInfo != null)
|
||||
throw Exception() // TODO
|
||||
storage.setEncInfo(Encryptor.generateEncryptionInfo(key, encryptPath))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
suspend fun openStorage(storage: IStorageInfo, key: EncryptKey): IStorageInfo {
|
||||
when(storage) {
|
||||
is IStorage -> {
|
||||
return unlockManager.open(storage, key)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user