Skip to content

Commit 06d78fc

Browse files
committed
security for sharedpref and storage added
1 parent 687e88e commit 06d78fc

File tree

2 files changed

+91
-0
lines changed

2 files changed

+91
-0
lines changed

_drafts/2019-08-05-sharedpreferences.md

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,4 +160,39 @@ class DataStore : PreferenceDataStore {
160160
//log operation
161161
}
162162
}
163+
{% endhighlight %}
164+
165+
## Bezpieczeństwo
166+
Biblioteka `Security` dostarcza dodatkowej ochrony przed niepowołanym dostępem do danych stosując się do kryptograficznych reguł bezpieczeństwa z jednoczesnym zachowaniem wydajności. Wykorzystuje dwuczęściowy system zarządzania kluczami składający się ze zbioru kluczy `keyset` oraz z klucza głównego `master key`. `Keyset` zawiera klucze szyfrujące dane, które są przechowywane w `SharedPreferences`, natomiast `master key` jest kluczem szyfrującym klucze z `keyset` i przechowywany jest w `KeyStore`. Dostęp do zaszyfrowanych `SharedPreferences` odbywa się przez instancję `EncryptedSharedPreferences`.
167+
168+
{% highlight kotlin %}
169+
class EncryptedActivity : AppCompatActivity() {
170+
171+
override fun onCreate(savedInstanceState: Bundle?) {
172+
super.onCreate(savedInstanceState)
173+
174+
val encryptedSharedPref = createEncryptedSharedPref()
175+
encryptedSharedPref.edit().putInt("key", 100).commit()
176+
val encrypted = encryptedSharedPref.getInt("key", 0) //100
177+
178+
//encrypted and standard SharedPreferences are different despite the same file key declaration
179+
val sharedPref = getSharedPreferences("pl.androidcode.app.FILE_KEY", Context.MODE_PRIVATE)
180+
val normal = sharedPref.getInt("key", 0) //it will be 0
181+
}
182+
183+
fun createEncryptedSharedPref(): SharedPreferences {
184+
return EncryptedSharedPreferences.create(
185+
"pl.androidcode.app.FILE_KEY",
186+
getMasterKeyAlias(),
187+
this,
188+
EncryptedSharedPreferences.PrefKeyEncryptionScheme.AES256_SIV,
189+
EncryptedSharedPreferences.PrefValueEncryptionScheme.AES256_GCM
190+
)
191+
}
192+
193+
fun getMasterKeyAlias(): String {
194+
val keyGenParameterSpec = MasterKeys.AES256_GCM_SPEC
195+
return MasterKeys.getOrCreate(keyGenParameterSpec)
196+
}
197+
}
163198
{% endhighlight %}

_drafts/2019-08-12-storage.md

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,4 +234,60 @@ class LruCacheActivity : AppCompatActivity() {
234234
return value
235235
}
236236
}
237+
{% endhighlight %}
238+
239+
## Bezpieczeństwo
240+
Przechowywanie danych na urządzeniu jest narażone na niepowołany dostęp nawet w prywatnej lokalizacji plików. Podstawowym sposobem zachowania bezpieczeństwa jest zastosowanie szyfrów kryptograficznych dzięki czemu pomimo uzyskania dostępu zawartość pliku pozostanie zakodowana. Biblioteka `Security` ułatwia to zadanie poprzez dostarczenie gotowego mechanizmu szyfrowania opartego o dwuczęściowy system zarządzania kluczami, gdzie klucze szyfrujące są również zaszyfrowane przez klucz główny znajdujący się w `KeyStore`. Klasa `EncryptedFile` dostarcza bezpieczną implementację dla `FileInputStream` i `FileOutputStream`.
241+
242+
{% highlight kotlin %}
243+
class EncryptedActivity : AppCompatActivity() {
244+
245+
override fun onCreate(savedInstanceState: Bundle?) {
246+
super.onCreate(savedInstanceState)
247+
writeEncrypt()
248+
readEncrypt()
249+
250+
//file is encrypted so reading in standard way returns encrypted content
251+
val file = File(filesDir, "file.txt")
252+
file.readBytes().toString(Charset.forName("UTF-8"))
253+
}
254+
255+
fun writeEncrypt() {
256+
val content = "some content"
257+
val encryptedFile = getEncryptedFile()
258+
encryptedFile.openFileOutput().apply {
259+
write(content.toByteArray(Charset.forName("UTF-8")))
260+
flush()
261+
close()
262+
}
263+
}
264+
265+
fun readEncrypt() {
266+
val encryptedFile = getEncryptedFile()
267+
encryptedFile.openFileInput().apply {
268+
val byteStream = ByteArrayOutputStream()
269+
var byte = read()
270+
while(byte != -1) {
271+
byteStream.write(byte)
272+
byte = read()
273+
}
274+
val content = byteStream.toByteArray()
275+
close()
276+
}
277+
}
278+
279+
fun getEncryptedFile(): EncryptedFile {
280+
return EncryptedFile.Builder(
281+
File(filesDir, "file.txt"),
282+
this,
283+
getMasterKeyAlias(),
284+
EncryptedFile.FileEncryptionScheme.AES256_GCM_HKDF_4KB
285+
).build()
286+
}
287+
288+
fun getMasterKeyAlias(): String {
289+
val keyGenParameterSpec = MasterKeys.AES256_GCM_SPEC
290+
return MasterKeys.getOrCreate(keyGenParameterSpec)
291+
}
292+
}
237293
{% endhighlight %}

0 commit comments

Comments
 (0)