SHA-1 calculation

Discuss and share scripts and script files...
Post Reply
ale
Posts: 127
Joined: 19 Feb 2009 23:23
Location: Italy

SHA-1 calculation

Post by ale »

I wanted a one click solution to find the SHA-1 hash of a file and find a way to further use this value in a script without temporary files but only using the clipboard.

My problem is that I really fight with quotes and I didn't fully grasp their use in complex situations, how and when the XY script parser eat them and so on, so I'm a bit hesitant posting... :oops:

Anyway the relevant snippet from my code with minor modifications and comments added. Tested on Windows XP. Please tell me if there are glaring mistakes!

Code: Select all

	// Hidden Start utility from ntwind.com
	$hstart = "<xydata>\Tools\hstart.exe";
	// hashutils from code.kliu.org
	$sha1sum = "<xydata>\Tools\sha1sum.exe";
	// Microsoft clip.exe utility
	$clip = "<xydata>\Tools\clip.exe";
	// Parameters for Hidden Start
	$parameters = "/WAIT /NOCONSOLE";
	// Performing the actual calculation and copying to the clipboard
	run $hstart." ".$parameters "%SystemRoot%\system32\cmd.exe /c """$sha1sum"" "<curitem>" | "$clip""", , 1;
	// Substring to extract the SHA-1 value 
	substr $sha1hash, "<clipboard>", 0, 40;
	// Optional MessageBox
	msg "$sha1hash";
	// Optional, it will copy the SHA-1 value to the clipboard
	copytext "$sha1hash";

Muroph
Posts: 561
Joined: 21 Aug 2007 16:13

Re: SHA-1 calculation

Post by Muroph »

i wasn't planning on posting this, but it's similar, so here it goes.

it's a script i've been using to calculate checksums from files.
it uses fsum.exe from this site.
fsum.exe must be in the %systemroot% folder.
if there are files selected it will calculate their checksum.
if nothing is selected it will calculate checksums for all files in current folder and, optionally, subfolders.

the last subscript is supposed to get the checksums (crc32 by default) and put it on a global variable, but i never used it before, so i'm not sure if it works.

p.s.: i'm too lazy to put comments on the script. :P

Code: Select all

"Calculate CRC32 : calculatecrc"
	if(getinfo(countselected)<1){
		$recurse=confirm("Look for files inside subdirectories?")?("-r"):("");
		run "cmd /k """"<xypath>\tools\fsum.exe"" $recurse -crc32 -jnc -jm -d""<curpath>"" *.*""";}
	else{
		$files=report("{Dir | ""{fullpath}\{name}""|}",1);
		if("$files"==""){end 1, "No files selected.";}
		strlen $len, $files;
		if($len>8000){
			$files=report("{dir | ""{name}""|}",1);
			run "cmd /k """"<xypath>\tools\fsum.exe"" -r -crc32 -jnc -jm -d""<curpath>"" $files""";
			status "Warning! Exceeded command line lenght limit!",ffff00,alert;}
		else{run "cmd /k """"<xypath>\tools\fsum.exe"" -crc32 -jnc -jm$files""";}}

"Save CRC32 to sfv : savecrc"
	$count=getinfo(countselected);
	if($count<1){
		$recurse=confirm("Look for files inside subdirectories?")?("-r"):("");
		run "cmd /c """"<xypath>\tools\fsum.exe"" $recurse -crc32 -jnc -js -d""<curpath>"" *.* >""<curfolder>_crcsums.sfv""""";}
	else{
		$sfvname=($count==1)?("<curname>"):("<curfolder>_crcsums");
		$items=report("{Dir | ""{fullpath}\{name}""|}",1);
		if("$items"==""){end 1, "No files selected.";}
		$files=$items." >""<curpath>\$sfvname.sfv""";
		strlen $len, $files;
		if($len>8000){
			$files=report("{dir |""{name}"" |}",1);
			run "cmd /c """"<xypath>\tools\fsum.exe"" -r -crc32 -jnc -js -d""<curpath>"" $files""";
			status "Warning! Exceeded command line lenght limit!",ffff00,alert;}
		else{run "cmd /c """"<xypath>\tools\fsum.exe"" -r -crc32 -jnc -js$files""";}}

