Skip to content

Instantly share code, notes, and snippets.

@AliAzaz
Created October 9, 2021 22:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AliAzaz/45f6fb2cec35476308eca6872c0b8175 to your computer and use it in GitHub Desktop.
Save AliAzaz/45f6fb2cec35476308eca6872c0b8175 to your computer and use it in GitHub Desktop.
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