根据散列确认文件内容 [英] Confirming file content against hash
问题描述
我有要求检查文件的内容的完整性。这些文件将写入CD / DVD,可能会被复制多次。这个想法是识别正确复制的拷贝(在它们从Nero等被移除之后)。
对此很新颖,但快速搜索表明 Arrays.hashCode(byte [])
将符合需要。我们可以在磁盘上包含一个包含每个感兴趣资源的调用结果的文件,然后将它与的
在检查时从磁盘读取。 byte []
进行比较。文件
我是否正确理解该方法,这是检查文件内容的有效方法吗? / p>
如果不是,则可以提供有关搜索关键字或策略/方法/类的建议。
基于Brendan答案的工作代码。它处理了VoidStar识别的问题(需要在内存中保存整个字节[]
以获取散列)。
import java.io.File;
import java.io.FileInputStream;
import java.util.zip.CRC32;
$ b $ class TestHash {
public static void main(String [] args)throws Exception {
File f = new File(TestHash.java);
FileInputStream fis = new FileInputStream(f);
CRC32 crcMaker =新的CRC32();
byte [] buffer = new byte [65536];
int bytesRead; ((bytesRead = fis.read(buffer))!= -1){
crcMaker.update(buffer,0,bytesRead);
}
long crc = crcMaker.getValue(); //这是你的错误检查代码
System.out.println(CRC代码是+ crc);
}
}
Arrays.hashCode()
被设计为非常快(在散列表中使用)。我强烈建议不要将它用于此目的。
您需要的是某种错误检查代码,如 CRC 。
Java正好有一个类来计算这些: CRC32 :
InputStream in = ...;
CRC32 crcMaker =新的CRC32();
byte [] buffer = new byte [someSize];
int bytesRead; ((bytesRead = in.read(buffer))!= -1){
crcMaker.update(buffer,0,bytesRead);
}
long crc = crcMaker.getValue(); //这是你的错误检查代码
I have a requirement to 'check the integrity' of the content of files. The files will be written to CD/DVD, which might be copied many times. The idea is to identify copies (after they are removed from Nero etc.) which copied correctly.
Am rather new to this, but a quick search suggests that Arrays.hashCode(byte[])
will fit the need. We can include a file on the disk that contains the result of that call for each resource of interest, then compare it to the byte[]
of the File
as read from disk when checked.
Do I understand the method correctly, is this a valid way to go about checking file content?
If not, suggestions as to search keywords or strategies/methods/classes would be appreciated.
Working code based on the answer of Brendan. It takes care of the problem identified by VoidStar (needing to hold the entire byte[]
in memory for getting the hash).
import java.io.File;
import java.io.FileInputStream;
import java.util.zip.CRC32;
class TestHash {
public static void main(String[] args) throws Exception {
File f = new File("TestHash.java");
FileInputStream fis = new FileInputStream(f);
CRC32 crcMaker = new CRC32();
byte[] buffer = new byte[65536];
int bytesRead;
while((bytesRead = fis.read(buffer)) != -1) {
crcMaker.update(buffer, 0, bytesRead);
}
long crc = crcMaker.getValue(); // This is your error checking code
System.out.println("CRC code is " + crc);
}
}
Arrays.hashCode()
is designed to be very fast (used in hash tables). I highly recommend not using it for this purpose.
What you want is some sort of error-checking code like a CRC.
Java happens to have a class for calculating these: CRC32:
InputStream in = ...;
CRC32 crcMaker = new CRC32();
byte[] buffer = new byte[someSize];
int bytesRead;
while((bytesRead = in.read(buffer)) != -1) {
crcMaker.update(buffer, 0, bytesRead);
}
long crc = crcMaker.getValue(); // This is your error checking code
这篇关于根据散列确认文件内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!