Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Encrypted Files in Jetpack Security
// 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