|
| 1 | +package com.yapp.ndgl.data.auth.local.security |
| 2 | + |
| 3 | +import android.security.keystore.KeyGenParameterSpec |
| 4 | +import android.security.keystore.KeyProperties |
| 5 | +import android.util.Base64 |
| 6 | +import timber.log.Timber |
| 7 | +import java.security.GeneralSecurityException |
| 8 | +import java.security.KeyStore |
| 9 | +import javax.crypto.Cipher |
| 10 | +import javax.crypto.KeyGenerator |
| 11 | +import javax.crypto.SecretKey |
| 12 | +import javax.crypto.spec.GCMParameterSpec |
| 13 | +import javax.inject.Inject |
| 14 | +import javax.inject.Singleton |
| 15 | + |
| 16 | +@Singleton |
| 17 | +class CryptoManager @Inject constructor() { |
| 18 | + private val keyStore = KeyStore.getInstance(KEYSTORE_PROVIDER).apply { |
| 19 | + load(null) |
| 20 | + } |
| 21 | + |
| 22 | + private fun getKey(): SecretKey { |
| 23 | + return keyStore.getKey(KEY_ALIAS, null) as? SecretKey ?: createKey() |
| 24 | + } |
| 25 | + |
| 26 | + private fun createKey(): SecretKey = |
| 27 | + KeyGenerator.getInstance( |
| 28 | + KeyProperties.KEY_ALGORITHM_AES, |
| 29 | + KEYSTORE_PROVIDER, |
| 30 | + ).apply { |
| 31 | + init( |
| 32 | + KeyGenParameterSpec.Builder( |
| 33 | + KEY_ALIAS, |
| 34 | + KeyProperties.PURPOSE_ENCRYPT or KeyProperties.PURPOSE_DECRYPT, |
| 35 | + ) |
| 36 | + .setBlockModes(KeyProperties.BLOCK_MODE_GCM) |
| 37 | + .setEncryptionPaddings(PADDING) |
| 38 | + .setUserAuthenticationRequired(false) |
| 39 | + .build(), |
| 40 | + ) |
| 41 | + }.generateKey() |
| 42 | + |
| 43 | + fun encrypt(plainText: String): String { |
| 44 | + check(plainText.isNotEmpty()) { "Plain text is empty" } |
| 45 | + |
| 46 | + val cipher = Cipher.getInstance(TRANSFORMATION) |
| 47 | + cipher.init(Cipher.ENCRYPT_MODE, getKey()) |
| 48 | + |
| 49 | + val encryptedBytes = cipher.doFinal(plainText.toByteArray()) |
| 50 | + val combined = cipher.iv + encryptedBytes |
| 51 | + |
| 52 | + return Base64.encodeToString(combined, Base64.NO_WRAP) |
| 53 | + } |
| 54 | + |
| 55 | + fun decrypt(encryptedText: String): String { |
| 56 | + try { |
| 57 | + if (encryptedText.isEmpty()) return "" |
| 58 | + |
| 59 | + val combined = Base64.decode(encryptedText, Base64.NO_WRAP) |
| 60 | + val iv = combined.copyOfRange(0, IV_SIZE) |
| 61 | + val encryptedBytes = combined.copyOfRange(IV_SIZE, combined.size) |
| 62 | + |
| 63 | + val cipher = Cipher.getInstance(TRANSFORMATION) |
| 64 | + val spec = GCMParameterSpec(GCM_TAG_LENGTH, iv) |
| 65 | + cipher.init(Cipher.DECRYPT_MODE, getKey(), spec) |
| 66 | + |
| 67 | + val decryptedBytes = cipher.doFinal(encryptedBytes) |
| 68 | + return String(decryptedBytes) |
| 69 | + } catch (e: GeneralSecurityException) { |
| 70 | + Timber.e(e, "Failed to decrypt") |
| 71 | + return "" |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + companion object { |
| 76 | + private const val KEYSTORE_PROVIDER = "AndroidKeyStore" |
| 77 | + private const val KEY_ALIAS = "ndgl_encryption_key" |
| 78 | + private const val ALGORITHM = KeyProperties.KEY_ALGORITHM_AES |
| 79 | + private const val BLOCK_MODE = KeyProperties.BLOCK_MODE_GCM |
| 80 | + private const val PADDING = KeyProperties.ENCRYPTION_PADDING_NONE |
| 81 | + private const val TRANSFORMATION = "$ALGORITHM/$BLOCK_MODE/$PADDING" |
| 82 | + private const val IV_SIZE = 12 |
| 83 | + private const val GCM_TAG_LENGTH = 128 |
| 84 | + } |
| 85 | +} |
0 commit comments