java - 禁止或不允许修改访问时间java

标签 java file ant md5 file-access

我正在编写一个 Java 类,它扩展了 Ant Zip Task 来为我完成特定的工作。我想创建一个 zip 文件,一旦创建了该文件,我就想抑制 inode 中的访问时间,这样即使文件被修改,我也无法修改或找到不让它改变的方法。这样做的原因是我制作了一个取决于访问时间的 md5 散列。因此,这给我带来了很多麻烦,让访问时间保持不变将解决我的问题。 现在有人知道我将如何做到这一点吗? 谢谢!

最佳答案

我之前不得不解决类似的问题 - 也许这是您的选择。就我而言,问题是:

We made a jar file and then ran an secure hash algorithm on the jar file. Because the jar file is really a zip file, and a zip file internally contains file metadata information including last access time, if we create a new jar file from the exact same source material, then the hash on the new jar file doesn't match the original hash (because while the zip contents are the same, the metadata stored in the zip file has different file creation / access times).

基本上,出于合规性目的,我们需要能够计算安全散列,以便能够轻松显示 jar 的内容未更改。重新编译等效的 jar 没问题 - 只是内容必须相同。

我们编写了一组简单的工具,专门为 zip/jar 文件执行安全散列(和验证)。它计算了两个哈希值:

  • 文件的常规安全散列(它将识别完全相同的 jar - 这将与标准 md5sum 的输出相同)
  • 通过迭代 zip/jar 的解压缩内容的字节计算的“仅内容”哈希(因此可用于识别重新编译的 jar 与原始 jar 匹配)

为了实现仅内容哈希,我们使用了 ZipInputStream遍历 zip 条目。

MessageDigest sha1;
byte[] digest;

for (each zip file entry)
{
  if (entry represents a directory)
  {
    sha1.update( directory name bytes as UTF-8 );
  }
  else
  {
    read the entry bytes using ZipInputStream.read()
    sha1.update( bytes );
  }
}

digest = sha1.digest(); 

另请参阅:ZipInputStream.read()

但是请注意,有些文件(如 list )可能包含用于创建 jar 的 ant 版本以及用于编译类的编译器版本等信息。因此,您必须从等效环境进行编译才能使哈希匹配。

最后,这无法解决 zip 文件本身可能包含其他 zip 文件的事实。虽然可以直接让检查满足这一要求并进入嵌套的 zip/jar/war 文件,但我们的实现并非如此。

关于java - 禁止或不允许修改访问时间java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10904265/

相关文章:

java - Groovy:list.pop() 不起作用

java - 更改 JFrame 大小时,在 GridLayout 中添加 ScrollPane 不会调整 JPane 中 JTextarea 的大小

java - 在 Java 中监视文件

file - Postgres COPY TO/FROM A FILE 作为非 super 用户

java - 如何在java中获取任何文件的图标/图像?

java - 用JDK1.7编译,在JRE 1.8中运行 : possible to install security patches?

java - Android 操作栏标题文本在某些手机上不可点击

java - 使用ant编译D2RQ

ant - 如何为foreach调用多个ant目标

Ant + Vista 64 : "Unable to locate tools.jar" (jre/jdk conflict? )