From 4d775d3a1706d58ddecfac178d69fe8b3c6496eb Mon Sep 17 00:00:00 2001 From: Javi Rueda Date: Mon, 11 Jan 2021 20:44:03 +0100 Subject: [PATCH] Fix NoSuchMethod exception for HelperSHA256 on API Level prior to P --- app/build.gradle | 2 ++ .../java/io/lbry/browser/utils/HelperTest.java | 15 +++++++++++++++ .../main/java/io/lbry/browser/utils/Helper.java | 6 +++++- 3 files changed, 22 insertions(+), 1 deletion(-) create mode 100644 app/src/androidTest/java/io/lbry/browser/utils/HelperTest.java diff --git a/app/build.gradle b/app/build.gradle index f3fca2fe..cf72ba92 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -128,6 +128,8 @@ dependencies { annotationProcessor 'com.github.bumptech.glide:compiler:4.11.0' testImplementation 'junit:junit:4.12' + androidTestImplementation 'androidx.test:runner:1.3.0' + androidTestImplementation 'androidx.test:rules:1.3.0' androidTestImplementation 'androidx.test.ext:junit:1.1.2' androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0' diff --git a/app/src/androidTest/java/io/lbry/browser/utils/HelperTest.java b/app/src/androidTest/java/io/lbry/browser/utils/HelperTest.java new file mode 100644 index 00000000..f6caa36f --- /dev/null +++ b/app/src/androidTest/java/io/lbry/browser/utils/HelperTest.java @@ -0,0 +1,15 @@ +package io.lbry.browser.utils; + +import androidx.test.filters.SmallTest; + +import org.junit.Test; + +import static org.junit.Assert.assertEquals; + +@SmallTest +public class HelperTest { + @Test + public void SHA256() { + assertEquals("de9edb2044d012f04553e49b04d54cbec8e8a46a40ad5a19bc5dcce1da00ecfd", Helper.SHA256(String.valueOf(12345678912345L))); + } +} diff --git a/app/src/main/java/io/lbry/browser/utils/Helper.java b/app/src/main/java/io/lbry/browser/utils/Helper.java index 73bfa10f..cd117028 100644 --- a/app/src/main/java/io/lbry/browser/utils/Helper.java +++ b/app/src/main/java/io/lbry/browser/utils/Helper.java @@ -795,7 +795,11 @@ public final class Helper { try { MessageDigest digest = MessageDigest.getInstance("SHA-256"); byte[] hash = digest.digest(value.getBytes(StandardCharsets.UTF_8)); - return Hex.encodeHexString(hash, true); + + if (Build.VERSION.SDK_INT > Build.VERSION_CODES.O_MR1) + return Hex.encodeHexString(hash, true); + else + return new String(Hex.encodeHex(hash)).toLowerCase(); } catch (NoSuchAlgorithmException ex) { return null; }