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

Z. S. O. tiredashell at gmail.com
Sat Mar 7 02:46:53 EST 2009


I was reading section 4 of the tutorial, but I just realized you were
referring to a program that encrypted a file, not hashing. Sorry for the
mistake.
Anyway, the code you suggest works great. The only change I made was
returning it in base64:

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

On Sat, Mar 7, 2009 at 12:14 AM, Jack Lloyd <lloyd at randombit.net> wrote:

> 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
> _______________________________________________
> botan-devel mailing list
> botan-devel at randombit.net
> http://lists.randombit.net/mailman/listinfo/botan-devel
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.randombit.net/pipermail/botan-devel/attachments/20090307/8474580a/attachment.html>


More information about the botan-devel mailing list