Skip to content

Commit

Permalink
[FEAT/#5] EncryptedSharedPreferences로 데이터 암호화 하기
Browse files Browse the repository at this point in the history
  • Loading branch information
leeeha committed May 5, 2023
1 parent eaa8b9b commit 6bd9db2
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 21 deletions.
6 changes: 4 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,14 @@ android {

dependencies {

implementation 'androidx.core:core-ktx:1.9.0'
implementation 'androidx.fragment:fragment-ktx:1.5.7'
implementation 'androidx.core:core-ktx:1.10.0'
implementation 'androidx.appcompat:appcompat:1.6.1'
implementation 'com.google.android.material:material:1.8.0'
implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.5'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'

implementation 'androidx.fragment:fragment-ktx:1.5.7'
implementation "androidx.security:security-crypto-ktx:1.1.0-alpha06"
}
19 changes: 9 additions & 10 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
xmlns:tools="http://schemas.android.com/tools">

<application
android:name=".Week2Application"
android:name=".GoSoptApplication"
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
Expand All @@ -15,22 +15,21 @@

<activity
android:name=".ui.login.LoginActivity"
android:exported="false"
android:windowSoftInputMode="adjustResize" />
<activity
android:name=".ui.signup.SignUpActivity"
android:exported="false"
android:windowSoftInputMode="adjustResize" />

<activity
android:name=".ui.main.MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

<activity
android:name=".ui.signup.SignUpActivity"
android:exported="false" />

<activity
android:name=".ui.main.MainActivity"
android:exported="false" />
</application>

</manifest>
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ package org.android.go.sopt
import android.app.Application
import org.android.go.sopt.util.PreferenceUtil

class Week2Application: Application() {
class GoSoptApplication: Application() {
override fun onCreate() {
prefs = PreferenceUtil(applicationContext)
super.onCreate()
prefs = PreferenceUtil(applicationContext)
}

companion object {
Expand Down
30 changes: 23 additions & 7 deletions app/src/main/java/org/android/go/sopt/util/PreferenceUtil.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,36 @@ package org.android.go.sopt.util

import android.content.Context
import android.content.SharedPreferences
import androidx.security.crypto.EncryptedSharedPreferences
import androidx.security.crypto.MasterKey

class PreferenceUtil(context: Context) {
private val prefs: SharedPreferences =
context.getSharedPreferences(SHARED_PREFS_NAME, Context.MODE_PRIVATE)
private val sharedPreferences: SharedPreferences by lazy {
// 암호화 할 마스터 키 생성
val masterKeyAlias = MasterKey
.Builder(context, MasterKey.DEFAULT_MASTER_KEY_ALIAS)
.setKeyScheme(MasterKey.KeyScheme.AES256_GCM)
.build()

fun getString(key: String, defValue: String?): String? {
return prefs.getString(key, defValue)
// key, value를 암호화 한 sharedPreferences 객체 생성
EncryptedSharedPreferences.create(
context,
FILE_NAME,
masterKeyAlias,
EncryptedSharedPreferences.PrefKeyEncryptionScheme.AES256_SIV,
EncryptedSharedPreferences.PrefValueEncryptionScheme.AES256_GCM
)
}

fun setString(key: String, str: String) {
prefs.edit().putString(key, str).apply()
fun getString(key: String, defaultValue: String?): String? {
return sharedPreferences.getString(key, defaultValue)
}

fun setString(key: String, newValue: String) {
sharedPreferences.edit().putString(key, newValue).apply()
}

companion object {
private const val SHARED_PREFS_NAME = "my_prefs"
private const val FILE_NAME = "encrypted_user_info"
}
}

0 comments on commit 6bd9db2

Please sign in to comment.