From 172751ea9838f5e368d862342dd24fb8b2eb66fc Mon Sep 17 00:00:00 2001 From: Hans-Christoph Steiner Date: Wed, 26 Jan 2022 10:45:34 +0100 Subject: [PATCH] rename Utils.getBinaryHash() to Utils.getFileHexDigest() Since this method works on any file, not just APKs or binaries... --- app/src/main/java/org/fdroid/fdroid/Utils.java | 14 +++++++------- app/src/main/java/org/fdroid/fdroid/data/App.java | 6 +++--- .../fdroid/data/InstalledAppProviderService.java | 2 +- app/src/test/java/org/fdroid/fdroid/UtilsTest.java | 10 +++++----- 4 files changed, 16 insertions(+), 16 deletions(-) diff --git a/app/src/main/java/org/fdroid/fdroid/Utils.java b/app/src/main/java/org/fdroid/fdroid/Utils.java index 3f0353345..6f66583e8 100644 --- a/app/src/main/java/org/fdroid/fdroid/Utils.java +++ b/app/src/main/java/org/fdroid/fdroid/Utils.java @@ -500,8 +500,8 @@ public final class Utils { } /** - * Get the checksum hash of the file {@code apk} using the algorithm in {@code algo}. - * {@code apk} must exist on the filesystem and {@code algo} must be supported + * Get the checksum hash of the file {@code file} using the algorithm in {@code hashAlgo}. + * {@code file} must exist on the filesystem and {@code hashAlgo} must be supported * by this device, otherwise an {@link IllegalArgumentException} is thrown. This * method must be very defensive about checking whether the file exists, since APKs * can be uninstalled/deleted in background at any time, even if this is in the @@ -514,11 +514,11 @@ public final class Utils { * for more detail. */ @Nullable - public static String getBinaryHash(File apk, String algo) { + public static String getFileHexDigest(File file, String hashAlgo) { FileInputStream fis = null; try { - MessageDigest md = MessageDigest.getInstance(algo); - fis = new FileInputStream(apk); + MessageDigest md = MessageDigest.getInstance(hashAlgo); + fis = new FileInputStream(file); BufferedInputStream bis = new BufferedInputStream(fis); byte[] dataBytes = new byte[8192]; @@ -532,9 +532,9 @@ public final class Utils { } catch (IOException e) { String message = e.getMessage(); if (message.contains("read failed: EIO (I/O error)")) { - Utils.debugLog(TAG, "potential filesystem corruption while accessing " + apk + ": " + message); + Utils.debugLog(TAG, "potential filesystem corruption while accessing " + file + ": " + message); } else if (message.contains(" ENOENT ")) { - Utils.debugLog(TAG, apk + " vanished: " + message); + Utils.debugLog(TAG, file + " vanished: " + message); } } catch (NoSuchAlgorithmException e) { throw new IllegalArgumentException(e); diff --git a/app/src/main/java/org/fdroid/fdroid/data/App.java b/app/src/main/java/org/fdroid/fdroid/data/App.java index bb975679a..3c35debf3 100644 --- a/app/src/main/java/org/fdroid/fdroid/data/App.java +++ b/app/src/main/java/org/fdroid/fdroid/data/App.java @@ -431,7 +431,7 @@ public class App extends ValueObject implements Comparable, Parcelable { app.installedApk.hash = installedApp.getHash(); } else if (apkFile.canRead()) { String hashType = "sha256"; - String hash = Utils.getBinaryHash(apkFile, hashType); + String hash = Utils.getFileHexDigest(apkFile, hashType); if (TextUtils.isEmpty(hash)) { return null; } @@ -876,10 +876,10 @@ public class App extends ValueObject implements Comparable, Parcelable { if (Integer.parseInt(segments[1]) <= apk.versionCode) { if ("main".equals(segments[0])) { apk.obbMainFile = filename; - apk.obbMainFileSha256 = Utils.getBinaryHash(f, apk.hashType); + apk.obbMainFileSha256 = Utils.getFileHexDigest(f, apk.hashType); } else if ("patch".equals(segments[0])) { apk.obbPatchFile = filename; - apk.obbPatchFileSha256 = Utils.getBinaryHash(f, apk.hashType); + apk.obbPatchFileSha256 = Utils.getFileHexDigest(f, apk.hashType); } } } diff --git a/app/src/main/java/org/fdroid/fdroid/data/InstalledAppProviderService.java b/app/src/main/java/org/fdroid/fdroid/data/InstalledAppProviderService.java index a285d680f..0f3024bf5 100644 --- a/app/src/main/java/org/fdroid/fdroid/data/InstalledAppProviderService.java +++ b/app/src/main/java/org/fdroid/fdroid/data/InstalledAppProviderService.java @@ -299,7 +299,7 @@ public class InstalledAppProviderService extends JobIntentService { if (apk.exists() && apk.canRead()) { try { String hashType = "sha256"; - String hash = Utils.getBinaryHash(apk, hashType); + String hash = Utils.getFileHexDigest(apk, hashType); insertAppIntoDb(this, packageInfo, hashType, hash); } catch (IllegalArgumentException e) { Utils.debugLog(TAG, e.getMessage()); diff --git a/app/src/test/java/org/fdroid/fdroid/UtilsTest.java b/app/src/test/java/org/fdroid/fdroid/UtilsTest.java index 044157daa..fe202a874 100644 --- a/app/src/test/java/org/fdroid/fdroid/UtilsTest.java +++ b/app/src/test/java/org/fdroid/fdroid/UtilsTest.java @@ -204,19 +204,19 @@ public class UtilsTest { } @Test - public void testGetBinaryHash() { + public void testGetFileHexDigest() { File f = TestUtils.copyResourceToTempFile("largeRepo.xml"); assertEquals("df1754aa4b56c86c06d7842dfd02064f0781c1f740f489d3fc158bb541c8d197", - Utils.getBinaryHash(f, "sha256")); + Utils.getFileHexDigest(f, "sha256")); f = TestUtils.copyResourceToTempFile("masterKeyIndex.jar"); assertEquals("625d5aedcd0499fe04ebab81f3c7ae30c236cee653a914ffb587d890198f3aba", - Utils.getBinaryHash(f, "sha256")); + Utils.getFileHexDigest(f, "sha256")); f = TestUtils.copyResourceToTempFile("index.fdroid.2016-10-30.jar"); assertEquals("c138b503c6475aa749585d0e3ad4dba3546b6d33ec485efd8ac8bd603d93fedb", - Utils.getBinaryHash(f, "sha256")); + Utils.getFileHexDigest(f, "sha-256")); f = TestUtils.copyResourceToTempFile("index.fdroid.2016-11-10.jar"); assertEquals("93bea45814fd8955cabb957e7a3f8790d6c568eaa16fa30425c2d26c60490bde", - Utils.getBinaryHash(f, "sha256")); + Utils.getFileHexDigest(f, "SHA-256")); } // TODO write tests that work with a Certificate