Created
October 9, 2021 22:50
-
-
Save AliAzaz/45f6fb2cec35476308eca6872c0b8175 to your computer and use it in GitHub Desktop.
Encrypted Files in Jetpack Security
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
// Although you can define your own key generation parameter specification, it's | |
// recommended that you use the value specified here. | |
val keyGenParameterSpec = MasterKeys.AES256_GCM_SPEC | |
val mainKeyAlias = MasterKeys.getOrCreate(keyGenParameterSpec) | |
val encrypFile = EncryptedFile.Builder( | |
File(directoryPath, "file_name"), | |
context, | |
mainKeyAlias, | |
EncryptedFile.FileEncryptionScheme.AES256_GCM_HKDF_4KB | |
).build() | |
/* | |
* For write use openFileOutput | |
* For Read use openFileInput | |
*/ | |
//Write in file | |
encrypFile.openFileOutput().bufferedWriter().use { writer-> | |
writer.write("your data") | |
} | |
//Read from file | |
val dataFile = encrypFile.openFileInput().bufferedReader().useLines { reader -> | |
// Read data | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment