代码之家  ›  专栏  ›  技术社区  ›  TheGribble

尝试在Android中创建应用校验和,但没有改变

  •  1
  • TheGribble  · 技术社区  · 7 年前

    我正在尝试这个,公然从 https://www.programcreek.com/java-api-examples/index.php?class=java.util.zip.CheckedInputStream&method=read :

    String apkPath = App.getContext().getPackageCodePath();
    Log.i("Checksum", "getting checksum for " + apkPath);
    Long chksum = null;
    Log.i("Checksum", "Size: " + new File(apkPath).length());
    Log.i("Checksum", "Date: " + new Date(new File(apkPath).lastModified()));
    
    try {
        // Open the file and build a CRC32 checksum.
        FileInputStream fis = new FileInputStream(new File(apkPath));
        CRC32 chk = new CRC32();
        CheckedInputStream cis = new CheckedInputStream(fis, chk);
        byte[] buff = new byte[80];
        while (cis.read(buff) >= 0) ;
        chksum = chk.getValue();
    } catch (Exception e) {
        e.printStackTrace();
    }
    Log.i("Checksum", "Checksum is  " + chksum);
    

    运行良好,输出良好:

    09-27 15:17:35.775 17288-17288/com.test.checksum.checksum I/Checksum: getting checksum for /data/app/com.test.checksum.checksum-2/base.apk
    09-27 15:17:35.775 17288-17288/com.test.checksum.checksum I/Checksum: Size: 3245610
    09-27 15:17:35.775 17288-17288/com.test.checksum.checksum I/Checksum: Date: Wed Sep 27 15:17:03 BST 2017
    09-27 15:17:37.075 17288-17288/com.test.checksum.checksum I/Checksum: And Checksum is  0DAD53E0
    

    问题是,更改代码似乎没有任何区别-因此,以下代码:

    String apkPath = App.getContext().getPackageCodePath();
    Log.i("Checksum", "getting checksum for " + apkPath);
    Long chksum = null;
    Log.i("Checksum", "Size: " + new File(apkPath).length());
    Log.i("Checksum", "Date: " + new Date(new File(apkPath).lastModified()));
    
    //do something differently
    Log.i("Checksum", "And now do something else!");
    Toast toast = Toast.makeText(getApplicationContext(), "I'm doing something else!", Toast.LENGTH_SHORT);
    toast.show();
    
    try {
        // Open the file and build a CRC32 checksum.
        FileInputStream fis = new FileInputStream(new File(apkPath));
        CRC32 chk = new CRC32();
        CheckedInputStream cis = new CheckedInputStream(fis, chk);
        byte[] buff = new byte[80];
        while (cis.read(buff) >= 0) ;
        chksum = chk.getValue();
    } catch (Exception e) {
        e.printStackTrace();
    }
    Log.i("Checksum", "Checksum is  " + String.format("%08X", chksum));
    

    生成此输出:

    09-27 15:45:51.085 29517-29517/com.test.checksum.checksum I/Checksum: getting checksum for /data/app/com.test.checksum.checksum-2/base.apk
    09-27 15:45:51.085 29517-29517/com.test.checksum.checksum I/Checksum: Size: 3245610
    09-27 15:45:51.085 29517-29517/com.test.checksum.checksum I/Checksum: Date: Wed Sep 27 15:45:30 BST 2017
    09-27 15:45:51.085 29517-29517/com.test.checksum.checksum I/Checksum: And now do something else!
    09-27 15:45:52.335 29517-29517/com.test.checksum.checksum I/Checksum: Checksum is  0DAD53E0
    

    File().list()

    谢谢

    2 回复  |  直到 7 年前
        1
  •  0
  •   user1531971 user1531971    7 年前

    cis.getChecksum().getValue()

    您所做的是从一个新实例化的CRC32对象中获取相同的长值,该对象从未更改。

    通常创建流:

    CheckedInputStream cis = new CheckedInputStream(fis, new CRC32())

    也就是说,传入的校验和不是“out”变量。一旦计算完成,您必须从流中获取它。

        2
  •  0
  •   TheGribble    7 年前

    但是,如果我用稍微不同的代码构建两个APK文件并手动安装,那么一切似乎都很好——安装每个APK文件都会给出一个单独的校验和,但每个文件的校验和是一致的。

    我只能假设Android Studio调试器安装了一个标准或核心APK文件,并在调试器中运行时从其他地方加载代码,如Visual Studio。vshost。exe文件。

    谢谢你抽出时间,

    本。