HashCompare: Script Command to compare file hashes

Note: This script now requires a minimum Opus version 11.16...

This script was written in response to a request from TheSeeker in the following discussion:

SHA-256 checksums

What does it do?:
This script adds a 'HashCompare' Script Command to Opus. Such commands can be used like any other Opus internal commands directly in toolbar buttons, hotkeys, etc. The 'HashCompare' command is intended by default to help you easily validate a hash / checksum provided by the website (or other trusted reference) you've downloaded a file from against the actual checksum of the file as calculated by Opus. The command expects you to have already copied the trusted checksum to your clipboard (for example, copied to the clipboard from your web browser) before then selecting your downloaded copy of the file in an Opus lister and then running the 'HashCompare' command. The script provides various popup dialogs to let you know what's happening...

In response to a user request (and my own needs :slight_smile:), the script was updated to allow a FILE option which compares the hashes of two selected files, instead of just one against the clipboard.

The command currently supports only two options that control which type of hash value the script will generate for comparison. Those options are MD5 or SHA... and will default to MD5 if no option is specified. If GPSoft ever add support for additional hash types to Opus (say SHA-512), updating the script to support them will be trivial.

How to install and use it?:
The script is provided here as an Opus Script Package... which in turn is actually a zip file with a VBScript packaged inside. It can be installed by either manually copying the OSP file to the /dopusdata\Script AddIns folder, or via drag-and-drop into the Preferences / Toolbars / Scripts page.

Download: HashCompare.osp (3.92 KB)

