Проверка ключа
This commit is contained in:
@@ -11,4 +11,5 @@ java {
|
|||||||
|
|
||||||
dependencies {
|
dependencies {
|
||||||
implementation(libs.kotlinx.coroutines.core)
|
implementation(libs.kotlinx.coroutines.core)
|
||||||
|
testImplementation(libs.junit)
|
||||||
}
|
}
|
||||||
@@ -1,10 +1,15 @@
|
|||||||
package com.github.nullptroma.wallenc.domain.datatypes
|
package com.github.nullptroma.wallenc.domain.datatypes
|
||||||
|
|
||||||
import java.security.MessageDigest
|
import java.security.MessageDigest
|
||||||
|
import javax.crypto.spec.SecretKeySpec
|
||||||
|
|
||||||
class EncryptKey(val key: String) {
|
class EncryptKey(val key: String) {
|
||||||
fun to32Bytes(): ByteArray {
|
fun to32Bytes(): ByteArray {
|
||||||
val digest = MessageDigest.getInstance("SHA-256")
|
val digest = MessageDigest.getInstance("SHA-256")
|
||||||
return digest.digest(key.toByteArray(Charsets.UTF_8))
|
return digest.digest(key.toByteArray(Charsets.UTF_8))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun toAesKey() : SecretKeySpec {
|
||||||
|
return SecretKeySpec(to32Bytes(), "AES")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -5,7 +5,6 @@ import com.github.nullptroma.wallenc.domain.common.impl.CommonFile
|
|||||||
import com.github.nullptroma.wallenc.domain.common.impl.CommonMetaInfo
|
import com.github.nullptroma.wallenc.domain.common.impl.CommonMetaInfo
|
||||||
import com.github.nullptroma.wallenc.domain.datatypes.DataPackage
|
import com.github.nullptroma.wallenc.domain.datatypes.DataPackage
|
||||||
import com.github.nullptroma.wallenc.domain.datatypes.EncryptKey
|
import com.github.nullptroma.wallenc.domain.datatypes.EncryptKey
|
||||||
import com.github.nullptroma.wallenc.domain.datatypes.StorageEncryptionInfo
|
|
||||||
import com.github.nullptroma.wallenc.domain.interfaces.IDirectory
|
import com.github.nullptroma.wallenc.domain.interfaces.IDirectory
|
||||||
import com.github.nullptroma.wallenc.domain.interfaces.IFile
|
import com.github.nullptroma.wallenc.domain.interfaces.IFile
|
||||||
import com.github.nullptroma.wallenc.domain.interfaces.ILogger
|
import com.github.nullptroma.wallenc.domain.interfaces.ILogger
|
||||||
@@ -23,7 +22,6 @@ import kotlinx.coroutines.flow.map
|
|||||||
import kotlinx.coroutines.launch
|
import kotlinx.coroutines.launch
|
||||||
import java.io.InputStream
|
import java.io.InputStream
|
||||||
import java.io.OutputStream
|
import java.io.OutputStream
|
||||||
import javax.crypto.spec.SecretKeySpec
|
|
||||||
import kotlin.io.path.Path
|
import kotlin.io.path.Path
|
||||||
import kotlin.io.path.pathString
|
import kotlin.io.path.pathString
|
||||||
|
|
||||||
@@ -46,7 +44,7 @@ class EncryptedStorageAccessor(
|
|||||||
private val _dirsUpdates = MutableSharedFlow<DataPackage<List<IDirectory>>>()
|
private val _dirsUpdates = MutableSharedFlow<DataPackage<List<IDirectory>>>()
|
||||||
override val dirsUpdates: SharedFlow<DataPackage<List<IDirectory>>> = _dirsUpdates
|
override val dirsUpdates: SharedFlow<DataPackage<List<IDirectory>>> = _dirsUpdates
|
||||||
|
|
||||||
private val _encryptor = Encryptor(SecretKeySpec(key.to32Bytes(), "AES"))
|
private val _encryptor = Encryptor(key.toAesKey())
|
||||||
|
|
||||||
init {
|
init {
|
||||||
collectSourceState()
|
collectSourceState()
|
||||||
@@ -217,14 +215,4 @@ class EncryptedStorageAccessor(
|
|||||||
_job.cancel()
|
_job.cancel()
|
||||||
_encryptor.dispose()
|
_encryptor.dispose()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
companion object {
|
|
||||||
private const val IV_LEN = 16
|
|
||||||
private const val AES_SETTINGS = "AES/CBC/PKCS5Padding"
|
|
||||||
|
|
||||||
fun generateEncryptionInfo(key: EncryptKey): StorageEncryptionInfo {
|
|
||||||
TODO()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
@@ -1,5 +1,7 @@
|
|||||||
package com.github.nullptroma.wallenc.domain.encrypt
|
package com.github.nullptroma.wallenc.domain.encrypt
|
||||||
|
|
||||||
|
import com.github.nullptroma.wallenc.domain.datatypes.EncryptKey
|
||||||
|
import com.github.nullptroma.wallenc.domain.datatypes.StorageEncryptionInfo
|
||||||
import kotlinx.coroutines.DisposableHandle
|
import kotlinx.coroutines.DisposableHandle
|
||||||
import java.io.InputStream
|
import java.io.InputStream
|
||||||
import java.io.OutputStream
|
import java.io.OutputStream
|
||||||
@@ -74,8 +76,33 @@ class Encryptor(private var _secretKey: SecretKey?) : DisposableHandle {
|
|||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
private const val IV_LEN = 16
|
private const val IV_LEN = 16
|
||||||
|
private const val TEST_DATA_LEN = 512
|
||||||
private const val AES_SETTINGS = "AES/CBC/PKCS5Padding"
|
private const val AES_SETTINGS = "AES/CBC/PKCS5Padding"
|
||||||
|
|
||||||
|
@OptIn(ExperimentalEncodingApi::class)
|
||||||
|
fun generateEncryptionInfo(key: EncryptKey) : StorageEncryptionInfo {
|
||||||
|
val encryptor = Encryptor(key.toAesKey())
|
||||||
|
val testData = ByteArray(TEST_DATA_LEN)
|
||||||
|
val encryptedData = encryptor.encryptBytes(testData)
|
||||||
|
return StorageEncryptionInfo(
|
||||||
|
isEncrypted = true,
|
||||||
|
encryptedTestData = Base64.Default.encode(encryptedData)
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
@OptIn(ExperimentalEncodingApi::class)
|
||||||
|
fun checkKey(key: EncryptKey, encInfo: StorageEncryptionInfo): Boolean {
|
||||||
|
if(encInfo.encryptedTestData == null)
|
||||||
|
return false
|
||||||
|
val encryptor = Encryptor(key.toAesKey())
|
||||||
|
try {
|
||||||
|
val encData = Base64.Default.decode(encInfo.encryptedTestData)
|
||||||
|
val testData = encryptor.decryptBytes(encData)
|
||||||
|
return testData.all { it == 0.toByte() } && testData.size == TEST_DATA_LEN
|
||||||
|
}
|
||||||
|
catch (e: Exception) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user