使用 JCA 高效检查大型数据集的签名
我必须验证一个可能大至 2Gb 的文件的签名,并且我希望以尽可能节省内存的方式来执行此操作。 由于各种原因,该文件已经完全加载到内存中,并由应用程序使用 InputStream
进行访问。 我想使用流接口验证签名,但 JCA Signature
类的 update
方法仅接受 byte[]
和相关类。
我怎样才能有效地做到这一点? 我不想将野兽加载到第二个字节数组中,否则我们将看到一些严重高的内存使用量,但该接口似乎不支持它。
更新
如果重要的话,签名算法是 SHA-1
I have to verify the signature on a file that may be as large as 2Gb, and I want to do so in a way that is as memory-efficient as possible. For various reasons, the file will already be loaded completely into memory, and is accessed using an InputStream
by the application. I would like to verify the signature using the stream interface, but the JCA Signature
class' update
method only accepts byte[]
and related classes.
How can I do this efficiently? I don't want to load the beast into a second byte array, otherwise we'll be seeing some seriously high memory use, but the interface doesn't seem to support it otherwise.
Update
If it matters, the signing algorithm is SHA-1
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
为什么不一次读取输入流一个块(4096 字节或任何方便的大小),为每个块调用 update() 。
Why not just read the input stream a block (4096bytes or whatever convenient size) at a time, call update() for each block.
创建一个字节数组作为缓冲区,并从 InputStream 中一次读取缓冲区,每次对 Signature 调用 update() 。 如果缓冲区大小合理,则将数据从一个进程传输到另一个进程所消耗的 CPU 时间(我猜这就是您正在做的事情?)与计算时间相比可能可以忽略不计。 在从磁盘读取的情况下,CPU 使用率回报可忽略不计的分界点似乎是 8K 左右的缓冲区大小,我怀疑这或多或少也适用于您的情况。 (如果感兴趣,请参阅我在 InputStream 缓冲区大小 上整理的页面.)
Create a byte array to act as a buffer and read buffer at a time from the InputStream, calling update() on the Signature each time. Provided the buffer is of a reasonable size, the CPU time consumed transferring the data from one process to another (I'm guessing that's what you're doing?) is likely to be negligible compared to the calculation time. In the case of reading from disk, the cut-off point for negligible return on CPU usage appears to be a buffer size of around 8K, and I suspect that this will more or less apply in your case too. (In case it's interesting, see the page I put together on InputStream buffer sizes.)