-
-
Notifications
You must be signed in to change notification settings - Fork 87
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Encrypt your Media in a secure folder only accessible by your credentials The vaults and media and their info are completely encrypted and stored in the app's private folder. The encryption is a standard AES256_GCM scheme, but customisable options are on their way (with option to backup/restore the vault) Fixes Ability to hide photos/videos in a hidden folder within the app #232 Fixes Hidden folders[Enhancement] #402 Signed-off-by: IacobIonut01 <[email protected]>
- Loading branch information
1 parent
9917971
commit 9bdb49e
Showing
44 changed files
with
4,812 additions
and
101 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
97 changes: 97 additions & 0 deletions
97
app/src/main/kotlin/com/dot/gallery/feature_node/data/data_source/KeychainHolder.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
package com.dot.gallery.feature_node.data.data_source | ||
|
||
import android.content.Context | ||
import android.net.Uri | ||
import android.security.keystore.UserNotAuthenticatedException | ||
import androidx.security.crypto.EncryptedFile | ||
import androidx.security.crypto.EncryptedFile.FileEncryptionScheme.AES256_GCM_HKDF_4KB | ||
import androidx.security.crypto.MasterKey | ||
import com.dot.gallery.feature_node.domain.model.Vault | ||
import com.dot.gallery.feature_node.domain.model.fromByteArray | ||
import com.dot.gallery.feature_node.domain.model.toByteArray | ||
import dagger.hilt.android.qualifiers.ApplicationContext | ||
import java.io.ByteArrayOutputStream | ||
import java.io.File | ||
import java.io.FileNotFoundException | ||
import java.io.IOException | ||
import java.io.Serializable | ||
import java.security.GeneralSecurityException | ||
import javax.inject.Inject | ||
|
||
class KeychainHolder @Inject constructor( | ||
@ApplicationContext private val context: Context | ||
) { | ||
|
||
val filesDir: File = context.filesDir | ||
|
||
fun vaultFolder(vault: Vault) = File(filesDir, vault.uuid.toString()) | ||
fun vaultInfoFile(vault: Vault) = File(vaultFolder(vault), VAULT_INFO_FILE_NAME) | ||
fun Vault.mediaFile(mediaId: Long) = File(vaultFolder(this), "$mediaId.enc") | ||
|
||
private val masterKey: MasterKey = MasterKey.Builder(context) | ||
.setKeyScheme(MasterKey.KeyScheme.AES256_GCM) | ||
.build() | ||
|
||
fun writeVaultInfo(vault: Vault, onSuccess: () -> Unit = {}) { | ||
val vaultFolder = File(filesDir, vault.uuid.toString()) | ||
if (!vaultFolder.exists()) { | ||
vaultFolder.mkdir() | ||
} | ||
|
||
File(vaultFolder, VAULT_INFO_FILE_NAME).apply { | ||
if (exists()) delete() | ||
encrypt(vault) | ||
} | ||
onSuccess() | ||
} | ||
|
||
fun checkVaultFolder(vault: Vault) { | ||
val mainFolder = File(filesDir, vault.uuid.toString()) | ||
if (!mainFolder.exists()) { | ||
mainFolder.mkdir() | ||
writeVaultInfo(vault) | ||
} | ||
} | ||
|
||
@Throws(GeneralSecurityException::class, IOException::class, FileNotFoundException::class, UserNotAuthenticatedException::class) | ||
fun <T : Serializable> File.decrypt(): T = EncryptedFile.Builder( | ||
context, | ||
this, | ||
masterKey, | ||
AES256_GCM_HKDF_4KB | ||
).build().openFileInput().use { | ||
fromByteArray(it.readBytes()) | ||
} | ||
|
||
|
||
@Throws(GeneralSecurityException::class, IOException::class, FileNotFoundException::class, UserNotAuthenticatedException::class) | ||
fun <T : Serializable> File.encrypt(data: T) { | ||
EncryptedFile.Builder( | ||
context, | ||
this, | ||
masterKey, | ||
AES256_GCM_HKDF_4KB | ||
).build().openFileOutput().use { | ||
it.write(data.toByteArray()) | ||
} | ||
} | ||
|
||
@Throws(IOException::class) | ||
fun getBytes(uri: Uri): ByteArray? = | ||
context.contentResolver.openInputStream(uri)?.use { inputStream -> | ||
val byteBuffer = ByteArrayOutputStream() | ||
val bufferSize = 1024 | ||
val buffer = ByteArray(bufferSize) | ||
|
||
var len: Int | ||
while (inputStream.read(buffer).also { len = it } != -1) { | ||
byteBuffer.write(buffer, 0, len) | ||
} | ||
byteBuffer.toByteArray() | ||
} | ||
|
||
companion object { | ||
const val VAULT_INFO_FILE_NAME = "info.vault" | ||
const val VAULT_INFO_FILE = "/$VAULT_INFO_FILE_NAME" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.