Confoederatio Helvetica Confoederatio Helvetica - 1 year ago 89
C Question

C++ Tweetnacl hash a file without read whole file to memory

I'm using tweetnacl to generate sha512 hashes of strings and file. For strings it works quite well but i have no idea how to do it with files.

The signature of the function ist

extern "C" int crypto_hash(u8 *out, const u8 *m, u64 n);

where u8 is of type unsigned char and u64 is of unsigend long long.
For string a can use it like that

string s("Hello");
unsigned char h[64];

crypto_hash(h, (unsigned char *)s.c_str(), s.size());

This works great for a string and small files but if i want to create a hash for a big file, it is not viable and uses to much memory. I searching for a solution to read the file byte by byte and pass it as unsigend char pointer to that function. Has anyone a idea how to achieve that?

P.S Sorry for the poor English.
p.s.s I use tweetnacl because of the small size and i need only the hashing function.

Answer Source

I'd suggest you to use a different implementation, one which you can incrementally feed in chunks.

This one for example. As the licence is bsd and the code is C with no dependencies, you can copy/paste only the 3 functions that you need without bringing a whole lib (no matter how small) into your project.

The life-cycle goes like:

  • sha256_init(&ctx)

  • repeatedly read blocks from file and feed them into sha256_update(&ctx, buff, buffLen)

  • when EOF, get your digest using sha256_final(&ctx, digestHere)

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download