/* HashTools 1.0 Beta3 By Enternal - December 26, 2014 Script Home Thread: http://xyplorer.com/xyfc/viewtopic.php?f=7&t=13052 http://www.xyplorer.com/ Thanks: 40k for "Hash" http://xyplorer.com/xyfc/viewtopic.php?f=7&t=9220 Links: http://microformats.org/wiki/hash-examples http://www.regexr.com/ */ "_HTMLCode" Global $HTMLCode = << Hash Checker HTMLCode; "Hash" // ALL 40k's Code // http://xyplorer.com/xyfc/viewtopic.php?f=7&t=9220 End (""==""),"Nothing Is Selected!"; $Files = Get('SelectedItemsPathNames', '|', 'a'); ForEach($Item, $Files) { If Exists($Item) == 2 {Msg "Only Files Can Be Hashed!"; End(2);} } $Report = ''; Global $g_Md5; Global $g_Sha1; Global $g_Sha256; Global $g_Sha512; Global $g_Crc; ForEach($File, $Files) { //$Name = GetPathComponent($File , 'file', ); $Name = $File; $Size = Report('{Size B}', $File, , ); $g_Crc = Hash('crc32', $File, '3'); $g_Md5 = Hash('md5', $File, '3'); $g_Sha1 = Hash('sha1', $File, '3'); $g_Sha256 = Hash('sha256', $File, '3'); $g_Sha512 = Hash('sha512', $File, '3'); $Report = <<<# $Name ---------------------------------------- Size : $Size Bytes CRC32 : $g_Crc MD5 : $g_Md5 SHA1 : $g_Sha1 SHA256: $g_Sha256 SHA512: $g_Sha512 $Report #; } $Report = RegexReplace($Report, "^([ \t]*)\r?\n\s{2}$", ""); // http://stackoverflow.com/questions/12955690/regex-in-notepad-to-remove-blank-lines Text $Report, 700, 800, "", "w"; "" "Hash Checker (Fast) - Specific File and Hash" End (""==""),"Nothing Is Selected!"; Global $HTMLCode; Sub "_HTMLCode"; $Count = Get("CountSelected"); If $Count == 0 {Msg "Nothing Selected!"; End(1);} Elseif $Count > 1 {Msg "Only 1 File Can Be Selected!"; End(1);} $File = Get("curitem"); If Exists($File) == 2 {Msg "Only A File Can Be Hashed!"; End(1);} $HashType = InputSelect("Hash Algorithm", "MD5|SHA1|SHA256|SHA512|CRC32", "", "40"); $ExpectedHash = Trim(Input("Enter Expected ".$HashType." Hash", $File, )); If $HashType == "MD5" {$AlgLen = "32";} ElseIf $HashType == "SHA1" {$AlgLen = "40";} ElseIf $HashType == "SHA256" {$AlgLen = "64";} ElseIf $HashType == "SHA512" {$AlgLen = "128";} ElseIf $HashType == "CRC32" {$AlgLen = "8";} $HashCheck = RegexMatches($ExpectedHash,"[a-fA-F0-9]{$AlgLen}"); //RegexMatches("7a869af318d6d34f4ab5834cb25fa634","[^a-fA-F0-9]+"); // Find anything not related to typical hash $HashOrNot = Compare($ExpectedHash, $HashCheck); If $HashOrNot != 0 {Echo "Not A ".$HashType." Hash!"; End(1);} $Hash = Hash($HashType, , "3"); $Compare = Compare($ExpectedHash, $Hash, "i"); If $Compare == 0 {$Result = "Hashes Match!"; $CSSID = "resultmsgmatch"; $ItemMatch = "itemmatch";} Else {$Result = "Hashes Are Different!"; $CSSID = "resultmsgnomatch"; $ItemMatch = "itemnomatch";} $Content = <<
$File
Expected Hash:
$ExpectedHash
Resultant Hash:
$Hash

