2020-08-11 17:09:46 +02:00
/ * *
* ProjectLaogai
*
* Copyright 2019 - 2020 < seil0 @mosad . xyz >
*
* This program is free software ; you can redistribute it and / or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation ; either version 3 of the License , or
* ( at your option ) any later version .
*
* This program is distributed in the hope that it will be useful ,
* but WITHOUT ANY WARRANTY ; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
* GNU General Public License for more details .
*
* You should have received a copy of the GNU General Public License
* along with this program ; if not , write to the Free Software
* Foundation , Inc . , 51 Franklin Street , Fifth Floor , Boston ,
* MA 02110 - 1301 , USA .
*
* /
2020-08-13 21:01:21 +02:00
package org.mosad.seil0.projectlaogai.controller.preferences
2020-08-11 17:09:46 +02:00
2020-08-12 11:00:22 +02:00
import android.content.Context
import android.content.SharedPreferences
import android.security.keystore.KeyGenParameterSpec
import android.security.keystore.KeyProperties
import android.util.Log
import androidx.security.crypto.EncryptedSharedPreferences
import androidx.security.crypto.MasterKey
import org.mosad.seil0.projectlaogai.R
2020-08-13 21:01:21 +02:00
object EncryptedPreferences {
2020-08-12 11:00:22 +02:00
var email = " "
internal set
/ * *
* save user email and password to encrypted preference
* /
fun saveCredentials ( email : String , password : String , context : Context ) {
this . email = email
with ( getEncryptedPreferences ( context ) ?. edit ( ) ) {
this ?. putString ( context . getString ( R . string . save _key _user _email ) , email )
this ?. putString ( context . getString ( R . string . save _key _user _password ) , password )
this ?. apply ( )
}
}
/ * *
* read user email and password from encrypted preference
2020-08-13 21:01:21 +02:00
* @return Pair ( email , password )
2020-08-12 11:00:22 +02:00
* /
fun readCredentials ( context : Context ) : Pair < String , String > {
return with ( getEncryptedPreferences ( context ) ) {
email = this ?. getString ( context . getString ( R . string . save _key _user _email ) , " " ) . toString ( )
Pair (
this ?. getString ( context . getString ( R . string . save _key _user _email ) , " " ) . toString ( ) ,
this ?. getString ( context . getString ( R . string . save _key _user _password ) , " " ) . toString ( )
)
}
}
2020-08-13 21:01:21 +02:00
/ * *
* initially load the stored values
* /
fun load ( context : Context ) {
with ( getEncryptedPreferences ( context ) ) {
email = this ?. getString (
2020-08-15 18:13:29 +02:00
context . getString ( R . string . save _key _user _email ) , " "
2020-08-13 21:01:21 +02:00
) . toString ( )
}
}
2020-08-12 11:00:22 +02:00
/ * *
* create a encrypted shared preference
* /
private fun getEncryptedPreferences ( context : Context ) : SharedPreferences ? {
return try {
val spec = KeyGenParameterSpec . Builder ( MasterKey . DEFAULT _MASTER _KEY _ALIAS ,
KeyProperties . PURPOSE _ENCRYPT or KeyProperties . PURPOSE _DECRYPT )
. setBlockModes ( KeyProperties . BLOCK _MODE _GCM )
. setEncryptionPaddings ( KeyProperties . ENCRYPTION _PADDING _NONE )
. setKeySize ( MasterKey . DEFAULT _AES _GCM _MASTER _KEY _SIZE )
. build ( )
val masterKey = MasterKey . Builder ( context )
. setKeyGenParameterSpec ( spec )
. build ( )
EncryptedSharedPreferences . create (
context ,
context . getString ( R . string . encrypted _preference _file _key ) ,
masterKey ,
EncryptedSharedPreferences . PrefKeyEncryptionScheme . AES256 _SIV ,
EncryptedSharedPreferences . PrefValueEncryptionScheme . AES256 _GCM
)
} catch ( ex : Exception ) {
Log . e ( javaClass . name , " Could not create encrypted shared preference. " , ex )
null
}
}
2020-08-11 17:09:46 +02:00
}