From a9909b37ba8dc23ce856c5d66f250ce1bc65cd7f Mon Sep 17 00:00:00 2001 From: Benoit Marty Date: Thu, 20 Feb 2020 19:54:35 +0100 Subject: [PATCH] Update after Valere's review --- .../crypto/verification/qrcode/HexParser.kt | 35 +++++++++ .../crypto/verification/qrcode/QrCodeTest.kt | 76 +------------------ .../crypto/verification/qrcode/Extensions.kt | 6 +- .../matrix/android/internal/util/Byte.kt | 25 ------ 4 files changed, 40 insertions(+), 102 deletions(-) create mode 100644 matrix-sdk-android/src/androidTest/java/im/vector/matrix/android/internal/crypto/verification/qrcode/HexParser.kt delete mode 100644 matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/util/Byte.kt diff --git a/matrix-sdk-android/src/androidTest/java/im/vector/matrix/android/internal/crypto/verification/qrcode/HexParser.kt b/matrix-sdk-android/src/androidTest/java/im/vector/matrix/android/internal/crypto/verification/qrcode/HexParser.kt new file mode 100644 index 0000000000..387b627713 --- /dev/null +++ b/matrix-sdk-android/src/androidTest/java/im/vector/matrix/android/internal/crypto/verification/qrcode/HexParser.kt @@ -0,0 +1,35 @@ +/* + * Copyright (c) 2020 New Vector Ltd + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package im.vector.matrix.android.internal.crypto.verification.qrcode + +fun hexToByteArray(hex: String): ByteArray { + // Remove all spaces + return hex.replace(" ", "") + .let { + if (it.length % 2 != 0) "0$it" else it + } + .let { + ByteArray(it.length / 2) + .apply { + for (i in this.indices) { + val index = i * 2 + val v = it.substring(index, index + 2).toInt(16) + this[i] = v.toByte() + } + } + } +} diff --git a/matrix-sdk-android/src/androidTest/java/im/vector/matrix/android/internal/crypto/verification/qrcode/QrCodeTest.kt b/matrix-sdk-android/src/androidTest/java/im/vector/matrix/android/internal/crypto/verification/qrcode/QrCodeTest.kt index ced7896aa3..d19fad4b59 100644 --- a/matrix-sdk-android/src/androidTest/java/im/vector/matrix/android/internal/crypto/verification/qrcode/QrCodeTest.kt +++ b/matrix-sdk-android/src/androidTest/java/im/vector/matrix/android/internal/crypto/verification/qrcode/QrCodeTest.kt @@ -60,81 +60,9 @@ class QrCodeTest : InstrumentedTest { private val sharedSecretByteArray = "12345678".toByteArray(Charsets.ISO_8859_1) - // 4d 79 6e 64 a4 d9 2e f4 91 58 e4 cf 94 ea 8b ab 9d f8 6c 0f bf 2b 8c cb 14 a4 ae f5 c1 8b 41 a5 - private val tlx_byteArray = ByteArray(32) { - when (it) { - 0 -> 0x4D.toByte() - 1 -> 0x79.toByte() - 2 -> 0x6E.toByte() - 3 -> 0x64.toByte() - 4 -> 0xA4.toByte() - 5 -> 0xD9.toByte() - 6 -> 0x2E.toByte() - 7 -> 0xF4.toByte() - 8 -> 0x91.toByte() - 9 -> 0x58.toByte() - 10 -> 0xE4.toByte() - 11 -> 0xCF.toByte() - 12 -> 0x94.toByte() - 13 -> 0xEA.toByte() - 14 -> 0x8B.toByte() - 15 -> 0xAB.toByte() - 16 -> 0x9D.toByte() - 17 -> 0xF8.toByte() - 18 -> 0x6C.toByte() - 19 -> 0x0F.toByte() - 20 -> 0xBF.toByte() - 21 -> 0x2B.toByte() - 22 -> 0x8C.toByte() - 23 -> 0xCB.toByte() - 24 -> 0x14.toByte() - 25 -> 0xA4.toByte() - 26 -> 0xAE.toByte() - 27 -> 0xF5.toByte() - 28 -> 0xC1.toByte() - 29 -> 0x8B.toByte() - 30 -> 0x41.toByte() - else -> 0xA5.toByte() - } - } + private val tlx_byteArray = hexToByteArray("4d 79 6e 64 a4 d9 2e f4 91 58 e4 cf 94 ea 8b ab 9d f8 6c 0f bf 2b 8c cb 14 a4 ae f5 c1 8b 41 a5") - // 92 d1 30 71 43 fa b2 ed 71 87 e1 ae 13 e0 98 91 0d c7 e9 6f c3 22 5f b2 6c 71 5d 68 43 ab a2 55 - private val kte_byteArray = ByteArray(32) { - when (it) { - 0 -> 0x92.toByte() - 1 -> 0xd1.toByte() - 2 -> 0x30.toByte() - 3 -> 0x71.toByte() - 4 -> 0x43.toByte() - 5 -> 0xfa.toByte() - 6 -> 0xb2.toByte() - 7 -> 0xed.toByte() - 8 -> 0x71.toByte() - 9 -> 0x87.toByte() - 10 -> 0xe1.toByte() - 11 -> 0xae.toByte() - 12 -> 0x13.toByte() - 13 -> 0xe0.toByte() - 14 -> 0x98.toByte() - 15 -> 0x91.toByte() - 16 -> 0x0d.toByte() - 17 -> 0xc7.toByte() - 18 -> 0xe9.toByte() - 19 -> 0x6f.toByte() - 20 -> 0xc3.toByte() - 21 -> 0x22.toByte() - 22 -> 0x5f.toByte() - 23 -> 0xb2.toByte() - 24 -> 0x6c.toByte() - 25 -> 0x71.toByte() - 26 -> 0x5d.toByte() - 27 -> 0x68.toByte() - 28 -> 0x43.toByte() - 29 -> 0xab.toByte() - 30 -> 0xa2.toByte() - else -> 0x55.toByte() - } - } + private val kte_byteArray = hexToByteArray("92 d1 30 71 43 fa b2 ed 71 87 e1 ae 13 e0 98 91 0d c7 e9 6f c3 22 5f b2 6c 71 5d 68 43 ab a2 55") @Test fun testEncoding1() { diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/crypto/verification/qrcode/Extensions.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/crypto/verification/qrcode/Extensions.kt index f6cc5b1be1..da926a0e10 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/crypto/verification/qrcode/Extensions.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/crypto/verification/qrcode/Extensions.kt @@ -18,7 +18,7 @@ package im.vector.matrix.android.internal.crypto.verification.qrcode import im.vector.matrix.android.internal.crypto.crosssigning.fromBase64NoPadding import im.vector.matrix.android.internal.crypto.crosssigning.toBase64NoPadding -import im.vector.matrix.android.internal.util.toPositiveInt +import im.vector.matrix.android.internal.extensions.toUnsignedInt // MATRIX private val prefix = "MATRIX".toByteArray(Charsets.ISO_8859_1) @@ -95,8 +95,8 @@ fun String.toQrCodeData(): QrCodeData? { cursor++ // Get transaction length - val bigEndian1 = byteArray[cursor].toPositiveInt() - val bigEndian2 = byteArray[cursor + 1].toPositiveInt() + val bigEndian1 = byteArray[cursor].toUnsignedInt() + val bigEndian2 = byteArray[cursor + 1].toUnsignedInt() val transactionLength = bigEndian1 * 0x0100 + bigEndian2 diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/util/Byte.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/util/Byte.kt deleted file mode 100644 index 77f28e17e5..0000000000 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/util/Byte.kt +++ /dev/null @@ -1,25 +0,0 @@ -/* - * Copyright (c) 2020 New Vector Ltd - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package im.vector.matrix.android.internal.util - -fun Byte.toPositiveInt(): Int { - return if (this < 0) { - 256 + this - } else { - toInt() - } -}