$Result
HTML; Html($HTMLCode.$Content,700,800); "Hash Checker (Slow) - Multiple Files and Hashes" // ALL 40k's Code // http://xyplorer.com/xyfc/viewtopic.php?f=7&t=9220 // Did not include SHA512 due to speed and overkill. // If you want SHA512, simply uncomment and add the // respective code for the report. End (""==""),"Nothing Is Selected!"; Global $HTMLCode; Sub "_HTMLCode"; $Content = ""; $Files = ""; $SelectedItems = Get('SelectedItemsPathNames', '|', 'a'); ForEach($Item, $SelectedItems, "|") { If Exists($Item) == 2 { $Files = $Files."|".FolderReport("files", "r", $Item, "r", , "|")."|";} ElseIf Exists($Item) == 1 { $Files = "$Files|$Item|"; } } $Files = FormatList($Files, "tsed", "|"); $Checksums = Input('Hash', 'Paste Text Containing Known Hashes Below:', , 'm', , , ); $Report = ''; Global $g_Crc; Global $g_Md5; Global $g_Sha1; Global $g_Sha256; Global $g_Sha512; ForEach($File, $Files) { $Name = GetPathComponent($File , 'file', ); $Size = Report('{Size B}', $File, , ); $g_Crc = Hash('crc32', $File, '1'); $g_Md5 = Hash('md5', $File, '1'); $g_Sha1 = Hash('sha1', $File, '1'); $g_Sha256 = Hash('sha256', $File, '1'); $g_Sha512 = Hash('sha512', $File, '1'); $matchCRC = RegexMatches($Checksums, $g_Crc, '|', '0') ? "hashmatch" : "hash"; $matchMD5 = RegexMatches($Checksums, $g_Md5, '|', '0') ? "hashmatch" : "hash"; $matchSHA1 = RegexMatches($Checksums, $g_Sha1, '|', '0') ? "hashmatch" : "hash"; $matchSHA256 = RegexMatches($Checksums, $g_Sha256, '|', '0') ? "hashmatch" : "hash"; $matchSHA512 = RegexMatches($Checksums, $g_Sha512, '|', '0') ? "hashmatch" : "hash"; If $matchCRC == "hashmatch" || $matchMD5 == "hashmatch" || $matchSHA1 == "hashmatch" || $matchSHA256 == "hashmatch" || $matchSHA512 == "hashmatch" { $CSSID = "resultmsgmatch"; $ItemMatch = "itemmatch"; $Result = "Hashes Match!"; } Else {$CSSID = "resultmsgnomatch"; $ItemMatch = "itemnomatch"; $Result = "No Hashes Match Up."; } $Content = <<
$File
CRC32   : $g_Crc
MD5     : $g_Md5
SHA-1   : $g_Sha1
SHA-256 : $g_Sha256
SHA-512 : $g_Sha512
$Result

