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 }