Skip to content

Instantly share code, notes, and snippets.

@shafty023
Created June 9, 2020 23:10
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 shafty023/d5f362870b31b1aff3b6ae164cd07d5b to your computer and use it in GitHub Desktop.
Save shafty023/d5f362870b31b1aff3b6ae164cd07d5b to your computer and use it in GitHub Desktop.
Returns the db key
/**
* Returns the database key suitable for using with Room.
*
* @param passcode the user's passcode
* @param context the caller's context
*/
fun getCharKey(passcode: CharArray, context: Context): CharArray {
if (dbCharKey == null) {
initKey(passcode, context)
}
return dbCharKey ?: error("Failed to decrypt database key")
}
private fun initKey(passcode: CharArray, context: Context) {
val storable = getStorable(context)
if (storable == null) {
createNewKey()
persistRawKey(passcode)
} else {
rawByteKey = getRawByteKey(passcode, storable)
dbCharKey = rawByteKey.toHex()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment