Column: SHA-256 and SHA-512

Overview

This simple script adds columns to display SHA-256 and SHA-512 hashes, similar to the built-in columns for MD5 and SHA-1.

Unlike the built-in columns, there is no maximum size limit, but you could add one if you wanted to.

Installing

  • Requires Opus 12.23 or above.
  • Download SHA-256.js.txt (1.3 KB)
  • Drag it to the list under Preferences / Toolbars / Scripts.
  • You can then find the column under the Scripts category.

History

  • v2.1 (05/Jan/2021):
    • Turned "multicol" mode off again, as it meant time was wasted calculating both hashes in the usual situation where only one was wanted. The "multicol" version is still available below, but only recommended if you're showing both hashes at once.
  • v2.0 (05/Jan/2021):
    • Added SHA512 column.
    • Refactored into "multicol" script, so it can calculate both hashes at the same time if both columns are enabled at once.
    • Set "autorefresh" on the columns, so if the files change their hashes are recalculated.
    • Wrapped exception handler around the hash call, so if it fails it doesn't spam the script error log.
  • v1.1 (01/Oct/2019):
    • Fix for Opus giving up waiting on calculation of large/slow files.
    • Requires Opus 12.17.1 beta or later, for the new timeout property.
  • v1.0 (15/Sep/2019):
    • Initial release.

Script code

The contents of the .js.txt file above are reproduced here so you can see how it works without downloading it:

function OnInit(initData)
{
	initData.name = "SHA-256 and SHA-512";
	initData.version = "2.1";
	initData.copyright = "(c) 2019-2021 Leo Davidson";
	initData.url = "https://resource.dopus.com/t/column-sha-256/33525/1";
	initData.desc = "SHA-256 and SHA-512 hash columns";
	initData.default_enable = true;
	initData.min_version = "12.23";
}

function OnAddColumns(addColData)
{
	AddColumn(addColData, "SHA256", "SHA256", "sha256");
	AddColumn(addColData, "SHA512", "SHA512", "sha512");
}

function AddColumn(addColData, colName, colLabel, hashName)
{
	var col = addColData.AddColumn();

	col.name = colName;
	col.label = colLabel;

	col.method = "OnColumns";
	col.multicol = false;

	col.justify = "left";
	col.autogroup = true;
	col.autorefresh = true;

	// Setting the timeout to zero tells Opus to Wait for us forever without giving up.
	// Hashing large files can take a long time.
	col.timeout = 0;
	
	col.userdata = hashName;
}

// Implement the columns
function OnColumns(scriptColData)
{
	try
	{
		var item = scriptColData.item;
		if (item.is_dir) return;

		var algorithm = scriptColData.userdata;
		var hash = DOpus.FSUtil().Hash(scriptColData.item.realpath, algorithm);

		scriptColData.value = hash;
	}
	catch(e)
	{
		scriptColData.value = "";
	}
}

Multi-Column version

The main version of the script, above, calculates each hash value independently. This is better in the usual case where you're only displaying one hash and not several at once.

It's also possible to calculate multiple hashes at once. This uses more CPU time, but means the file data only has to be read once instead of multiple times. This is faster in the less usual case where you're displaying multiple hashes at once.

You should only install one version or the other. The script version above is best for most people.

The alternative version is more likely to be of interest to people writing their own scripts, as is a good example of how "multicol" scripts work, and you can compare the two versions to see how they differ.

Click here to see the alternative multi-column version.

Multi-column version download:

Multi-column version code:

function OnInit(initData)
{
	initData.name = "SHA-256 and SHA-512";
	initData.version = "2.0";
	initData.copyright = "(c) 2019-2021 Leo Davidson";
	initData.url = "https://resource.dopus.com/t/column-sha-256/33525/1";
	initData.desc = "SHA-256 and SHA-512 hash columns";
	initData.default_enable = true;
	initData.min_version = "12.23";
}

