Fix NoSuchMethod exception for HelperSHA256 on API Level prior to P #1117

Merged
kekkyojin merged 1 commit from fix-nosuchmethod-commons into master 2021-01-12 08:32:09 +01:00
3 changed files with 22 additions and 1 deletions

View file

@ -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'

View file

@ -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)));
}
}

View file

@ -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;
}