0

我正在使用用 BufferedInputStream 包装的 FileInputStream 来读取字节块中的大文件。

public void MD5(BufferedInputStream in, Key pubKey) throws Exception{

        Mac md = Mac.getInstance("HmacMD5");
            md.init(pubKey);
            byte[] contents = new byte[1024];
            int readSize;
            while ((readSize = in.read(contents)) != -1) {
                {
                    md.update(contents,0,readSize);
                }
                byte[] hashValue = md.doFinal();

            }
}

它对于一个小文件非常有效,但对于一个 200MB 的文件来说需要大量的时间。

当我尝试使用 SHA256withRSA 对 200MB 文件进行签名时,同样的方法效果很好。

这有什么具体原因吗??我觉得这与md.update() 有关。

但是我在使用“签名”时也使用了相同的功能。

任何帮助,将不胜感激。

4

1 回答 1

5

您在doFinalwhile 循环中调用。那看起来不对。尝试以下操作:

public void MD5(BufferedInputStream in, Key pubKey) throws Exception{
    Mac md = Mac.getInstance("HmacMD5");
    md.init(pubKey);
    byte[] contents = new byte[1024];
    int readSize;
    while ((readSize = in.read(contents)) != -1) {
        md.update(contents, 0, readSize);
    }
    byte[] hashValue = md.doFinal();
}
于 2017-09-30T19:28:14.213 回答