Skip to content

Instantly share code, notes, and snippets.

@shafty023
Created June 9, 2020 23:02
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/7932dbd19d5b8b2b034a32f7d9d1b1c4 to your computer and use it in GitHub Desktop.
Save shafty023/7932dbd19d5b8b2b034a32f7d9d1b1c4 to your computer and use it in GitHub Desktop.
Decrypts the Storable instance using a passcode
/**
* 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