英文:
Java MD5 Copy function generates different digest
问题
以下是您要翻译的内容:
我正在尝试使用Java,并创建了一个复制文件并生成MD5校验和的小程序。程序能够生成校验和,但复制的结果文件与原始校验和不匹配。
我是Java的新手,不明白问题出在哪里。我是否将错误的缓冲区写入了输出文件?
package com.application;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.math.BigInteger;
import java.security.MessageDigest;
public class Main {
    static int secure_copy(String src, String dest) throws Exception {
        InputStream inFile = new FileInputStream(src);
        OutputStream outFile = new FileOutputStream(dest);
        MessageDigest md = MessageDigest.getInstance("MD5");
        byte[] buf = new byte[1024];
        int numRead;
        do {
            numRead = inFile.read(buf);
            if (numRead > 0) {
                md.update(buf, 0, numRead);
                outFile.write(buf);
                outFile.flush();
            }
        } while (numRead != -1);
        inFile.close();
        outFile.close();
        BigInteger no = new BigInteger(1, md.digest());
        String result = no.toString(16);
        while (result.length() < 32) {
            result = "0" + result;
        }
        System.out.println("MD5: " + result);
        return 0;
    }
    public static void main(String[] args) {
        try {
            secure_copy(args[0], args[1]);
        } catch (Exception e) {
            System.out.println("Error: " + e.getMessage());
        }
    }
}
从源文件的输出:(正确)
MD5: 503ea121d2bc6f1a2ede8eb47f0d13ef
复制函数生成的文件,通过md5sum检查:
md5sum file.mov 
56883109c28590c33fb31cc862619977  file.mov
英文:
I am experimenting with Java and created a small program that copies a file and generates a MD5 checksum. The program works and generates a checksum, but the resulting file that is copied does not match the original checksum.
I am new to Java and do not understand what the problem is here. Am I writing the wrong buffer to the output file?
package com.application;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.math.BigInteger;
import java.security.MessageDigest;
public class Main {
static int secure_copy(String src, String dest) throws Exception {
InputStream inFile = new FileInputStream(src);
OutputStream outFile = new FileOutputStream(dest);
MessageDigest md = MessageDigest.getInstance("MD5");
byte[] buf = new byte[1024];
int numRead;
do {
numRead = inFile.read(buf);
if (numRead > 0) {
md.update(buf, 0, numRead);
outFile.write(buf);
outFile.flush();
}
} while (numRead != -1);
inFile.close();
outFile.close();
BigInteger no = new BigInteger(1, md.digest());
String result = no.toString(16);
while(result.length() < 32) {
result = "0" + result;
}
System.out.println("MD5: " + result);
return 0;
}
public static void main(String[] args) {
try {
secure_copy(args[0], args[1]);
} catch (Exception e) {
System.out.println("Error: " + e.getMessage());
}
}
}
Output from source file: (Correct)
MD5: 503ea121d2bc6f1a2ede8eb47f0d13ef
The file from the copy function, checked via md5sum
md5sum file.mov 
56883109c28590c33fb31cc862619977  file.mov
答案1
得分: 2
你正在将整个缓冲区写入输出文件,而不仅仅是最新读取的数据部分。修复方法很简单:
if (numRead > 0) {
    md.update(buf, 0, numRead);
    outFile.write(buf, 0, numRead);
}
英文:
You are writing the entire buffer to the output file, not just the portion that has data from the latest read. The fix is simple:
        if (numRead > 0) {
md.update(buf, 0, numRead);
outFile.write(buf, 0, numRead);
}
答案2
得分: 0
在每次从InputStream读取时,代码不断更改要计算哈希值的数据。而不是在循环内调用md.update(buf, 0, numRead);,应该将整个文件读入一个byte[],然后一次调用md.update(entireFileByeArray)。(请参阅此答案以找到在打开文件之前确定适当数组大小的方法。)
英文:
On every read from the InputStream, the code is continually changing the data to calculate the hash of. Instead of calling md.update(buf, 0, numRead); within the loop, it should read the entire file into a byte[] and then call md.update(entireFileByeArray) once. (See this answer for a way to find the appropriate array size ahead of opening the file.)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论