From 3cc00caeabf7c55b677f3a4e35ac3d2361c80dd1 Mon Sep 17 00:00:00 2001 From: Andrew Date: Fri, 28 Jun 2024 12:43:40 +0930 Subject: [PATCH] Check recovery password is hex if its not a mnemonic --- .../java/org/session/libsignal/crypto/MnemonicCodec.kt | 2 ++ .../src/main/java/org/session/libsignal/utilities/Hex.kt | 9 +++++++++ 2 files changed, 11 insertions(+) create mode 100644 libsignal/src/main/java/org/session/libsignal/utilities/Hex.kt diff --git a/libsignal/src/main/java/org/session/libsignal/crypto/MnemonicCodec.kt b/libsignal/src/main/java/org/session/libsignal/crypto/MnemonicCodec.kt index a2ce5e5c53..12cb092b00 100644 --- a/libsignal/src/main/java/org/session/libsignal/crypto/MnemonicCodec.kt +++ b/libsignal/src/main/java/org/session/libsignal/crypto/MnemonicCodec.kt @@ -1,6 +1,7 @@ package org.session.libsignal.crypto import org.session.libsignal.utilities.Hex +import org.session.libsignal.utilities.isHex import java.util.zip.CRC32 /** @@ -126,6 +127,7 @@ class MnemonicCodec(private val loadFileContents: (String) -> String) { fun decodeMnemonicOrHexAsByteArray(mnemonicOrHex: String): ByteArray = try { decode(mnemonic = mnemonicOrHex).let(Hex::fromStringCondensed) } catch (decodeException: Exception) { + if (mnemonicOrHex.isHex()) throw decodeException try { Hex.fromStringCondensed(mnemonicOrHex) } catch (_: Exception) { diff --git a/libsignal/src/main/java/org/session/libsignal/utilities/Hex.kt b/libsignal/src/main/java/org/session/libsignal/utilities/Hex.kt new file mode 100644 index 0000000000..661f089068 --- /dev/null +++ b/libsignal/src/main/java/org/session/libsignal/utilities/Hex.kt @@ -0,0 +1,9 @@ +package org.session.libsignal.utilities + +private val HEX_CHARS = buildSet { + addAll('0'..'9') + addAll('a'..'f') + addAll('A'..'F') +} + +fun String.isHex() = all { it in HEX_CHARS }