SecretStoringUtils

class SecretStoringUtils @Inject constructor(    context: Context,     keyStore: KeyStore,     buildVersionSdkIntProvider: BuildVersionSdkIntProvider,     keyNeedsUserAuthentication: Boolean = false)

Offers simple methods to securely store secrets in an Android Application. The encryption keys are randomly generated and securely managed by the key store, thus your secrets are safe. You only need to remember a key alias to perform encrypt/decrypt operations.

Android M++ On android M+, the keystore can generates and store AES keys via API. But below API M this functionality is not available.

Android [K-M[ For android >=L and

Sample usage: val secret = "The answer is 42" val KEncrypted = SecretStoringUtils.securelyStoreString(secret, "myAlias") //This can be stored anywhere e.g. encoded in b64 and stored in preference for example

    //to get back the secret, just call
    val kDecrypted = SecretStoringUtils.loadSecureSecret(KEncrypted, "myAlias")

You can also just use this utility to store a secret key, and use any encryption algorithm that you want.

Important: Keys stored in the keystore can be wiped out (depends of the OS version, like for example if you add a pin or change the schema); So you might and with a useless pile of bytes.

Constructors

Link copied to clipboard
@Inject
fun SecretStoringUtils(    context: Context,     keyStore: KeyStore,     buildVersionSdkIntProvider: BuildVersionSdkIntProvider,     keyNeedsUserAuthentication: Boolean = false)

Types

Link copied to clipboard
object Companion

Functions

Link copied to clipboard
fun ensureKey(alias: String): KeyStore.Entry

Allows creation of the crypto keys associated witht he alias before encrypting some value with it.

Link copied to clipboard
fun getEncryptCipher(alias: String): Cipher
Link copied to clipboard
fun <T> loadSecureSecret(inputStream: InputStream, keyAlias: String): T?
Link copied to clipboard
fun loadSecureSecretBytes(encrypted: ByteArray, keyAlias: String): ByteArray

Decrypt a secret that was encrypted by securelyStoreBytes.

Link copied to clipboard
fun safeDeleteKey(keyAlias: String)

Deletes the key associated with the keyAlias and logs any KeyStoreException that could happen.

Link copied to clipboard
fun securelyStoreBytes(secret: ByteArray, keyAlias: String): ByteArray

Encrypt the given secret using the android Keystore. On android >= M, will directly use the keystore to generate a symmetric key On android >= Lollipop and

Link copied to clipboard
fun securelyStoreObject(    any: Any,     keyAlias: String,     output: OutputStream)