【发布时间】:2012-02-04 13:08:35
【问题描述】:
我想从文件中的特定字节创建一个 md5 校验和。 校验和将来自文件的 100 个字节。
我写了这段代码:
public static String getMD5ChecksumByFlash(String filename) throws Exception {
InputStream fis = new FileInputStream(filename);
byte[] buffer = new byte[1];
MessageDigest complete = MessageDigest.getInstance("MD5");
int passes = fis.available() / 100;
int currentOffset = 0;
int readBytes = -1;
do {
System.out.println("0a "+currentOffset);
System.out.println("0b "+readBytes);
readBytes = fis.read(buffer, currentOffset, 1);
System.out.println("1 "+currentOffset);
System.out.println("2 "+readBytes);
if ( readBytes!=-1 ) {
complete.update(buffer, 0, readBytes);
currentOffset += passes;
System.out.println("4 "+readBytes);
}
System.out.println("3 "+currentOffset);
System.out.println("5 "+readBytes);
} while ( readBytes!=-1 );
fis.close();
byte[] b = complete.digest();
String result = "";
for (int i = 0; i < b.length; i++) {
result += Integer.toString((b[i] & 0xff) + 0x100, 16).substring(1);
}
return result;
}
但它不起作用:/ 它返回:
0a 0
0b -1
1 0
2 1
4 1
3 93928
5 1
0a 93928
0b 1
null
怎么了?
附言。 该文件不是 1 字节文件 - 它是 pidgin-2.10.1.exe
【问题讨论】:
-
代码有意义。它尝试一次将一个字节读入缓冲区,在位置 0、(filesize*1/10)、(filesize*2/10) 等处,以从各种偏移量创建 100 个字节的 MD5 总和。但似乎 Java 从文件偏移量 0 到文件偏移量 93928 寻找一些问题。
-
您意识到您仍然只会从文件的开头读取,对吧?你不是在寻找或类似的东西......
-
@Jon:查看docs.oracle.com/javase/1.4.2/docs/api/java/io/… 的文档,这应该是因为
currentOffset被传递给read函数。 -
@schnaader:这是字节数组的偏移量,而不是文件。而且字节数组只有1长,所以没有意义。
-
@schnaader 再次阅读文档。
off参数是字节数组的偏移量,而不是文件的偏移量。 (有趣的是,它下面的函数正是你想要的)