"Check CRC32 from name : checkcrcfromname"
	end (getinfo(countselected)<1),"Nothing selected";
	$list=report("{dir |{fullpath}\{name}<crlf>|}",1);
	if("$list"==""){end 1, "No files selected.";}
	regexreplace $list,$list,"^(.+[\[\(_])([A-Fa-f0-9]{8})([\)\]_].+)\r\n","$1$2$3    $2<crlf>";
	writefile("%temp%\crcsumfromname.sfv",$list);
	run 'cmd /K """<xypath>\tools\fsum.exe"" -r -w -c "%temp%\crcsumfromname.sfv""';
	
-

"Calculate MD5 : calculatemd5"
	if(getinfo(countselected)<1){
		$recurse=confirm("Look for files inside subdirectories?")?("-r"):("");
		run "cmd /k """"<xypath>\tools\fsum.exe"" $recurse -md5 -jnc -jm -d""<curpath>"" *.*""";}
	else{
		$files=report("{Dir | ""{fullpath}\{name}""|}",1);
		if("$files"==""){end 1, "No files selected.";}
		strlen $len, $files;
		if($len>8000){
			$files=report("{dir | ""{name}""|}",1);
			run "cmd /k """"<xypath>\tools\fsum.exe"" -r -md5 -jnc -jm -d""<curpath>"" $files""";
			status "Warning! Exceeded command line lenght limit!",ffff00,alert;}
		else{run "cmd /k """"<xypath>\tools\fsum.exe"" -r -md5 -jnc -jm$files""";}}

"Save MD5 to md5 : savemd5"
	$count=getinfo(countselected);
	if($count<1){
		$recurse=confirm("Look for files inside subdirectories?")?("-r"):("");
		run "cmd /c """"<xypath>\tools\fsum.exe"" $recurse -md5 -jnc -jm -d""<curpath>"" *.* >""<curfolder>_md5sums.md5""""";}
	else{
		$sfvname=($count==1)?("<curname>"):("<curfolder>_md5sums");
		$items=report("{Dir | ""{fullpath}\{name}""|}",1);
		if("$items"==""){end 1, "No files selected.";}
		$files=$items." >""<curpath>\$sfvname.md5""";
		strlen $len, $files;
		if($len>8000){
			$files=report("{dir |""{name}"" |}",1);
			run "cmd /c """"<xypath>\tools\fsum.exe"" -r -md5 -jnc -jm -d""<curpath>"" $files""";
			status "Warning! Exceeded command line lenght limit!",ffff00,alert;}
		else{run "cmd /c """"<xypath>\tools\fsum.exe"" -r -md5 -jnc -jm$files""";}}

-

"Other checksums : other"
	$count=getinfo(countselected);
	if($count>0){
		$items=report("{Dir | ""{fullpath}\{name}""|}",1);
		end ("$items"==""), "No files selected.";
		$disabled=disabled;$disabled2="";}
	else{$disabled2=disabled;$disabled="";}
	$html=<<<HTML