Notes:
2014-09-11: Initial release...
2014-09-12: v1.1.0 - This update moves the point at which the clipboard content gets reset back to it's original state due to some misbehavior in the original version. Some of the popup messages were also tweaked

  • Added an 'info' icon to successful comparison popup to provide a more obvious visual indicator of success
  • A progress dialog is now provided while Opus is calculating the hash value
  • The 'Ok' button has been turned into a drop-down menu button with an option to copy the newly calculated hash value to the clipboard (otherwise, the clipboard is returned to it's original state)

2015-12-31: v1.2.1 - Renamed script, and added a FILE option to compare two selected file checksums instead of 1 selected file against the clipboard, added a DEBUG_CLEAR config option, added config option descriptions, made several messaging changes, set minimum Opus version to v11.16
2016-01-28: v1.2.5 - Added support for comparing two files from different tabs (in the same or different lister), since I very often have files who's hashes I want to compare in different folders and even different drives; and moving them to the same folder the way the first use of the FILE option worked isn't always practical. Also added a TOFILE option to write the calculated hash value out to a file named <filename>.<hashType>. So the MD5 hash for foo.bin will be written to foo.bin.MD5. If multiple files are selected, then each file gets it's own hash file.
2016-11-22: v1.2.7 - A minor cleanliness update with fixes for the problems DesertDwarf noticed re: DOpus.SetClip.

After installing the script package, users can configure the scripts options via the scripts listing in Prefs (Configure button):



There are only debug logging options in the current version of this script:



Once installed, you can then use the HashCompare command just like any other Opus command:




How does it work?:

The HashCompare script command operates against selected items and the clipboard. It does not take explicit file name/path arguments, but otherwise supports several other command line options:

  • CLIP Compares the hash of a single selected file (only first if multiple are selected) against the clipboard contents. Intended for comparing the actual hash of a file from against the hash posted for it on the website from which you downloaded it... This is the default behavior if not otherwise specified on the command line.
  • FILE Compares the hash values of the 2 files (only the first two if multiple are selected) against each other. Currently, the two files need to be in the same folder/file display.
  • MD5 Performs an MD5 based hash comparison. This is the default behavior if not otherwise specified on the command line.
  • SHA Performs an SHA based hash comparison.

For comparisons of a SINGLE files hash against the Clipboard:

The script shows a progress dialog while the hash is being calculated for the file:


For a successful comparison:


For a failed comparison:


In the event that things required for the script to do it's job are missing, the script will throw various obvious errors:






For comparisons of TWO files hashes against each other:

The script shows a separate progress dialog for each file while the hash is being calculated for the files:


Note that due to both my poor understanding of how to use the Progress dialog object that Opus provides, as well as perhaps some limitations in using it the way I'd like to - when using the FILE option to compare two files as is shown here, the percentage indicator in the progress dialogs title will NOT show numeric progress percentage as each files hash is being calculated like you get with the dialog for a single files comparison against the clipboard (above). It will only show 0% as the first file begins, 50% as the second file begins, and then briefly flash 100% upon the second files completion. HOWEVER, the top progress meter (BAR) does in fact at least accurately show relative progress of each individual file.
Note: As of v1.2.5 I've made changes to my original use of the progress dialog. I found that Opus would not run a single 'Clipboard COPYNAMES' command with two files from different folders added to the scripting object used to run the command. This meant I had to run two separate commands, and that caused me to lose the 'All files' overall progress of the full progress dialog all-together. So it's no longer a full progress dialog anymore, but you'll still see the percentage progress for each file separately.

For a successful comparison:


For a failed comparison:


Again, in the event that things required for the script to do it's job are missing, the script will throw various obvious errors:




Feedback appreciated... I may make additional changes to allow the new FILE option to compare files from different folders... as having them in the same folder is sometimes a PITA.

5 Likes

ToDo: I haven't played very much with the Progress scripting object... but in the case where you are letting the 'HashCompare' script calculate the checksum for the first time for a large file... it would probably be good if some indicator was available to let you know the calculation was still working. Otherwise, you run the command - and then there's no visible sign of activity until the dialog pops up to inform you of the results...

Script Writer notes: I wound up using the ScriptCommandData.command.RunCommand method in order to run the necessary built-in 'Clipboard COPYNAMES=hash*' command to calculate the hash value. I could have used the DOpus.FSUtil.Hash method instead... which would have eliminated the need for multiple other bits of code in the script... but I found that Opus does not use any of the cached hash values that may have been generated by a previous use of the 'Clipboard COPYNAMES=hash*' commands when re-calculating them via the FSUtil object. Not a big surprise, but just wanted to acknowledge that here... But as a result, I am also resetting the clipboard contents to whatever it was prior to running the script command, since calling the 'Clipboard' command overwrites whatever was in the clipboard with the newly calculated hash value...

This is great. Thanks for sharing.
1 less program I have to install now :slight_smile:

Frankly I didn't expect there would be a 'Opus-solution'... so I went on I checking out various hash tools and found this one quite nice and has an option for context menu.
There is a portable version and a PRO (setup).
Pro also adds the tools to the context menu.

=

I've updated the post with a new version of the script as well as various informational updates to the post about the changes. Again here:

I added the progress dialog I wanted, which works well to let you know that Opus is still calculating the hash value before it pops up the result dialog...

I modified the result dialog so the 'Ok' button is now a drop-down menu button with an option to copy the hash value that was just calculated for the file to the clipboard. This actually happens when the hash is calculated by the 'Clipboard' command the script uses... but I set it back to it's original state as soon as possible because I wanted the manipulation of the clipboard due to the way the script works to be transparent to the user. But I still thought there could be use in getting the newly calculated hash value to the clipboard, and this seemed the most convenient way. I'd have loved to be able to write a bit more of an elaborate dialog - similar to the third-party app above, but as cool as the dialog controls in Opus' scripting interface are - there's a limit to what we can do.

The successful result dialog now also includes an 'info' icon to give a little more obvious visual distinction between success and failed result dialogs...

Getting an error for matching MD5s, no doubt because the clipboard entry contains the filename:

Yes, clearly :slight_smile:.

About this... it's worth noting that because I've seen some websites that post the checksum with all capitals, I am doing a lower case conversion of the clipboard contents for the purpose of the comparison. I'm also trimming any leading or trailing spaces from the string as well, since - sometimes, my browsers insist on catching a space on either end of a string when I dbl-click the text in the browser window to copy it to the clipboard. Just know that when I set the clipboard back to it's original state, its the actual unmodified original content.

In your example above - I would say to just copy the actual hash without the other filename info from the website. Particularly if you're like me and you save the file with a different name than its original name from the website, which would make the check performed by this script fail every time. If you REALLY want to use it with the name though, I can provide script configuration options to modify the hash code used in the Opus command to use the sort of output you show above (Opus has 3 different format codes for each supported hash type for things like this).

One other thing I could do is perform a string 'search' rather than a 'strict' comparison. The reason I didn't do this to begin with is because I didn't want the potential for a false positive match if the hash calculated by Opus is in fact just a portion of the real hash you're comparing to. This is HIGHLY unlikely to have happen... but just the same, since we're talking about verifying the integrity of a file - using a method that is not a strict comparison just seems conceptually wrong to me. If you think this would be a useful change, then I can instead add a script configuration flag to allow for a RELAXED comparison (string 'search') instead of the current and safer STRICT comparison (literal string 'compare').

Any chance I could convince you to create script like this but which works with 2 selected files instead of needing the checksum for one of the to be put in the clipboard?

For me the confirmation window would need to include the full paths to the 2 files in some form.

Thanks if you even consider this.

Jason

@jproos Not exactly what you're looking for but I wonder if you're aware of the Duplicate Files utility panel, which you can run in MD5 mode. Opens with Ctrl + U on the standard menu toolbar (Tools / Find Duplicate Files).

It sounds like it might be what I'm looking for. I'll check it out. Thanks for the tip.

Jason

It looks like the Duplicate File utility might be sufficient for my needs.

To be clear I've been doing what I need with Opus already using the MD5 column and some fussing around with the layout whenever I need this. I was simply looking for a simpler way to accomplish the confirmation that 2 files which should be identical have identical checksums.

Thanks,

Jason

@Jason glad that helps. I do see the use of your idea of a quick command to compare two files directly. steje's script is probably not too hard to tweak for someone who knows VB and the scripting interface — not me, but thought I'd peek inside. Strangely when I unzipped the osp file, my text editor showed the script with Chinese characters—it assumed a UTF-16 encoding. Converting it to UTF-8 it displayed it properly.

@jproos, playful and TheSeeker...

I've updated the root post with a fair overhaul to the script... the ONLY change to the old behavior is some adjusted messaging. The update otherwise adds a FILE option that will compare the hash values of two selected files like you had mentioned, which I find very useful for quick comparisons myself - so THANKS for suggesting it!

Read the notes on the FILE comparisons progress dialog snafu... If there is interest, I will also post another update to allows comparison of files in different folders (file from source file display against file from destination file display). The command currently works against only two files in the same folder - which I admit isn't ideal.

Cheers - I'll take a look.

Update to v1.2.5.

2016-01-28: v1.2.5 - Added support for comparing two files from different tabs (in the same or different lister), since I very often have files who's hashes I want to compare in different folders and even different drives; and moving them to the same folder the way the first use of the FILE option worked isn't always practical. Also added a TOFILE option to write the calculated hash value out to a file named .. So the MD5 hash for foo.bin will be written to foo.bin.MD5. If multiple files are selected, then each file gets it's own hash file.

I also added some notes to the root post about having to fall back to a simple rather than full progress dialog when comparing two files hashes against each other.

I now use PowerShell to compare files:

Get-FileHash *.iso | Format-List

The above code will produce the following for the two ISO files I have in this particular directory (hashes are the same as it's the same file):

Algorithm : SHA256
Hash      : 9D0CD2C98C5912463E7A64986EF24DC421D973376E8A35E392038CB5F4693AAB
Path      : C:\Users\Seeker\Downloads\F23-x86_64-WORK-20160303.iso

Algorithm : SHA256
Hash      : 9D0CD2C98C5912463E7A64986EF24DC421D973376E8A35E392038CB5F4693AAB
Path      : C:\Users\Seeker\Downloads\test.iso

SHA256 is used by default. If wanting SHA1, SHA512 or MD5, simply use the -Algorithm parameter:

Get-FileHash -Algorithm SHA1 *.iso | Format-List

Produces the following:

Algorithm : SHA1
Hash      : 1E71B6FBB10BFAEE6258FD93A3CD60B2DA4146F4
Path      : C:\Users\Seeker\Downloads\F23-x86_64-WORK-20160303.iso

Algorithm : SHA1
Hash      : 1E71B6FBB10BFAEE6258FD93A3CD60B2DA4146F4
Path      : C:\Users\Seeker\Downloads\test.iso

More here.

Edit: Added some more examples. If too off-topic, please let me know.

This script broke for me. It complained about:

HashCompare.vbs: Initializing...
HashCompare: Error at line 221, position 7
HashCompare: Error 0x80004005

...which was a DOpus.SetClip without any arguments. I changed it to DOpus.SetClip() and it worked. I actually changed two of the three instances where there was no (), which were lines 221 and 436. I somehow missed fixing the one on line 130 and it still ran fine. I'm not much of a VBSer, so I don't know the rules about missing parentheses.

@DesertDwarf... weird. Thanks, I'll get around to posting an update to the root thread soon.

@TheSeeker... yeah, value there in hash types not supported by Opus. I don't think it's too off topic at all. People who are interested in this script might very well be also wanting support for other hash types. I wonder if I can capture that cmdlets output in a script such that I could incorporate the 'other' hash types not supported by Opus into the script. Hmmm... I'd keep the native Opus stuff thats in here already since I can take advantage of some caching on repeat comparisons though.

Posted a minor update to fix the DOpus.SetClip error..

Add a feature to ingnore ":" and the text before it in clipboard is needed, think about it.