16

There are several posts about generating MD5 sums for files and/or folders on various Windows platforms. However, none of these worked for me. I tried:

  • Windows CertUtil: CertUtil -hashfile myFileName MD5 returns "Access is denied" on all folders (my cmd is running with admin privileges),
  • HashTab: does not show up in the Properties dialog in Explorer as advertised,
  • Summer Properties: does not show up in the Properties dialog either,
  • HashCheck: does not allow MD5 for folders, only files,
  • md5checker: does not compute the MD5 of the entire folder (only files in it).

At this point I am starting to get a bit desperate. Please note that I am using Windows 7 x64.

For info, if possible, I am trying to find a tool that would allow something like this in Linux:

find DIR -type f -exec md5sum {} \; | sort -k 2 | md5sum
Klangen
  • 807
  • 2
  • 7
  • 10
  • Make a ZIP and hash it. – Biswapriyo Apr 19 '18 at 08:41
  • 1
    @Biswapriyo That would work, except I have very large folders with hundreds of GB in them... – Klangen Apr 19 '18 at 08:43
  • 2
    CertUtil only works on files. You would have to write a PowerShell script to loop through each file contained in a folder. If you were using Windows 10 you could use WSL calculate the hash. – Ramhound Apr 19 '18 at 09:22
  • 7
    Note that there is no standard method for hashing a folder (which isn't a single byte stream but an unordered collection), so different programs will give different results. – u1686_grawity Apr 19 '18 at 09:33
  • See also https://serverfault.com/questions/1099949/windows-equivalent-to-sha256sum-c-cryptographic-hash-digest-file-recursive – Michael Altfield May 03 '22 at 19:12

8 Answers8

8

None of these quite did what I needed so I came up with this alternative...

@echo off
for /R . %%f in (*.*) do (
    echo | set/p="%%f - "
    certutil -hashfile "%%f" MD5 | findstr /V ":"
)

Outputs in the format "<Path><Filename> - <Hash>" at one line per file.

Tirinoarim
  • 96
  • 1
  • 1
6

If you want to use a GUI, I can recommend Fsum Frontend.

Fsum Frontend is a free and easy-to-use tool that allows to compute message digests, checksums and HMACs for files and text strings. It supports drag-and-drop and you can handle multiple files at once. The checksum generated can be used to verify the integrity of the files.

It supports 96 algorithms: [...] md5 [...]

Screenshot of FsumFrontend


As the name implies, Fsum Frontend is a GUI for (among others) SlavaSoft fsum.

A fast and handy command line utility for file integrity verification. It offers a choice of 13 of the most popular hash and checksum functions for file message digest and checksum calculation.

Its features include:

  • Possibility to act recursively. FSUM can operate not only on files from a specific directory, but also on files from all subdirectories of the specified directory;
  • Work with large size files. (Tested on file sizes of up to 15 GB);
  • Full compatibility with md5sum utility

Screenshot of fsum.exe command line usage

ischeriad
  • 1,070
  • 1
  • 7
  • 11
5

You can achieve the equivalent to your Unix command (minus the sorting) with the following:

for /R . %f in (*.*) do @certutil -hashfile "%f" MD5

You can change the dot (.) for whatever folder you want to recurse from, and the *.* to whatever file mask you need in order to narrow down your file set.

  • this isn't equivalent to the OP's command which hashes the final output to get a single hash for the folder – phuclv Jul 09 '23 at 01:52
5

PowerShell provides loop statement, some people may prefer this syntax

foreach($f in dir){ certutil -hashfile "$f" md5}

Reference: https://en.wikiversity.org/wiki/PowerShell/Loops

Endle_Zhenbo
  • 169
  • 1
  • 4
  • 1
    “… easier to understand and use” than what?  I find the CMD-language `for /R . %f in (*.*) do` to be just as easy to understand and use as the PowerShell version. – Scott - Слава Україні Feb 26 '19 at 19:19
  • 1
    Personally, I prefer the syntax with {}. You're right, I should avoid the subjective words in my answer – Endle_Zhenbo Feb 26 '19 at 20:33
  • why on earth would you use `certutil` when PowerShell already has `Get-FileHash`? – phuclv Jul 09 '23 at 01:21
  • this is wrong, it also list all directories in the loop, you'll need `Get-ChildItem -File` or `dir -af` to get only files. And the OP wants a single hash of all the hashes in the folder – phuclv Jul 09 '23 at 01:42
3

HashCheck Shell Extension (archive) can be used to get a hash of a directory. This can be done by:

  1. Using HashCheck on the directory.
  2. This will generate a .md5 file which contains a listing of the hashes of each file in that directory, including all files in sub-directories.
  3. Use HashCheck again on the .md5 file it generated above.
  4. This final generated .md5 file contains a hash of the entire directory.
slartar
  • 31
  • 1
2

Late to the question but finding only unaccepted answers here is what I have found:

function Get-FolderHash ($folder) {
 dir $folder -Recurse | ?{!$_.psiscontainer} | %{[Byte[]]$contents += [System.IO.File]::ReadAllBytes($_.fullname)}
 $hasher = [System.Security.Cryptography.SHA1]::Create()
 [string]::Join("",$($hasher.ComputeHash($contents) | %{"{0:x2}" -f $_}))
}

Copy and paste this code to PowerShell console and type:

Get-FolderHash "C:\CustomFolder"

Runtime may vary depending on folder contents.

Switcher
  • 39
  • 1
  • 1
  • 3
  • Thanks for your opinion, if the script is so wrong feel free to post an corrected answer. Here on earth we use scripts written in many different ways, and sometimes people just want something that works, while also not caring about leet code performance because we are not measuring memory like it's 1990. – Switcher Jul 21 '23 at 01:18
  • a small folder might contain GBs of data, and reading the whole of it into memory is not what a sane program would do. It's especially worse when the order of the bytes aren't even fixed. The next time you run the command on the folder the hash might already be changed even if the content of the files doesn't change, for example in FAT32 after defragmentation or a FAT entry movement the order will be changed. Something that works means works the same the next time for the same set of data, not randomized results that's uncomparable – phuclv Jul 21 '23 at 02:26
  • Waiting for your answer or edit... – Switcher Jul 27 '23 at 23:28
1

If you have Python 3 installed, you could use my pyfstools package for that. Quick usage:

$ pip install git+https://github.com/sorgloomer/pyfstools.git@main
...
$ python -m pyfstools hash --algo md5 .
dir 88c17b149c1d9fef50f642b698cef9e6
0

I wrote a cross-platform CLI package for getting a hash of a file or directory.

It uses the blake3 cryptographic algorithm.

paq can hash very large directories or files.