HTML; } $CodeBottom = << HTMLBOTTOM; Html($HTMLCode.$Content.$CodeBottom,700,800); "" "_HashFileCreate" // PFA > +"Create Hashfile With HashTools" *>::Load HashTools, "_HashFileCreate"; Global $Load = 1; Global $SelectedItems = Get('SelectedItemsPathNames', '|', 'a'); Sub "Create Hash File"; "Create Hash File" End (""==""),"Nothing Is Selected!"; If $Load != 1 {$SelectedItems = Get('SelectedItemsPathNames', '|', 'a');} Global $Files; ForEach($Item, $SelectedItems, "|") { If Exists($Item) == 2 { $Files = $Files."|".FolderReport("files", "r", $Item, "r", , "|")."|";} ElseIf Exists($Item) == 1 { $Files = "$Files|$Item|"; } } $Files = FormatList($Files, "tsed", "|"); $HashType = InputSelect("Hash Algorithm", "CRC32|MD5|SHA1|SHA256|SHA512", "", "40"); $Filename = Input("Enter Desired Filename", , ""); If $HashType == "CRC32" {$HashType = "SFV";} $Exist = WriteFile("$Filename.$HashType", "; Created by XYplorer's HashTools Script", n, tu); If $Exist == 0 {Msg "Error Writing File. Aborting."; End;} ElseIf $Exist == 2 {Msg "$Filename.$HashType Already Exist. Aborting."; End} ForEach($File, $Files) { $Name = Replace("$File", "\", "", 1); If $HashType == "SFV" { $Hash = Hash("crc32", $File, '1'); WriteFile("$Filename.$HashType", "$Name $Hash", a, tu); } Else { $Hash = Hash($HashType, $File, '1'); WriteFile("$Filename.$HashType", "$Hash *$Name", a, tu); } } "_HashFileVerify" // PFA > +"Verify With HashTools" sfv;md5;sha1;sha256;sha512>::Load HashTools, "_HashFileVerify"; $Count = Get("CountSelected"); If $Count == 0 {Msg "Nothing Selected!"; End(1);} Elseif $Count > 1 {Msg "Only 1 File Can Be Selected!"; End(1);} Global $Load = 1; Global $InputFile = ""; Sub "Verify Hash File"; "Verify Hash File" Global $HTMLCode; Sub "_HTMLCode"; Global $Load; Global $InputFile; $Content = ""; If $Load != 1 {$InputFile = InputFile("", "sfv|md5|sha1|sha256|sha512", "Select Hash File");} $InputPath = GetPathComponent("$InputFile", "path"); $HashFile = ReadFile($InputFile); $HashType = GetPathComponent("$InputFile", "ext"); If $HashType == "sha512" {$HashBits = "128";} ElseIf $HashType == "sha256" {$HashBits = "64";} ElseIf $HashType == "sha1" {$HashBits = "40";} ElseIf $HashType == "md5" {$HashBits = "32";} ElseIf $HashType == "sfv" {$HashType = "crc32"; $HashBits = "8";} // MD5, SHA1, SHA256, SHA512 - Must be two spaces or a space and an asterisk between each hash value and filename. // $HASH$ * "FILENAME" or $HASH$ "FILENAME" // SFV - Must have a space or more between filename and hash. "FILENAME" $HASH$ $HashFileItem = RegexMatches("$HashFile", "(^[a-fA-F0-9]{$HashBits}(\s{2}|\s\*).+$|^.+[a-fA-F0-9]{$HashBits}$)"); End ($HashFileItem == ""),"Hash File Not In Proper Format!"; ForEach($Item, $HashFileItem, "|") { $ExpectedHash = RegexMatches("$Item", "[a-fA-F0-9]{$HashBits}"); If $HashType == "crc32" {$File = "$InputPath\".Trim(RegexMatches("$Item", "^.+ "), " *");} Else {$File = "$InputPath\".Trim(RegexMatches("$Item", "(\s{2}|\s\*).+$"), " *");} If Exists($File) == 0 {$Result = "File Not Found!"; $CSSID = "resultmsgnomatch"; $ItemMatch = "itemnomatch";} Else { $Hash = Hash($HashType, "$File", "3"); $Compare = Compare($ExpectedHash, $Hash, "i"); If $Compare == 0 {$Result = "Hashes Match!"; $CSSID = "resultmsgmatch"; $ItemMatch = "itemmatch";} Else {$Result = "Hashes Are Different!"; $CSSID = "resultmsgnomatch"; $ItemMatch = "itemnomatch";} } $Content = <<
$File
Expected Hash:
$ExpectedHash
Resultant Hash:
$Hash
$Result

