Created
June 9, 2020 23:02
-
-
Save shafty023/7932dbd19d5b8b2b034a32f7d9d1b1c4 to your computer and use it in GitHub Desktop.
Decrypts the Storable instance using a passcode
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
/** | |
* Decrypts the [Storable] instance using the [passcode]. | |
* | |
* @pararm passcode the user's passcode | |
* @param storable the storable instance previously saved with [saveToPrefs] | |
* @return the raw byte key previously generated with [generateRandomKey] | |
*/ | |
fun getRawByteKey(passcode: CharArray, storable: Storable): ByteArray { | |
val aesWrappedKey = Base64.decode(storable.key, Base64.DEFAULT) | |
val iv = Base64.decode(storable.iv, Base64.DEFAULT) | |
val salt = Base64.decode(storable.salt, Base64.DEFAULT) | |
val secret: SecretKey = generateSecretKey(passcode, salt) | |
val cipher = Cipher.getInstance("AES/GCM/NoPadding") | |
cipher.init(Cipher.DECRYPT_MODE, secret, IvParameterSpec(iv)) | |
return cipher.doFinal(aesWrappedKey) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment