tags:

views:

38

answers:

3

Hi, i need to create hash for folder, that contains some files. I already done this task for each of files, but i searching the way to create one hash for all files in folder. Any ideas how to do that?

(of course i can create hash for each file and concatenate it to some big hash but it's not a way i like)

Thanks in advance.

+1  A: 

Concatenate filenames and files content in one big string and hash that, or do the hashing in chunks for performance.

Sure you need to take few things into account:

  • You need to sort files by name, so you don't get two different hashes in case files order changes.
  • Using this method you only take the filenames and content into account. if the filename doesn't count you may sort by content first then hash, if more attributes (ctime/mtime/hidden/archived..) matters, include them in the to-be-hashed string.
aularon
Thanks for your response. The string can be really big, so i will need to divide it on chunks, just thinking how to do it correctly.
Igor Pistolyaka
I remember C# hashers had a function to feed them with chunks, and finally you can ask to get the final hash, not sure what these functions/classes were. With them you can sort your input the way you like in memory, then loop files and load chunks into few hundred KBs and feed it to the hasher, this way you don't need much memory but still, will take some time for hashing, which is something you can't get rid of.
aularon
A: 

If you already have hashes for all the files, just sort the hashes alphabetically, concatenate them and hash them again to create an uber hash.

Sam Saffron
A: 

Create tarball of files, hash the tarball.

> tar cf hashes *.abc
> md5sum hashes

Or hash the individual files and pipe output into hash command.

> md5sum *.abc | md5sum

Edit: both approaches above do not sort the files so may return different hash for each invocation, depending upon how the shell expands asterisks.

Paul Ruane