-
-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
"Added Password Protection by Encrypted Shared Preferences"
- Loading branch information
1 parent
4a519c3
commit 28af217
Showing
7 changed files
with
247 additions
and
13 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
49 changes: 49 additions & 0 deletions
49
android/app/src/main/java/rocks/poopjournal/fucksgiven/data/SecureStorage.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,49 @@ | ||
package rocks.poopjournal.fucksgiven.data | ||
|
||
import android.content.Context | ||
import android.content.SharedPreferences | ||
import androidx.security.crypto.EncryptedSharedPreferences | ||
import androidx.security.crypto.MasterKeys | ||
|
||
fun getEncryptedSharedPreferences(context: Context): SharedPreferences { | ||
val masterKeyAlias = MasterKeys.getOrCreate(MasterKeys.AES256_GCM_SPEC) | ||
|
||
return EncryptedSharedPreferences.create( | ||
"secure_prefs", | ||
masterKeyAlias, | ||
context, | ||
EncryptedSharedPreferences.PrefKeyEncryptionScheme.AES256_SIV, | ||
EncryptedSharedPreferences.PrefValueEncryptionScheme.AES256_GCM | ||
) | ||
} | ||
|
||
fun savePassword(context: Context, password: String) { | ||
val sharedPreferences = getEncryptedSharedPreferences(context) | ||
val editor = sharedPreferences.edit() | ||
editor.putString("user_password", password) | ||
editor.apply() | ||
} | ||
|
||
fun getPassword(context: Context): String? { | ||
val sharedPreferences = getEncryptedSharedPreferences(context) | ||
return sharedPreferences.getString("user_password", null) | ||
} | ||
|
||
fun clearStoredPassword(context: Context) { | ||
val sharedPreferences = getEncryptedSharedPreferences(context) | ||
val editor = sharedPreferences.edit() | ||
editor.remove("user_password") | ||
editor.apply() | ||
} | ||
|
||
fun setPasswordProtectionEnabled(context: Context, enabled: Boolean) { | ||
val sharedPreferences = getEncryptedSharedPreferences(context) | ||
val editor = sharedPreferences.edit() | ||
editor.putBoolean("password_protection_enabled", enabled) | ||
editor.apply() | ||
} | ||
|
||
fun getPasswordProtectionEnabled(context: Context): Boolean { | ||
val sharedPreferences = getEncryptedSharedPreferences(context) | ||
return sharedPreferences.getBoolean("password_protection_enabled", false) | ||
} |
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
64 changes: 64 additions & 0 deletions
64
...p/src/main/java/rocks/poopjournal/fucksgiven/presentation/screens/PasswordPromptScreen.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,64 @@ | ||
package rocks.poopjournal.fucksgiven.presentation.screens | ||
import rocks.poopjournal.fucksgiven.data.getPassword | ||
|
||
import android.content.Context | ||
import android.widget.Toast | ||
import androidx.compose.foundation.Image | ||
import androidx.compose.foundation.layout.Arrangement | ||
import androidx.compose.foundation.layout.Column | ||
import androidx.compose.foundation.layout.fillMaxSize | ||
import androidx.compose.foundation.layout.fillMaxWidth | ||
import androidx.compose.foundation.layout.padding | ||
import androidx.compose.foundation.layout.size | ||
import androidx.compose.material3.OutlinedButton | ||
import androidx.compose.material3.OutlinedTextField | ||
import androidx.compose.material3.Text | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.getValue | ||
import androidx.compose.runtime.mutableStateOf | ||
import androidx.compose.runtime.remember | ||
import androidx.compose.runtime.setValue | ||
import androidx.compose.ui.Alignment | ||
import androidx.compose.ui.Modifier | ||
import androidx.compose.ui.res.painterResource | ||
import androidx.compose.ui.text.input.PasswordVisualTransformation | ||
import androidx.compose.ui.unit.dp | ||
import rocks.poopjournal.fucksgiven.R | ||
|
||
@Composable | ||
fun PasswordPromptScreen(context: Context, onAuthenticated: () -> Unit) { | ||
var enteredPassword by remember { mutableStateOf("") } | ||
val storedPassword = getPassword(context) | ||
|
||
Column( | ||
modifier = Modifier.fillMaxSize(), | ||
verticalArrangement = Arrangement.Center, | ||
horizontalAlignment = Alignment.CenterHorizontally, | ||
) { | ||
Image( | ||
painter = painterResource(R.drawable.fucks), | ||
contentDescription = "Logo", | ||
modifier = Modifier.size(200.dp) | ||
.align(Alignment.CenterHorizontally) | ||
.padding(vertical = 24.dp) | ||
) | ||
OutlinedTextField( | ||
value = enteredPassword, | ||
onValueChange = { enteredPassword = it }, | ||
label = { Text("Enter Password") }, | ||
modifier = Modifier.padding(vertical = 16.dp), | ||
visualTransformation = PasswordVisualTransformation() | ||
) | ||
OutlinedButton(onClick = { | ||
if (storedPassword == enteredPassword) { | ||
onAuthenticated() | ||
} else { | ||
Toast.makeText(context, "Password is not correct", Toast.LENGTH_SHORT).show() | ||
} | ||
}, | ||
modifier = Modifier.fillMaxWidth(0.3f) | ||
) { | ||
Text(text = "Login") | ||
} | ||
} | ||
} |
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