<html>
<body>
<h4 style="text-align:center">Select checksums to calculate</h4>
<form action="xys:" method="get" style="font-size:13px">
<input type="checkbox" name="alg" value="crc32"> CRC32<br />
<input type="checkbox" name="alg" value="md2"> MD2<br />
<input type="checkbox" name="alg" value="md4"> MD4<br />
<input type="checkbox" name="alg" value="md5"> MD5<br />
<input type="checkbox" name="alg" value="sha1"> SHA-1<br />
<input type="checkbox" name="alg" value="sha256"> SHA-2 (256)<br />
<input type="checkbox" name="alg" value="sha384"> SHA-2 (384)<br />
<input type="checkbox" name="alg" value="sha512"> SHA-2 (512)<br />
<input type="checkbox" name="alg" value="edonkey"> EDONKEY<br />
<input type="checkbox" name="alg" value="tiger"> TIGER<br />
<input type="checkbox" name="alg" value="rmd"> RIPEMD-160<br />
<input type="checkbox" name="alg" value="panama"> PANAMA<br />
<input type="checkbox" name="alg" value="adler"> ADLER32<br />
<br />
Look inside subdirectories?<br />
<input type="hidden" name="rec" value="0" $disabled2>
<input type="radio" name="rec" value="1" $disabled> Yes <input type="radio" name="rec" value="0" checked $disabled> No<br />
<br />
<input type="submit" name="act" value="Calculate" /> <input type="submit" name="act" value="Save to file" />
</form>
</body>
</html>
HTML;
	replace $input,html($html,320,510),"?","&";
	$alg=gettoken($input,1,"&rec=");
	if("$alg"==""){end 1;}
	replace $alg,$alg,"&alg="," -";
	$act=gettoken($input,2,"&act=");
	regexreplace $rec,$input,"^.*?&rec=(\d).+$","$1";
	if("$act"=="Calculate"){$save=0; $kp=k;}
	else{$save=1; $kp=c;}
	if($count<1){
		$rec=($rec==1)?("-r"):("");
		run "cmd /$kp """"<xypath>\tools\fsum.exe"" $rec$alg -jnc -jm -d""<curpath>"" *.*".($save==1)?(" >""<curfolder>_checksums.md5"""""):('""');}
	else{
		$sfvname=($count==1)?("<curname>"):("<curfolder>_checksums");
		$files=($save==1)?("$items >""<curpath>\$sfvname.md5"""):($items);
		strlen $len, $files;
		if($len>8000){
			$files=report("{dir | ""{name}""|}",1);
			run "cmd /$kp """"<xypath>\tools\fsum.exe"" $alg -jnc -jm -d""<curpath>""$files ".($save==1)?(">""$sfvname.md5"""""):('""');
			status "Warning! Exceeded command line lenght limit!",ffff00,alert;}
		else{run "cmd /$kp """"<xypath>\tools\fsum.exe"" $alg -jnc -jm$files""";}}

"Get Checksum : _getchecksum"
	global $getchecksum;
	if("$getchecksum"==""){$getchecksum="-crc32";} // crc32 md5 sha1 edonkey
	end (getinfo(countselected)<1),"Nothing selected";
	$items=report("{Dir | ""{fullpath}\{name}""|}",1);
	if("$items"==""){end 1, "No files selected.";}
	$files=$items.' >"%temp%\checksums.tmp"';
	strlen $len, $files;
	if($len>8000){
		$files=report("{dir |""{name}"" |}",1);
		run "cmd /c """"<xypath>\tools\fsum.exe"" -r $getchecksum -jnc  -d""<curpath>"" $files>""$sfvname.md5""""",1;
		status "Warning! Exceeded command line lenght limit!",ffff00,alert;}
	else{run "cmd /c ""<xypath>\tools\fsum.exe"" -r $getchecksum -jnc$files""",1;}}
	$getchecksum=readfile("%temp%\checksums.tmp");

-

"Cancel"
	end 1;
--------
ale wrote:My problem is that I really fight with quotes and I didn't fully grasp their use in complex situations, how and when the XY script parser eat them and so on
yup, quotes can be a real pain some times.

i'm sure there's an explanation on how to use them somewhere in the wiki, but my internet is so crappy today that i can't even open that page.

your "run" command had some misplaced quotes when i tested it.
i think it should be like this:

Code: Select all

run "$hstart $parameters """"%SystemRoot%\system32\cmd.exe"" /c ""$sha1sum"" ""<curitem>"" | ""$clip""""", , 1;
use the debug window to see if the quotes are in the right places.
Last edited by Muroph on 27 Oct 2009 08:21, edited 2 times in total.
My shared scripts:
TeraCopy Integration, Tag Manager, Comments Menu, Text Replacer, Image and Media Tools, Checksum Calculator, Video Calculator
only 5 URLs are allowed on the sig...

ale
Posts: 127
Joined: 19 Feb 2009 23:23
Location: Italy

Re: SHA-1 calculation

Post by ale »

Muroph wrote:your "run" command had some misplaced quotes when i tested it.
i think it should be like this:

Code: Select all