function OnAddColumns(addColData)
{
	AddColumn(addColData, "SHA256", "SHA256", "sha256");
	AddColumn(addColData, "SHA512", "SHA512", "sha512");
}

function AddColumn(addColData, colName, colLabel, hashName)
{
	var col = addColData.AddColumn();

	col.name = colName;
	col.label = colLabel;

	col.method = "OnColumns";
	col.multicol = true;

	col.justify = "left";
	col.autogroup = true;
	col.autorefresh = true;

	// Setting the timeout to zero tells Opus to Wait for us forever without giving up.
	// Hashing large files can take a long time.
	col.timeout = 0;
	
	col.userdata = hashName;
}

// Implement the columns
function OnColumns(scriptColData)
{
	try
	{
		var item = scriptColData.item;
		if (item.is_dir) return;

		// Work out which hashes to calculate.
		// Opus can calculate more than one at once, to avoid reading the file multiple times if multiple hash columns are displayed.

		var algorithms = "";
		var multiple = false;

		for (var e = new Enumerator(scriptColData.columns); !e.atEnd(); e.moveNext())
		{
			var colName = e.item();
			var colData = scriptColData.columns(colName);
			if (algorithms != "")
			{
				algorithms += ",";
				multiple = true;
			}
			algorithms += colData.userdata;
		}

		if (algorithms == "") return;

		// If we request one hash, hashes will be set to the string result.
		// If we request more than one, hashes will be set to a vector of strings.
		
		var hashes = DOpus.FSUtil().Hash(scriptColData.item.realpath, algorithms);

		var idx = 0;		

		for (var e = new Enumerator(scriptColData.columns); !e.atEnd(); e.moveNext())
		{
			var colName = e.item();
			var colData = scriptColData.columns(colName);
			if (multiple)
			{
				colData.value = hashes(idx++);
			}
			else
			{
				colData.value = hashes;
			}
		}
	}
	catch(e)
	{
		for (var e = new Enumerator(scriptColData.columns); !e.atEnd(); e.moveNext())
		{
			var colName = e.item();
			var colData = scriptColData.columns(colName);
			colData.value = "";
		}
	}
}
3 Likes

Root post updated with v1.1:

  • Fix for Opus giving up waiting on calculation of large/slow files.
  • Requires Opus 12.17.1 beta or later, for the new timeout property.

SHA-256 in Directory Opus is calculated incorrectly. Why?
I gave a drawing.
The red arrows indicate the DO and script for the column from the current topic.
Green arrows indicate HashTab and Online Tools.

It seems to match the value given in the HASHs.txt file?

Red arrows - SHA256 calculated in DO (txt HASHs) and column script (SHA-256.js)
and
green arrows - SHA-256 calculated in HashTab and Online Tools
do not match
Why?

We've tracked this down and have a fix coming.

The SHA256 algorithm (only exposed to scripts) produced incorrect results for files >= 512MB. (The same bug is in lots of implementations of SHA256, it turns out. It's almost like a separate hash function due to how common it is, but we'll change ours to match the correct version.)

2 Likes

Root post updated with new version:

  • v2.0 (05/Jan/2021):
    • Added SHA512 column.
    • Refactored into "multicol" script, so it can calculate both hashes at the same time if both columns are enabled at once.
    • Set "autorefresh" on the columns, so if the files change their hashes are recalculated.
    • Wrapped exception handler around the hash call, so if it fails it doesn't spam the script error log.

Note that this does not fix the issue with files larger than 512 MB. That will be in the next Opus update.

1 Like

Root post updated again:

  • v2.1 (05/Jan/2021):
    • Turned "multicol" mode off again, as it meant time was wasted calculating both hashes in the usual situation where only one was wanted. The "multicol" version is still available, but only recommended if you're showing both hashes at once.

The "multicol" version is still available, if you expand the section at the bottom of the root post. I mainly kept it so you can compare the two versions to see the how the same script would be written as "multicol" and not.

1 Like