大文件的hmac MD5

Hmac MD5 of a large file

我正在使用 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().

有关

但我在使用 'Signature' 时也使用了相同的功能。

如有任何帮助,我们将不胜感激。

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

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();
}