run "$hstart $parameters """"%SystemRoot%\system32\cmd.exe"" /c ""$sha1sum"" ""<curitem>"" | ""$clip""""", , 1;
use the debug window to see if the quotes are in the right places.
First of all thank you for all the advices! I tried your version and it didn't work that's why I said I struggle with quotes :)
Did you have errors with my "run" command? it worked here :( the complexity comes because I used hstart to avoid the console Window, and hstart has also all his command line parsing, so I had to find a way to have both XY and hstart happy. The way I used to test hstart is with its /TEST command line parameter

admin
Site Admin
Posts: 60567
Joined: 22 May 2004 16:48
Location: Win8.1 @100%, Win10 @100%
Contact:

Re: SHA-1 calculation

Post by admin »

ale wrote:
Muroph wrote:your "run" command had some misplaced quotes when i tested it.
i think it should be like this:

Code: Select all

run "$hstart $parameters """"%SystemRoot%\system32\cmd.exe"" /c ""$sha1sum"" ""<curitem>"" | ""$clip""""", , 1;
use the debug window to see if the quotes are in the right places.
First of all thank you for all the advices! I tried your version and it didn't work that's why I said I struggle with quotes :)
Did you have errors with my "run" command? it worked here :( the complexity comes because I used hstart to avoid the console Window, and hstart has also all his command line parsing, so I had to find a way to have both XY and hstart happy. The way I used to test hstart is with its /TEST command line parameter
Two tips concerning quotes:
(1) You can use single quotes on the outer ends and you do not have to double the contained double-quotes to preserve them.
(2) You can use the function quote() to double-quote a string.

ale
Posts: 127
Joined: 19 Feb 2009 23:23
Location: Italy

Re: SHA-1 calculation

Post by ale »

Thanks for the tips Don, I have to experiment and hopefully improve my understanding, maybe one day I might even write a file manager :D
I was thinking I didn't make very easy to test my snippet so I'll show better the links for the needed tools.

hashutils
http://code.kliu.org/misc/hashutils/

Hidden Start
http://www.ntwind.com/software/utilities/hstart.html

I read clip.exe may be found in Windows Server 2003 (I have not verified yet), but it can be also found on the web, for example here
http://www.petri.co.il/quickly_copy_err ... ssages.htm
scroll down to the section which talks about Windows 2000 and Windows XP

I was very happy with clip because we can capture the output of a command line tool and further process it. I'll see if it works with other tools.

It would be nice to have a topic where we put all known tools which works from the command line about any purpose, text conversion/manipulation, graphics related, network tools, about encryption, anything. I have another promising one to test about graphic manipulation.

NConvert
http://www.xnview.com/en/nconvert.html

Muroph
Posts: 561
Joined: 21 Aug 2007 16:13

Re: SHA-1 calculation

Post by Muroph »

ale wrote:I tried your version and it didn't work that's why I said I struggle with quotes :)
Did you have errors with my "run" command? it worked here :( the complexity comes because I used hstart to avoid the console Window, and hstart has also all his command line parsing, so I had to find a way to have both XY and hstart happy.
ah! hstart also messes around with the quotes.
i didn't consider that. :oops:
ale wrote:It would be nice to have a topic where we put all known tools which works from the command line about any purpose, text conversion/manipulation, graphics related, network tools, about encryption, anything. I have another promising one to test about graphic manipulation.
it looks interesting.
i also have a few of them i could share.
admin wrote:(2) You can use the function quote() to double-quote a string.
quick bug:
a comma in the quote function will break it.

Code: Select all

text quote(test 1, test "2", test (3))
My shared scripts:
TeraCopy Integration, Tag Manager, Comments Menu, Text Replacer, Image and Media Tools, Checksum Calculator, Video Calculator
only 5 URLs are allowed on the sig...

Pagat
Posts: 306
Joined: 09 Oct 2007 21:23
Location: Austria

Re: SHA-1 calculation

Post by Pagat »

Muroph wrote:quick bug:
a comma in the quote function will break it.

Code: Select all

text quote(test 1, test "2", test (3))
it's not a bug. quote only takes one parameter and "," separates parameters, therefore everything after the first "," gets lost.

The following works:

Code: Select all

::text quote('test 1, test "2", test (3)')
or

Code: Select all

::$temp = 'test 1, test "2", test (3)'; text quote($temp);

Muroph
Posts: 561
Joined: 21 Aug 2007 16:13

Re: SHA-1 calculation

Post by Muroph »

now that finals are over i decided to spend some time lerning html.
as my first task, i decided to replace the text input in the "calculate checksums" command with a simple html form.
the new script is in the second post of this thread.

now, if only i could manage to learn javascript by the end of winter break... :)
My shared scripts:
TeraCopy Integration, Tag Manager, Comments Menu, Text Replacer, Image and Media Tools, Checksum Calculator, Video Calculator
only 5 URLs are allowed on the sig...

Post Reply