[Botan-devel] Hashing a file 1KB at a time

Jack Lloyd lloyd at randombit.net
Sat Mar 7 00:14:44 EST 2009


On Fri, Mar 06, 2009 at 11:59:37PM -0500, Z. S. O. wrote:
> I'd like to make a function that hashes a file of any length and returns the
> result as a string. The tutorial recommends using DataSink_Stream, but I
> don't want to write anything to the disk.

I may have typoed something there: certainly there is no reason to write to
disk to hash data. Where did you see this? I couldn't find 

> I would just like it to hash 1
> kilobyte at a time, updating the hash each time.
> Normally, I would do something like this:
> 
> while ( (len = fread (buffer, 1, 1024, file)) ) {
> //update hash with buffer here
> }

You can hash data using exactly a loop like this. For instance

SHA_256 hash;
while ( (len = fread (buffer, 1, 1024, file)) ) {
   hash.update(buffer, len);
}

byte hash_out[32]; // 256 bits
hash.final(hash_out);

-Jack



More information about the botan-devel mailing list