HTML; } $CodeBottom = << HTMLBOTTOM; Html($HTMLCode.$Content.$CodeBottom,700,800); "" "Append Hash To Filename" End (""==""),"Nothing Is Selected!"; $Files = ""; $SelectedItems = Get('SelectedItemsPathNames', '|', 'a'); ForEach($Item, $SelectedItems, "|") { If Exists($Item) == 2 { $Files = $Files."|".FolderReport("files", "r", $Item, "r", , "|")."|";} ElseIf Exists($Item) == 1 { $Files = "$Files|$Item|"; } } $Files = FormatList($Files, "tsed", "|"); $HashType = InputSelect("Hash Algorithm", "CRC32|MD5|SHA1|SHA256|SHA512", "", "40"); ForEach($Item, $Files, "|") { $Hash = Hash($HashType, "$Item", "3"); If Regexmatches($Item, " ") {RenameItem("* [$Hash]", "$Item", 1);} Else {RenameItem("*_[$Hash]", "$Item", 1);} Wait "100"; } "Remove Hash From Filename" End (""==""),"Nothing Is Selected!"; $Files = ""; $SelectedItems = Get('SelectedItemsPathNames', '|', 'a'); ForEach($Item, $SelectedItems, "|") { If Exists($Item) == 2 { $Files = $Files."|".FolderReport("files", "r", $Item, "r", , "|")."|";} ElseIf Exists($Item) == 1 { $Files = "$Files|$Item|"; } } $Files = FormatList($Files, "tsed", "|"); ForEach($Item, $Files, "|") { Rename r, "((\s|_|)(\[|{|\()([a-fA-F0-9]{8}|[a-fA-F0-9]{32}|[a-fA-F0-9]{40}|[a-fA-F0-9]{64}|[a-fA-F0-9]{128})(\]|}|\))) > ", , "$Item", 8); } "Verify Using Filename Inline Hash" End (""==""),"Nothing Is Selected!"; Global $HTMLCode; Sub "_HTMLCode"; $Content = ""; $Files = ""; $SelectedItems = Get('SelectedItemsPathNames', '|', 'a'); ForEach($Item, $SelectedItems, "|") { If Exists($Item) == 2 { $Files = $Files."|".FolderReport("files", "r", $Item, "r", , "|")."|";} ElseIf Exists($Item) == 1 { $Files = "$Files|$Item|"; } } $Files = FormatList($Files, "tsed", "|"); $HashBits = "128|64|40|32|8"; ForEach($Item, $Files, "|") { ForEach($HashItem, $HashBits, "|") { $ExpectedHash = RegexMatches("$Item", "(\[|{|\()[a-fA-F0-9]{$HashItem}(\]|}|\))"); If $ExpectedHash != "" {$HashType = $HashItem; Break (1);} } $ExpectedHash = Trim($ExpectedHash,"[{()}"); $ExpectedHash = Trim($ExpectedHash,"]"); // According to manual, "]" can only be trimmed as // individual character. If $HashType == "128" {$HashType = "sha512";} ElseIf $HashType == "64" {$HashType = "sha256";} ElseIf $HashType == "40" {$HashType = "sha1";} ElseIf $HashType == "32" {$HashType = "md5";} ElseIf $HashType == "8" {$HashType = "crc32";} Else {$HashType = "";} If $HashType != "" {$Hash = Hash($HashType, "$Item", "3");} Else {$Hash = "";} $Compare = Compare($ExpectedHash, $Hash, "i"); If $ExpectedHash == "" {$Result = "No Hash Found In Filename!"; $CSSID = "resultmsgnomatch"; $ItemMatch = "itemnomatch";} ElseIf $Compare == 0 {$Result = "Hashes Match!"; $CSSID = "resultmsgmatch"; $ItemMatch = "itemmatch";} Else {$Result = "Hashes Are Different!"; $CSSID = "resultmsgnomatch"; $ItemMatch = "itemnomatch";} $Content = <<
$Item
Expected Hash:
$ExpectedHash
Resultant Hash:
$Hash
$Result

HTML; } $CodeBottom = << HTMLBOTTOM; Html($HTMLCode.$Content.$CodeBottom,700,800); "" "Edit|:conf" Openwith "Notepad",,"\HashTools.xys";