File tree Expand file tree Collapse file tree
core/security/src/main/java/com/threegap/bitnagil/security Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ package com.threegap.bitnagil.security
2+
3+ import android.security.keystore.KeyGenParameterSpec
4+ import android.security.keystore.KeyProperties
5+ import java.security.KeyStore
6+ import javax.crypto.KeyGenerator
7+ import javax.crypto.SecretKey
8+
9+ class AndroidKeyProvider : KeyProvider {
10+ private val keyStore =
11+ KeyStore
12+ .getInstance(" AndroidKeyStore" )
13+ .apply { load(null ) }
14+
15+ override fun getKey (): SecretKey {
16+ val existingKey = keyStore.getEntry(KEY_ALIAS , null ) as ? KeyStore .SecretKeyEntry
17+ return existingKey?.secretKey ? : createKey()
18+ }
19+
20+ private fun createKey (): SecretKey {
21+ return KeyGenerator .getInstance(ALGORITHM ).apply {
22+ init (
23+ KeyGenParameterSpec .Builder (
24+ KEY_ALIAS ,
25+ KeyProperties .PURPOSE_ENCRYPT or KeyProperties .PURPOSE_DECRYPT ,
26+ ).setBlockModes(BLOCK_MODE )
27+ .setEncryptionPaddings(PADDING )
28+ .setRandomizedEncryptionRequired(true )
29+ .setUserAuthenticationRequired(false )
30+ .build(),
31+ )
32+ }.generateKey()
33+ }
34+
35+ companion object {
36+ private const val KEY_ALIAS = " bitnagil_auth_token"
37+ private const val ALGORITHM = KeyProperties .KEY_ALGORITHM_AES
38+ private const val BLOCK_MODE = KeyProperties .BLOCK_MODE_CBC
39+ private const val PADDING = KeyProperties .ENCRYPTION_PADDING_PKCS7
40+ }
41+ }
Original file line number Diff line number Diff line change 1+ package com.threegap.bitnagil.security
2+
3+ import javax.crypto.SecretKey
4+
5+ interface KeyProvider {
6+ fun getKey (): SecretKey
7+ }
You can’t perform that action at this time.
0 commit comments