Created
June 9, 2020 23:10
-
-
Save shafty023/d5f362870b31b1aff3b6ae164cd07d5b to your computer and use it in GitHub Desktop.
Returns the db key
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
/** | |
* 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