security-samples
security-samples copied to clipboard
method encryptFile() has bug
private fun encryptFile() {
val title = binding.titleEditText.text.toString()
val body = binding.bodyEditText.text.toString()
if (title.isBlank()) return
try {
deleteFile(existingFileTitle)
val encryptedFile = getEncryptedFile(title)
encryptedFile.openFileOutput().use { output ->
output.write(body.toByteArray())
}
} catch (e: Exception) {
e.printStackTrace()
showSnackbar(R.string.error_unable_to_save_file)
}
}
should change deleteFile(existingFileTitle) to deleteFile(title )
I'm not sure I follow why? The way I'm thinking of it is this:
Edit note: title="Test", existingFileTitle="Test"
Don't change the title, we delete existingFileTitle and create a new file title which both have the same name. (Note: You cannot overwrite an EncryptedFile)
OR
Edit note: title="Test", existingFileTitle="Test"
Change the name to "Done" -> title="Done", existingFileTitle="Test"
To save the note, we delete existingFileTitle and write title which removes the "Test" encrypted note and creates the "Done" encrypted note.