-
Notifications
You must be signed in to change notification settings - Fork 106
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ebd0561
commit c682027
Showing
7 changed files
with
133 additions
and
100 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 6 additions & 0 deletions
6
Calendula/src/main/java/es/usc/citius/servando/calendula/util/security/EncryptionProvider.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package es.usc.citius.servando.calendula.util.security | ||
|
||
interface EncryptionProvider { | ||
fun encrypt(value: String): String | ||
fun decrypt(value: String): String | ||
} |
31 changes: 31 additions & 0 deletions
31
...src/main/java/es/usc/citius/servando/calendula/util/security/LibraryEncryptionProvider.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package es.usc.citius.servando.calendula.util.security | ||
|
||
import devliving.online.securedpreferencestore.EncryptionManager | ||
import es.usc.citius.servando.calendula.util.GsonUtil | ||
import es.usc.citius.servando.calendula.util.LogUtil | ||
|
||
class LibraryEncryptionProvider(private val encryptionManager: EncryptionManager) : | ||
EncryptionProvider { | ||
|
||
companion object { | ||
private const val TAG = "LibraryEncryptionProv" | ||
} | ||
|
||
override fun encrypt(value: String): String { | ||
val data = value.toByteArray(charset("UTF-8")) | ||
val secret = encryptionManager.encrypt(data) | ||
return GsonUtil.get().toJson(secret) | ||
} | ||
|
||
override fun decrypt(value: String): String { | ||
try { | ||
val secret = | ||
GsonUtil.get().fromJson(value, EncryptionManager.EncryptedData::class.java) | ||
val data = encryptionManager.decrypt(secret) | ||
return data.toString(charset("UTF-8")) | ||
} catch (e: Exception) { | ||
LogUtil.d(TAG, "Error decrypting property", e) | ||
throw e | ||
} | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
...la/src/main/java/es/usc/citius/servando/calendula/util/security/NoopEncryptionProvider.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package es.usc.citius.servando.calendula.util.security | ||
|
||
/** | ||
* Noop encrypt/decrypt provider to be used when encryption is not available (for testing) | ||
*/ | ||
class NoopEncryptionProvider : EncryptionProvider { | ||
override fun encrypt(value: String): String = value | ||
override fun decrypt(value: String): String = value | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
75 changes: 75 additions & 0 deletions
75
Calendula/src/main/java/es/usc/citius/servando/calendula/util/security/SecurityProvider.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
package es.usc.citius.servando.calendula.util.security | ||
|
||
import android.content.Context | ||
import android.content.SharedPreferences | ||
import devliving.online.securedpreferencestore.DefaultRecoveryHandler | ||
import devliving.online.securedpreferencestore.SecuredPreferenceStore | ||
import es.usc.citius.servando.calendula.util.LogUtil | ||
import es.usc.citius.servando.calendula.util.PreferenceUtils | ||
import java.lang.ref.WeakReference | ||
|
||
/** | ||
* Abstracts access to encryption features: makes SecuredVault behave as normal prefs, and encryption noop, if it's not initialized (such as for testing) | ||
*/ | ||
class SecurityProvider { | ||
|
||
companion object { | ||
|
||
private const val TAG = "SecurityProvider" | ||
|
||
private const val STORE_NAME = "secure_vault" | ||
private const val STORE_PREFIX = "vault_pref" | ||
private const val SEED_KEY = "CalendulaVault" | ||
|
||
private var securedPrefStore: SecuredPreferenceStore? = null | ||
private var encryptionProvider: WeakReference<EncryptionProvider>? = null | ||
|
||
@JvmStatic | ||
fun init(ctx: Context) { | ||
SecuredPreferenceStore.init( | ||
ctx.applicationContext, | ||
STORE_NAME, | ||
STORE_PREFIX, | ||
SEED_KEY.toByteArray(), | ||
DefaultRecoveryHandler() | ||
) | ||
securedPrefStore = SecuredPreferenceStore.getSharedInstance() | ||
encryptionProvider = null | ||
} | ||
|
||
fun isAvailable(): Boolean = securedPrefStore != null | ||
|
||
@JvmStatic | ||
fun getEncryptionProvider(): EncryptionProvider { | ||
|
||
if (encryptionProvider == null) { | ||
encryptionProvider = if (isAvailable()) { | ||
WeakReference(LibraryEncryptionProvider(securedPrefStore!!.encryptionManager)) | ||
} else { | ||
LogUtil.w( | ||
TAG, | ||
"getEncryptionProvider: SecurityProvider not initialized! Not using encryption. Is this what you want?" | ||
) | ||
WeakReference(NoopEncryptionProvider()) | ||
} | ||
} | ||
|
||
return encryptionProvider!!.get()!! | ||
} | ||
|
||
fun getPreferences(): SharedPreferences { | ||
if (isAvailable()) { | ||
return securedPrefStore as SharedPreferences | ||
} else { | ||
LogUtil.w( | ||
TAG, | ||
"getPreferences: SecurityProvider not initialized! Not using encryption. Is this what you want?" | ||
) | ||
return PreferenceUtils.instance().preferences() | ||
} | ||
} | ||
|
||
} | ||
|
||
|
||
} |