Search code examples
javahashdirectorychecksumsha1

Computing SHA-1 for a directory


I'm trying to a compute a SHA-1 checksum for an entire directory structure.

What I have done so far is to recursively compute the SHA-1 for each regular file and append all the checksums together. The problem is that this makes the computing dependent on the order of traversing the files

I.e if I have a folder containing the files

file1.dat file2.dat

and the other folder containing the same files, but ordered by

file2.dat file1.dat

this will compute different checksums.

How can I make the SHA-1 computation order independent?

Thanks!


Solution

  • You have at least two options with that:

    1. Create a archive file, for directory and then create the digest for it. (zip, jar)

    2. Read all file bytes into single byte array and then create from it the digest.

    3. Pre-order data before you create the digest, using your algorithm.

    4. Create a two list of digest and compare them.

    Personally I would go with option one, is fast and easy.