简体   繁体   English

如何在C ++中使用gcrypt计算大文件的哈希

[英]How do I calculate the Hash of a large file with gcrypt, in c++

I have file that I need to hash, this file can be too large to read into memory at one time, so I need a way using gcrypt to hash the file in more manageable sized chunks how can I do this? 我有需要散列的文件,该文件可能太大而无法一次读入内存,因此我需要一种使用gcrypt将文件散列为更可管理的大小块的方法,我该怎么做?

Thank you 谢谢

I found how to do this: 我发现了如何做到这一点:

The gcrypt handle is valid and builds upon its input so that whether you write five 1k chunks to the handle or one 5k chunk of data the hash is the same either way. gcrypt句柄是有效的,并建立在其输入的基础上,因此无论您将5个1k块写入该句柄还是一个5k数据块,散列都是相同的。 The hash itself is not finalized until you attempt to read it, so you can sit in a loop reading chunks of a large file at a time out and passing them to gcrypt and thus prevent yourself from ever having too much in memory at one time. 直到您尝试读取哈希之后,哈希本身才会最终确定,因此您可以坐在循环中一次读取一个大文件的块,然后将它们传递给gcrypt ,从而防止自己一次拥有过多的内存。

I have not solved this problem before, but I can think of 2 possible solutions. 我以前没有解决过这个问题,但是我可以想到2种可能的解决方案。

  1. Store a Hash Set. 存储哈希集。 Lets say that your memory size is 1k and your file is 5k. 假设您的内存大小为1k,文件为5k。 You can have 5 pieces of this file. 您可以拥有5个此文件。 Take the hash of each piece and store the set of answers. 取每个片段的哈希值并存储答案集。 In this example you would store 5 hash values to represent this file. 在此示例中,您将存储5个散列值来表示该文件。

  2. Use option 1, then string the hash values together and take the hash of this. 使用选项1,然后将哈希值串在一起,并对其进行哈希处理。

Mostly it depends on your limits on how the hash values are stored. 通常,这取决于您对散列值的存储方式的限制。

Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM