Skip to content

Commit 684caea

Browse files
committed
Upadate libs
1 parent 77b1f82 commit 684caea

5 files changed

Lines changed: 117 additions & 110 deletions

File tree

armadillo-datastore/build.gradle.kts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,18 +47,18 @@ android {
4747
dependencies {
4848
implementation(project(":armadillo"))
4949

50-
implementation("org.jetbrains.kotlin:kotlin-stdlib:1.4.10")
50+
implementation("org.jetbrains.kotlin:kotlin-stdlib:1.4.30")
5151

5252
implementation("androidx.core:core-ktx:1.3.2")
5353
implementation("androidx.appcompat:appcompat:1.2.0")
54-
implementation("androidx.datastore:datastore-core:1.0.0-alpha01")
54+
implementation("androidx.datastore:datastore-core:1.0.0-alpha07")
5555

56-
androidTestImplementation("org.jetbrains.kotlinx:kotlinx-serialization-protobuf:1.0.0")
56+
implementation("org.jetbrains.kotlinx:kotlinx-serialization-protobuf:1.0.0")
5757

58-
testImplementation("junit:junit:4.13")
58+
testImplementation("junit:junit:4.13.2")
5959
androidTestImplementation("androidx.test.ext:junit:1.1.2")
6060
androidTestImplementation("androidx.test.espresso:espresso-core:3.3.0")
61-
androidTestImplementation("org.bouncycastle:bcprov-jdk15on:1.60")
61+
androidTestImplementation("org.bouncycastle:bcprov-jdk15on:1.67")
6262
androidTestImplementation("org.mindrot:jbcrypt:0.4")
6363
androidTestImplementation("androidx.test.ext:junit:1.1.2")
6464
androidTestImplementation("androidx.test:rules:1.3.0")

armadillo-datastore/src/androidTest/java/at/favre/lib/armadillo/datastore/UserStore.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
package at.favre.lib.armadillo.datastore
22

33
import android.content.Context
4-
import androidx.datastore.DataStore
5-
import androidx.datastore.createDataStore
4+
import androidx.datastore.core.DataStore
5+
import androidx.datastore.core.createDataStore
66
import kotlinx.coroutines.flow.Flow
77
import kotlinx.serialization.ExperimentalSerializationApi
88
import kotlinx.serialization.protobuf.ProtoBuf

armadillo-datastore/src/main/java/at/favre/lib/armadillo/datastore/ArmadilloSerializer.kt

Lines changed: 104 additions & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package at.favre.lib.armadillo.datastore
22

33
import android.content.Context
44
import android.os.Build
5-
import androidx.datastore.Serializer
5+
import androidx.datastore.core.Serializer
66
import at.favre.lib.armadillo.*
77
import at.favre.lib.armadillo.Armadillo.CONTENT_KEY_OUT_BYTE_LENGTH
88
import at.favre.lib.armadillo.BuildConfig
@@ -12,116 +12,119 @@ import java.security.Provider
1212
import java.security.SecureRandom
1313

1414
class ArmadilloSerializer<T>(
15-
context: Context,
16-
private val protocol: ProtobufProtocol<T>,
17-
password: CharArray? = null,
18-
fingerprintData: List<String> = emptyList(),
19-
secureRandom: SecureRandom = SecureRandom(),
20-
additionalDecryptionConfigs: List<EncryptionProtocolConfig> = listOf(),
21-
enabledKitkatSupport: Boolean = false,
22-
provider: Provider? = null,
23-
preferencesSalt: ByteArray = BuildConfig.PREF_SALT
15+
context: Context,
16+
private val protocol: ProtobufProtocol<T>,
17+
password: CharArray? = null,
18+
fingerprintData: List<String> = emptyList(),
19+
secureRandom: SecureRandom = SecureRandom(),
20+
additionalDecryptionConfigs: List<EncryptionProtocolConfig> = listOf(),
21+
enabledKitkatSupport: Boolean = false,
22+
provider: Provider? = null,
23+
preferencesSalt: ByteArray = BuildConfig.PREF_SALT
2424
) : Serializer<T> {
2525

26-
private val serializerPassword: ByteArrayRuntimeObfuscator?
27-
private val encryptionProtocol: EncryptionProtocol
28-
private val fingerprint: EncryptionFingerprint = EncryptionFingerprintFactory.create(
29-
context,
30-
buildString { fingerprintData.forEach(::append) }
31-
)
32-
private val defaultConfig = EncryptionProtocolConfig.newDefaultConfig()
33-
private val kitKatConfig by lazy {
34-
@Suppress("DEPRECATION")
35-
EncryptionProtocolConfig.newBuilder(defaultConfig.build())
36-
.authenticatedEncryption(AesCbcEncryption(secureRandom, provider))
37-
.protocolVersion(Armadillo.KITKAT_PROTOCOL_VERSION)
38-
.build()
39-
}
40-
41-
init {
42-
43-
val stringMessageDigest = HkdfMessageDigest(
44-
BuildConfig.PREF_SALT,
45-
CONTENT_KEY_OUT_BYTE_LENGTH
26+
private val serializerPassword: ByteArrayRuntimeObfuscator?
27+
private val encryptionProtocol: EncryptionProtocol
28+
private val fingerprint: EncryptionFingerprint = EncryptionFingerprintFactory.create(
29+
context,
30+
buildString { fingerprintData.forEach(::append) }
4631
)
32+
private val defaultConfig = EncryptionProtocolConfig.newDefaultConfig()
33+
private val kitKatConfig by lazy {
34+
@Suppress("DEPRECATION")
35+
EncryptionProtocolConfig.newBuilder(defaultConfig.build())
36+
.authenticatedEncryption(AesCbcEncryption(secureRandom, provider))
37+
.protocolVersion(Armadillo.KITKAT_PROTOCOL_VERSION)
38+
.build()
39+
}
40+
41+
init {
42+
43+
val stringMessageDigest = HkdfMessageDigest(
44+
BuildConfig.PREF_SALT,
45+
CONTENT_KEY_OUT_BYTE_LENGTH
46+
)
47+
48+
val config =
49+
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
50+
kitKatConfig
51+
} else {
52+
EncryptionProtocolConfig
53+
.newBuilder(defaultConfig.build())
54+
.authenticatedEncryption(AesGcmEncryption(secureRandom, provider))
55+
.build()
56+
}
57+
checkKitKatSupport(config.authenticatedEncryption)
58+
59+
val factory = DefaultEncryptionProtocol.Factory(
60+
config,
61+
fingerprint,
62+
stringMessageDigest,
63+
secureRandom,
64+
false, // enableDerivedPasswordCache,
65+
if (enabledKitkatSupport) {
66+
additionalDecryptionConfigs + kitKatConfig
67+
} else {
68+
additionalDecryptionConfigs
69+
},
70+
)
71+
72+
encryptionProtocol = factory.create(preferencesSalt)
73+
serializerPassword = password?.let(factory::obfuscatePassword)
74+
}
4775

48-
val config =
49-
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
50-
kitKatConfig
51-
} else {
52-
EncryptionProtocolConfig
53-
.newBuilder(defaultConfig.build())
54-
.authenticatedEncryption(AesGcmEncryption(secureRandom, provider))
55-
.build()
76+
77+
private fun checkKitKatSupport(authenticatedEncryption: AuthenticatedEncryption) {
78+
if (Build.VERSION.SDK_INT == Build.VERSION_CODES.KITKAT && authenticatedEncryption.javaClass == AesGcmEncryption::class.java) {
79+
throw UnsupportedOperationException("aes gcm is not supported with KitKat, add support " +
80+
"manually with Armadillo.Builder.enableKitKatSupport()")
5681
}
57-
checkKitKatSupport(config.authenticatedEncryption)
58-
59-
val factory = DefaultEncryptionProtocol.Factory(
60-
config,
61-
fingerprint,
62-
stringMessageDigest,
63-
secureRandom,
64-
false, // enableDerivedPasswordCache,
65-
if (enabledKitkatSupport) {
66-
additionalDecryptionConfigs + kitKatConfig
67-
} else {
68-
additionalDecryptionConfigs
69-
},
70-
)
82+
}
7183

72-
encryptionProtocol = factory.create(preferencesSalt)
73-
serializerPassword = password?.let(factory::obfuscatePassword)
74-
}
84+
companion object {
85+
private const val CRYPTO_KEY = "ArmadilloStoreSerializer"
86+
}
7587

7688

77-
private fun checkKitKatSupport(authenticatedEncryption: AuthenticatedEncryption) {
78-
if (Build.VERSION.SDK_INT == Build.VERSION_CODES.KITKAT && authenticatedEncryption.javaClass == AesGcmEncryption::class.java) {
79-
throw UnsupportedOperationException("aes gcm is not supported with KitKat, add support " +
80-
"manually with Armadillo.Builder.enableKitKatSupport()")
89+
private fun encrypt(content: ByteArray): ByteArray = with(encryptionProtocol) {
90+
encrypt(
91+
deriveContentKey(CRYPTO_KEY),
92+
deobfuscatePassword(serializerPassword),
93+
content
94+
)
8195
}
82-
}
8396

84-
companion object {
85-
private const val CRYPTO_KEY = "ArmadilloStoreSerializer"
86-
}
8797

98+
private fun decrypt(encrypted: ByteArray): ByteArray? =
99+
if (encrypted.isEmpty()) {
100+
null
101+
} else {
102+
encryptionProtocol
103+
.decrypt(
104+
encryptionProtocol.deriveContentKey(CRYPTO_KEY),
105+
encryptionProtocol.deobfuscatePassword(serializerPassword),
106+
encrypted
107+
)
108+
}
109+
110+
override fun readFrom(input: InputStream): T =
111+
input
112+
.readBytes()
113+
.let(::decrypt)
114+
.let {
115+
val bytes = it ?: byteArrayOf()
116+
if (bytes.isEmpty()) defaultValue
117+
else protocol.decode(bytes)
118+
}
119+
120+
121+
override fun writeTo(t: T, output: OutputStream) {
122+
protocol
123+
.encode(t)
124+
.let(::encrypt)
125+
.also(output::write)
126+
}
88127

89-
private fun encrypt(content: ByteArray): ByteArray = with(encryptionProtocol) {
90-
encrypt(
91-
deriveContentKey(CRYPTO_KEY),
92-
deobfuscatePassword(serializerPassword),
93-
content
94-
)
95-
}
96-
97-
98-
private fun decrypt(encrypted: ByteArray): ByteArray? =
99-
if (encrypted.isEmpty()) {
100-
null
101-
} else {
102-
encryptionProtocol
103-
.decrypt(
104-
encryptionProtocol.deriveContentKey(CRYPTO_KEY),
105-
encryptionProtocol.deobfuscatePassword(serializerPassword),
106-
encrypted
107-
)
108-
}
109-
110-
override fun readFrom(input: InputStream): T =
111-
input
112-
.readBytes()
113-
.let(::decrypt)
114-
.let {
115-
val bytes = it ?: byteArrayOf()
116-
if (bytes.isEmpty()) protocol.default()
117-
else protocol.decode(bytes)
118-
}
119-
120-
121-
override fun writeTo(t: T, output: OutputStream) {
122-
protocol
123-
.encode(t)
124-
.let(::encrypt)
125-
.also(output::write)
126-
}
128+
override val defaultValue: T
129+
get() = protocol.default()
127130
}

armadillo-datastore/src/main/java/at/favre/lib/armadillo/datastore/ProtobufProtocol.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ package at.favre.lib.armadillo.datastore
88
* Datastore will always return an empty
99
*/
1010
interface ProtobufProtocol<T> {
11+
// /**
12+
// * If the file contents has change you must migrate the data
13+
// */
14+
// val version: Int
1115
/**
1216
* un-encrypted proto byte encoding of [T]
1317
*/

build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@ buildscript {
77
jcenter()
88
}
99
dependencies {
10-
classpath 'com.android.tools.build:gradle:4.1.0'
10+
classpath 'com.android.tools.build:gradle:4.1.2'
1111
classpath 'com.github.dcendents:android-maven-gradle-plugin:2.1'
1212
classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.8.5'
1313
classpath 'org.kt3k.gradle.plugin:coveralls-gradle-plugin:2.8.3'
1414
classpath 'com.vanniktech:gradle-android-junit-jacoco-plugin:0.16.0'
15-
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.4.10"
15+
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.4.30"
1616
}
1717
}
1818

0 commit comments

Comments
 (0)