Skip to content

Instantly share code, notes, and snippets.

@Otiel
Last active March 16, 2022 13:28
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Otiel/51c8c12d2e60120afe0d5a801d305bc9 to your computer and use it in GitHub Desktop.
Save Otiel/51c8c12d2e60120afe0d5a801d305bc9 to your computer and use it in GitHub Desktop.
XYplorer script to generate hash (CRC-32, MD5, SHA-1, SHA-256, SHA-512)
"HashFiles"
// Check if there's a file/folder selected
if (Get("CountSelected") == 0) {
Msg("Please select at least one file to calculate the hash.", 16);
End(1);
}
// Remove the folders from the list of selected paths
$filesPaths = "";
foreach ($path, Get('SelectedItemsPathNames', '|'), "|") {
if (FileType($path) != "Nofile") {
$filesPaths = $filesPaths.$path."|"
}
}
$filesPaths = SubStr($filesPaths, 0, -1);
// Check if there's a file selected
if ($filesPaths == "") {
Msg("Please select at least one file to calculate the hash.".<br>."Folders are not supported.", 16);
End(1);
}
// Ask user to select hash algorithm(s)
$hashAlgos = "";
while ($hashAlgos == "") {
$hashAlgos = InputSelect("Select the hash algorithm(s) to calculate", "CRC-32|+MD5|SHA-1|SHA-256|SHA-512", "|", 2,, 300, 250, "Hash files");
if ($hashAlgos == "") {
Msg("Please select at least one hash algorithm to calculate.", 16);
}
}
$results = "";
foreach ($filePath, $filesPaths, "|") {
// Store file path and size in results
$fileSize = FileSize($filePath);
$fileSizeTxt = FormatBytes($fileSize)." (".Format($fileSize, "0,###")." bytes)";
$results = $results."File: ".$filePath.<br>."Size: ".$fileSizeTxt.<br>;
foreach ($hashAlgo, $hashAlgos, "|") {
// Transform the hash algorithm string to the hash algorithm known by XYplorer
switch ($hashAlgo) {
case "CRC-32":
$hashAlgoXY = "crc32";
$hashAlgoText = "CRC-32: ";
break;
case "MD5":
$hashAlgoXY = "md5";
$hashAlgoText = "MD5: ";
break;
case "SHA-1":
$hashAlgoXY = "sha1";
$hashAlgoText = "SHA-1: ";
break;
case "SHA-256":
$hashAlgoXY = "sha256";
$hashAlgoText = "SHA-256: ";
break;
case "SHA-512":
$hashAlgoXY = "sha512";
$hashAlgoText = "SHA-512: ";
break;
}
// Compute hash
$startTime = <date yyyy-mm-dd hh:nn:ss.fff>;
$hashValue = Hash($hashAlgoXY, $filePath, 3);
$endTime = <date yyyy-mm-dd hh:nn:ss.fff>;
// Compute duration
$duration = DateDiff($startTime, $endTime, "ms");
$durationUnit = "ms";
if ($duration >= 1000) {
$duration = Format($duration / 1000, "0.00"); // Keep two digits behind comma
$durationUnit = "s";
}
// Store result for this hash
$results = $results.$hashAlgoText.$hashValue." (".$duration.$durationUnit.")".<br>;
}
// Store result for this file
$results = $results.<br>;
}
// Adapt window width depending on selected hash types
if (StrPos($hashAlgos, "SHA-512") == -1) {
$windowWidth = 800;
} else {
$windowWidth = 1250;
}
// Display results
Text($results, $windowWidth, 400, "Hash files");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment