From a8330e3bb2fd22fcb7bb4fbff3c1fadc83a26a7d Mon Sep 17 00:00:00 2001 From: Simon Mueller Date: Wed, 18 Oct 2023 17:27:25 +0200 Subject: [PATCH] Refactor calcPublicKey function --- .../asitplus/crypto/datatypes/io/Encoding.kt | 45 +++++++++---------- 1 file changed, 20 insertions(+), 25 deletions(-) diff --git a/datatypes/src/commonMain/kotlin/at/asitplus/crypto/datatypes/io/Encoding.kt b/datatypes/src/commonMain/kotlin/at/asitplus/crypto/datatypes/io/Encoding.kt index db71fb45..85ed6bba 100644 --- a/datatypes/src/commonMain/kotlin/at/asitplus/crypto/datatypes/io/Encoding.kt +++ b/datatypes/src/commonMain/kotlin/at/asitplus/crypto/datatypes/io/Encoding.kt @@ -125,35 +125,20 @@ object MultibaseHelper { it.removePrefix("m").decodeToByteArrayOrNull(Base64Strict) } else null - // No decompression, because that would need some EC math - private fun decodeEcKey(it: ByteArray?): Pair? { - if (it == null) return null - val half: Int = it.size.floorDiv(2) - val x = it.sliceArray(0 until half) - val y = it.sliceArray(half until it.size) - return Pair(x, y) + private fun decodeEcKey(it: ByteArray?): CryptoPublicKey? { + val test = it?.let { bytes -> byteArrayOf(0x04.toByte(), *bytes) } + return if (test != null) CryptoPublicKey.Ec.fromAnsiX963Bytes(test) else null } private fun decodeRsaKey(it: ByteArray?): CryptoPublicKey? { - return if ( it != null ) CryptoPublicKey.Rsa.fromPKCS1encoded(it) else null + return if (it != null) CryptoPublicKey.Rsa.fromPKCS1encoded(it) else null } - fun calcPublicKey(multiKey: Pair?): CryptoPublicKey? { - when (multiKey?.first) { - true -> { - val (xCoordinate, yCoordinate) = decodeEcKey( - multiKey.second - ) ?: return null - val curve = - EcCurve.entries.find { it.coordinateLengthBytes.toInt() == xCoordinate.size } ?: return null - return CryptoPublicKey.Ec(curve = curve, x = xCoordinate, y = yCoordinate) - } - - false -> { - return decodeRsaKey(multiKey.second) - } - - else -> return null + internal fun calcPublicKey(multiKey: Pair?): CryptoPublicKey? { + return when (multiKey?.first) { + true -> decodeEcKey(multiKey.second) + false -> decodeRsaKey(multiKey.second) + else -> null } } @@ -164,7 +149,7 @@ object MultibaseHelper { val multibaseDecode = multibaseDecode(stripped) val multiKey = multiKeyDecode(multibaseDecode) ?: return null - return if (multiKey.first) decodeEcKey(multiKey.second) else TODO("ASN1 decoding of RSA keys") + return decodeEcKeyDep(multiKey.second) } @Deprecated("Dependency of calcEncPublicKeyCoords - Use [multiKeyGetKty] instead ") @@ -177,4 +162,14 @@ object MultibaseHelper { false to it.drop(2).toByteArray() } else null } else null + + @Deprecated("Use [Ec.fromAnsiX963Bytes] instead") + // No decompression, because that would need some EC math + private fun decodeEcKeyDep(it: ByteArray?): Pair? { + if (it == null) return null + val half: Int = it.size.floorDiv(2) + val x = it.sliceArray(0 until half) + val y = it.sliceArray(half until it.size) + return Pair(x, y) + } } \ No newline at end of file