Button: Show MD5/SHA

Overview:

This script-button calculates the hashes of selected files and displays them in a dialog:

Snipaste_2019-05-07_09-35-47

Download:

Alternatively, here is the same button in the form of a User Command:

  • showHASH.zip (797 Bytes)
  • Extract the zip and copy the file to /dopusdata\User Commands in Opus, and you will then find it under Settings > Customize Toolbars > Commands > User-Defined Commands where you can drag it to a toolbar.

Script Code:

The script code inside the downloads above is reproduced here for reference:

function OnClick(clickData)
{
	if (clickData.func.sourcetab.selected.count == 0)
	{
		var dlg= DOpus.Dlg;
		dlg.message="Please select a file!";
		dlg.Show;
       return;
	}
	else
	{
		for (var eSel = new Enumerator(clickData.func.sourcetab.selected); !eSel.atEnd(); eSel.moveNext())
		{
			if (eSel.item().is_dir)
			{
				var dlg= DOpus.Dlg;
			    dlg.message=eSel.item().RealPath +" is fold, not a file!";
				dlg.Show;
			}
			else
			{
			    var md5=DOpus.FSUtil.Hash(eSel.item().RealPath,"md5");
				var sha1=DOpus.FSUtil.Hash(eSel.item().RealPath,"sha1");
				var sha256=DOpus.FSUtil.Hash(eSel.item().RealPath,"sha256");
				var dlg= DOpus.Dlg;
				dlg.message=eSel.item().RealPath + "\r\n\r\n\r\n MD5:\t\t" +md5 +"\r\n\r\n SHA-1:\t" +sha1+"\r\n\r\n SHA-256:\t"+sha256;
				dlg.buttons="Copy+Copy Md5+Copy Sha1+Copy Sha256|Close"
				dlg.Show;

				switch(dlg.result)
				{
					case 1:
						DOpus.SetClip(dlg.message);
					break;
					case 2:
						DOpus.SetClip(md5);
					break;
					case 3:
						DOpus.SetClip(sha1)
					break;
					case 4:
						DOpus.SetClip(sha256)
					break;
					
				}
			}
		}
	}
}

Not something I'll use right now but looks like the dialog box should be widened when the hash is too long.

20190507%2010-34-47

I used two Tab.

\t\t

you can delete them.

ShowHash TYPE=MD5,SHA1 
1 Like

I can't align the values in the window and even the sha 256 is only partially displayed ..
I can also show the CRC32?
How do I fix it?
Thanks

Here's a version that supports CRC32 and displays SHA256 with a line break.

function OnClick(clickData) {
    var tab = clickData.func.sourcetab;
    var dlg = clickData.func.Dlg();
    var fsu = DOpus.FSUtil();

    if (tab.selected_files.count == 0) {
        dlg.Request('Please select at least one file!', 'OK');
    } else {
        for (var eSel = new Enumerator(tab.selected_files); !eSel.atEnd(); eSel.moveNext()) {
            var item = eSel.item();
            var md5 = fsu.Hash(item, 'md5');
            var sha1 = fsu.Hash(item, 'sha1');
            var sha256 = fsu.Hash(item, 'sha256');
            var crc32 = fsu.Hash(item, 'crc32');

            dlg.message = item + '';
            dlg.message += '\n\nCRC32:\n' + crc32;
            dlg.message += '\n\nMD5:\n' + md5;
            dlg.message += '\n\nSHA1:\n' + sha1;
            dlg.message += '\n\nSHA256:\n' + sha256.substr(0, 32) + '\n' + sha256.substr(32);

            dlg.buttons = 'Copy+Copy CRC32+Copy MD5+Copy SHA1+Copy SHA256|Cancel';
            dlg.Show;

            switch (dlg.result) {
                case 0:
                    exit;
                case 1:
                    DOpus.SetClip(dlg.message);
                    break;
                case 2:
                    DOpus.SetClip(crc32);
                    break;
                case 3:
                    DOpus.SetClip(md5);
                    break;
                case 4:
                    DOpus.SetClip(sha1);
                    break;
                case 5:
                    DOpus.SetClip(sha256);
                    break;
            }
        }
    }
}

Show HASH.dcf (3.2 KB)

1 Like

This version of lxp's script should be slightly quicker for large files:

function OnClick(clickData) {
    var tab = clickData.func.sourcetab;
    var dlg = clickData.func.Dlg();
    var fsu = DOpus.FSUtil();

    if (tab.selected_files.count == 0) {
        dlg.Request('Please select at least one file!', 'OK');
    } else {
        for (var eSel = new Enumerator(tab.selected_files); !eSel.atEnd(); eSel.moveNext()) {
            var item = eSel.item();
            varHashes = fsu.Hash(item, 'md5,sha1,sha256,crc32');
            var md5 = varHashes(0);
            var sha1 = varHashes(1);
            var sha256 = varHashes(2);
            var crc32 = varHashes(3);

            dlg.message = item + '';
            dlg.message += '\n\nCRC32:\n' + crc32;
            dlg.message += '\n\nMD5:\n' + md5;
            dlg.message += '\n\nSHA1:\n' + sha1;
            dlg.message += '\n\nSHA256:\n' + sha256.substr(0, 32) + '\n' + sha256.substr(32);

            dlg.buttons = 'Copy+Copy CRC32+Copy MD5+Copy SHA1+Copy SHA256|Cancel';
            dlg.Show;

            switch (dlg.result) {
                case 0:
                    return;
                case 1:
                    DOpus.SetClip(dlg.message);
                    break;
                case 2:
                    DOpus.SetClip(crc32);
                    break;
                case 3:
                    DOpus.SetClip(md5);
                    break;
                case 4:
                    DOpus.SetClip(sha1);
                    break;
                case 5:
                    DOpus.SetClip(sha256);
                    break;
            }
        }
    }
}

Show HASH tweaked2.dcf (3.2 KB)


Changed:

var md5 = fsu.Hash(item, 'md5');
var sha1 = fsu.Hash(item, 'sha1');
var sha256 = fsu.Hash(item, 'sha256');
var crc32 = fsu.Hash(item, 'crc32');

to:

varHashes = fsu.Hash(item, 'md5,sha1,sha256,crc32');
var md5 = varHashes(0);
var sha1 = varHashes(1);
var sha256 = varHashes(2);
var crc32 = varHashes(3);

So all the hashes are calculated at once.

Also changed exit to break return near the end, to fix the error when cancelling the dialog.

2 Likes

Thanks. Meant to write return to get out of the for loop.

1 Like

Many thanks !!!!!

Ah, that makes sense. I wondered why it was different but missed that.

I've edited it in my post above to do that.