Skip to content

Commit f726d16

Browse files
committed
Fix wrong sha256 conversion and add unit test.
1 parent 51c20b4 commit f726d16

File tree

4 files changed

+70
-10
lines changed

4 files changed

+70
-10
lines changed

matrix-sdk-android/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,7 @@ dependencies {
233233
testImplementation 'net.lachlanmckee:timber-junit-rule:1.0.1'
234234
// Transitively required for mocking realm as monarchy doesn't expose Rx
235235
testImplementation libs.rx.rxKotlin
236+
testImplementation libs.tests.robolectric
236237

237238
kaptAndroidTest libs.dagger.daggerCompiler
238239
androidTestImplementation libs.androidx.testCore

matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/identity/IdentityBulkLookupTask.kt

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ import org.matrix.android.sdk.internal.session.identity.model.IdentityLookUpPara
3030
import org.matrix.android.sdk.internal.session.identity.model.IdentityLookUpResponse
3131
import org.matrix.android.sdk.internal.task.Task
3232
import org.matrix.android.sdk.internal.util.base64ToBase64Url
33-
import java.security.MessageDigest
3433
import java.util.Locale
3534
import javax.inject.Inject
3635

@@ -43,7 +42,8 @@ internal interface IdentityBulkLookupTask : Task<IdentityBulkLookupTask.Params,
4342
internal class DefaultIdentityBulkLookupTask @Inject constructor(
4443
private val identityApiProvider: IdentityApiProvider,
4544
private val identityStore: IdentityStore,
46-
@UserId private val userId: String
45+
@UserId private val userId: String,
46+
private val sha256Converter: Sha256Converter,
4747
) : IdentityBulkLookupTask {
4848

4949
override suspend fun execute(params: IdentityBulkLookupTask.Params): List<FoundThreePid> {
@@ -120,7 +120,9 @@ internal class DefaultIdentityBulkLookupTask @Inject constructor(
120120
private fun getHashedAddresses(threePids: List<ThreePid>, pepper: String): List<String> {
121121
return threePids.map { threePid ->
122122
base64ToBase64Url(
123-
(threePid.value.lowercase(Locale.ROOT) + " " + threePid.toMedium() + " " + pepper).toSha256()
123+
sha256Converter.convertToSha256(
124+
str = threePid.value.lowercase(Locale.ROOT) + " " + threePid.toMedium() + " " + pepper
125+
)
124126
)
125127
}
126128
}
@@ -139,11 +141,4 @@ internal class DefaultIdentityBulkLookupTask @Inject constructor(
139141
)
140142
}
141143
}
142-
143-
private val sha256 by lazy { MessageDigest.getInstance("SHA-256") }
144-
145-
@OptIn(ExperimentalStdlibApi::class)
146-
private fun String.toSha256(): String {
147-
return sha256.digest(toByteArray()).toHexString()
148-
}
149144
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
* Copyright (c) 2024 New Vector Ltd
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.matrix.android.sdk.internal.session.identity
18+
19+
import org.matrix.android.sdk.api.util.toBase64NoPadding
20+
import java.security.MessageDigest
21+
import javax.inject.Inject
22+
23+
class Sha256Converter @Inject constructor() {
24+
private val sha256 by lazy { MessageDigest.getInstance("SHA-256") }
25+
26+
fun convertToSha256(str: String): String {
27+
return sha256.digest(str.toByteArray()).toBase64NoPadding()
28+
}
29+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
* Copyright (c) 2024 New Vector Ltd
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.matrix.android.sdk.internal.session.identity
18+
19+
import org.amshove.kluent.shouldBeEqualTo
20+
import org.junit.Test
21+
import org.junit.runner.RunWith
22+
import org.robolectric.RobolectricTestRunner
23+
24+
@RunWith(RobolectricTestRunner::class)
25+
class Sha256Test {
26+
/**
27+
* Check that the behavior is the same than what is done in the Olm library.
28+
* https://gitlab.matrix.org/matrix-org/olm/-/blob/master/tests/test_olm_sha256.cpp#L16
29+
*/
30+
@Test
31+
fun testSha256() {
32+
val sut = Sha256Converter()
33+
sut.convertToSha256("Hello, World") shouldBeEqualTo "A2daxT/5zRU1zMffzfosRYxSGDcfQY3BNvLRmsH76KU"
34+
}
35+
}

0 commit comments

Comments
 (0)