@@ -5,24 +5,24 @@ import android.content.SharedPreferences
55import android.util.Base64
66import com.facebook.react.bridge.ReactApplicationContext
77import com.oblador.keychain.KeychainModule.KnownCiphers
8- import com.oblador.keychain.cipherStorage.CipherStorage.CipherResult
8+ import com.oblador.keychain.PrefsStorageBase.Companion.KEYCHAIN_DATA
9+ import com.oblador.keychain.PrefsStorageBase.Companion.getKeyForCipherStorage
10+ import com.oblador.keychain.PrefsStorageBase.Companion.getKeyForPassword
11+ import com.oblador.keychain.PrefsStorageBase.Companion.getKeyForUsername
12+ import com.oblador.keychain.PrefsStorageBase.Companion.isKeyForCipherStorage
13+ import com.oblador.keychain.PrefsStorageBase.ResultSet
914import com.oblador.keychain.cipherStorage.CipherStorage.EncryptionResult
1015
1116@Suppress(" unused" )
12- class PrefsStorage (reactContext : ReactApplicationContext ) {
13- class ResultSet (
14- @JvmField @field:KnownCiphers @param:KnownCiphers val cipherStorageName : String ,
15- usernameBytes : ByteArray? ,
16- passwordBytes : ByteArray?
17- ) : CipherResult<ByteArray?>(usernameBytes, passwordBytes)
17+ open class PrefsStorage (reactContext : ReactApplicationContext ) : PrefsStorageBase {
1818
1919 private val prefs: SharedPreferences
2020
2121 init {
2222 prefs = reactContext.getSharedPreferences(KEYCHAIN_DATA , Context .MODE_PRIVATE )
2323 }
2424
25- fun getEncryptedEntry (service : String ): ResultSet ? {
25+ override fun getEncryptedEntry (service : String ): ResultSet ? {
2626 val bytesForUsername = getBytesForUsername(service)
2727 val bytesForPassword = getBytesForPassword(service)
2828 var cipherStorageName = getCipherStorageName(service)
@@ -40,14 +40,14 @@ class PrefsStorage(reactContext: ReactApplicationContext) {
4040 return ResultSet (cipherStorageName, bytesForUsername, bytesForPassword)
4141 }
4242
43- fun removeEntry (service : String ) {
43+ override fun removeEntry (service : String ) {
4444 val keyForUsername = getKeyForUsername(service)
4545 val keyForPassword = getKeyForPassword(service)
4646 val keyForCipherStorage = getKeyForCipherStorage(service)
4747 prefs.edit().remove(keyForUsername).remove(keyForPassword).remove(keyForCipherStorage).apply ()
4848 }
4949
50- fun storeEncryptedEntry (service : String , encryptionResult : EncryptionResult ) {
50+ override fun storeEncryptedEntry (service : String , encryptionResult : EncryptionResult ) {
5151 val keyForUsername = getKeyForUsername(service)
5252 val keyForPassword = getKeyForPassword(service)
5353 val keyForCipherStorage = getKeyForCipherStorage(service)
@@ -59,16 +59,7 @@ class PrefsStorage(reactContext: ReactApplicationContext) {
5959 .apply ()
6060 }
6161
62- val usedCipherNames: Set <String ?>
63- /* *
64- * List all types of cipher which are involved in en/decryption of the data stored herein.
65- *
66- * A cipher type is stored together with the datum upon encryption so the datum can later be
67- * decrypted using correct cipher. This way, a [PrefsStorage] can involve different ciphers for
68- * different data. This method returns all ciphers involved with this storage.
69- *
70- * @return set of cipher names
71- */
62+ override val usedCipherNames: Set <String ?>
7263 get() {
7364 val result: MutableSet <String ?> = HashSet ()
7465 val keys: Set <String > = prefs.all.keys
@@ -102,24 +93,4 @@ class PrefsStorage(reactContext: ReactApplicationContext) {
10293 Base64 .decode(value, Base64 .DEFAULT )
10394 } else null
10495 }
105-
106- companion object {
107- const val KEYCHAIN_DATA = " RN_KEYCHAIN"
108-
109- fun getKeyForUsername (service : String ): String {
110- return " $service :u"
111- }
112-
113- fun getKeyForPassword (service : String ): String {
114- return " $service :p"
115- }
116-
117- fun getKeyForCipherStorage (service : String ): String {
118- return " $service :c"
119- }
120-
121- fun isKeyForCipherStorage (key : String ): Boolean {
122- return key.endsWith(" :c" )
123- }
124- }
12596}
0 commit comments