1

アプリケーションから署名されたjarを確認する必要があります。次のように、すべてのコンテンツを読むことでそれができることがわかりました。

public boolean verifyJar(String filePath) {
    try {
        JarFile jar = new JarFile(filePath, true);
        Enumeration<JarEntry> entries = jar.entries();
        while (entries.hasMoreElements()) {
            JarEntry entry = entries.nextElement();
            InputStream is = jar.getInputStream(entry);
            byte[] buffer = new byte[10000];
            while (is.read(buffer, 0, buffer.length) != -1) {
                // we just read. this will throw a SecurityException
                // if a signature/digest check fails.
            }
            is.close();
        }
        return true;
    } catch (Exception e) {
        return false;
    }
}

有効なjarを使用してチェッカーを実行すると、合格します。半分に切って瓶を壊すと失敗します。しかし、1つのプロセスで両方を実行すると、2番目のチェックに合格します(以前のバージョンのファイルを読み取ったかのように)。

public static void main(String[] args) throws Exception {
    String path = "src/test/resources/temp/lib.jar";
    // Passes - that's good
    System.out.println(new Validator().verifyJar(path));

    byte[] content = FileUtil.readFile(path);
    FileUtil.save(path, Arrays.copyOf(content, content.length / 2));
    // Passes - but it shouldn't.
    // Fails if the first check is commented out though.
    System.out.println(new Validator().verifyJar(path));
}

したがって、それはのように見えるZipFileか、JarFile何らかの形でキャッシュされます。この動作を抑制するにはどうすればよいですか?

4

1 に答える 1

0

ネイティブコードがキャッシュされないようにするには、ZipFileを閉じる必要があります。パスとFile.lastModifiedが同じ場合、 Iirc ZipFileは同じハンドル(jzfile)をラップします。

または、File.lastModifiedにタッチすることでもうまくいく可能性がありますが、リソースリークを防ぐために、開いているもの(ZipFileを含む)を手動で閉じる必要があります。

于 2011-06-08T08:25:55.387 に答える