Skip to content

Instantly share code, notes, and snippets.

@remithaunay
remithaunay / SecureStorage.kt
Created April 28, 2025 10:14
SharedPreferences with Tink and Android KeyStore
/**
* Securely stores string data in SharedPreferences using Tink's AEAD (Authenticated Encryption with
* Associated Data) primitive, backed by AES-256-GCM encryption and Android KeyStore for key management.
*/
object SecureStorage {
// Constants for SharedPreferences and Tink keyset configuration
private const val TAG = "SecureStorage" // For logging
private const val KEYSET_NAME = "tink_keyset" // Name of the Tink keyset stored in SharedPreferences
private const val PREFS_NAME = "secure_prefs" // Name of the SharedPreferences file
private const val MASTER_KEY_URI = "android-keystore://tink_master_key" // URI for the master key in KeyStore