Page 2 of 2

Re: hashdeep/md5deep (CLI hashing utilities)

Posted: Fri Jan 17, 2020 7:50 pm
by webfork
Midas wrote: Mon Jan 13, 2020 6:39 am ... it just dawned on me that the cause of your problem might in fact be just the default Powershell settings -- counter-intuitively, prior to running any scripts on an untouched Powershell, it's mandatory to change Set-ExecutionPolicy.
That does correspond to some conversations I've had on the topic. The tool I was using got cut off by an admin setting, but I have a different laptop with very different security settings (and a more developer-friendly culture) so I'll see where that stands with this an other powershell tools.

Thanks!

Re: Free & Portable Checksum Tools [file hashing utilities]

Posted: Sat May 09, 2020 5:05 pm
by vevy
Midas wrote: Tue Oct 29, 2019 12:00 pm [Mod note: post moved here from hashdeep topic, where it was somehwhat off-topic.]

A further hashing script I patched together that doesn't require an updated Powershell...

Code: Select all

:: Sidecar MD5 hashing script v0.2 by Midas (2019-10-29)
:: Creates Teracopy v2.x compatible sidecar MD5 hash files
:: WARNING! Won't work if filename contains "&" or other special characters

@ECHO OFF
certutil -v -hashfile %1 MD5 | find /v "MD5" | find /v "CertUtil" > tmp_hash
SET /P hash=<tmp_hash
SET hash=%hash: =%
ECHO:%hash% *%~nx1> "%~n1".md5
DEL tmp_hash
You can simplify the certutil line by filtering out lines with ":":

Code: Select all

certutil -v -hashfile %1 MD5|find /v ":"
You can also do away with temp files:

Code: Select all

set fp="%~1%"
set fn=%~nx1%
for /f %%i in ('certutil -hashfile %fp% sha256 ^|find /v ^":^"') do echo %%i *%fn%>%fn%.sha256

Re: Free & Portable Checksum Tools [file hashing utilities]

Posted: Sun May 10, 2020 12:59 pm
by Midas
vevy wrote:You can simplify the certutil line by filtering out lines with ":":

Code: Select all

certutil -v -hashfile %1 MD5|find /v ":"

That is a great tip, thank you. :sunglasses:

As for the temp file in my original batch script (posted at viewtopic.php?p=94544#p94544), it's by design: call it an instance of Occam's razor. Contrary to your tip above, you'd be making the code more, not less, convoluted, by replacing this:

Code: Select all

CERTUTIL -v -hashfile %1 MD5 | FIND /v ":" > tmp_hash
SET /P hash=<tmp_hash

with this:

Code: Select all

for /f %%i in ('CERTUTIL -hashfile %fp% MD5 ^|find /v ^":^"') do echo %%i *%fn%>%fn%.MD5

I like code to be easily human readable. I know all about FOR loops, but I find them highly error-prone, with rather less than intuitive syntax and overall not really newbie-friendly.

Now, if you can recommend any other way of transferring command output to a local variable, I'm all ears.

Re: Free & Portable Checksum Tools [file hashing utilities]

Posted: Sun May 10, 2020 1:05 pm
by vevy
Midas wrote: Sun May 10, 2020 12:59 pmbut I find them highly error-prone
:?:

Re: Free & Portable Checksum Tools [file hashing utilities]

Posted: Sun May 10, 2020 1:13 pm
by Midas
Scripts are supposed to be used and adapted by human beings, not merely copy and pasted. Just look at this piece of the FOR command manpage...
Syntax
FOR /F ["options"] %%parameter IN ('command_to_process') DO command

Key
options:
delims=xxx The delimiter character(s)
(default = a space or TAB)
skip=n A number of lines to skip at the beginning.
(default = 0)

eol=; Character at the start of each line to indicate a comment
The default is a semicolon ;

tokens=n The numbered items to read from each line
(default = 1)

usebackq Use the alternate quoting style:
- Use double quotes for long file names in "filenameset".
- Use single quotes for 'Text string to process'
- Use back quotes for `command_to_process`

[...]
@ https://ss64.com/nt/for_cmd.html

Re: Free & Portable Checksum Tools [file hashing utilities]

Posted: Sun May 10, 2020 1:21 pm
by vevy
You mean it can trip up the user, not act up? If so, I kind of agree it is not the most intuitive! :)

Re: Free & Portable Checksum Tools [file hashing utilities]

Posted: Sun May 10, 2020 1:32 pm
by Midas
Precisely. :slightly_smiling_face:

Re: Free & Portable Checksum Tools [file hashing utilities]

Posted: Sun May 10, 2020 2:09 pm
by vevy
Midas wrote: Sun May 10, 2020 12:59 pm Now, if you can recommend any other way of transferring command output to a local variable, I'm all ears.
Without using for, forfiles, or a third party tool (like sfk), no!

Other than commenting the commands, you can spread it out:

Code: Select all

set filepath="%~1%"
set filename=%~nx1%

        for /f %%i in ('
certutil -hashfile %filepath% sha256    ^|^
find /v ":"
        ') do ^
echo %%i *%filename%> %filename%.sha256
Each relevant command cleanly in a separate line.

Re: Free & Portable Checksum Tools [file hashing utilities]

Posted: Mon May 11, 2020 1:36 am
by Midas
You're right but I think I'll leave that to user discretion. :neutral_face:

A miss in any of those hard to see diacritic chains and all you've got is a jumble of barely readable non-working code to debug.

My preferred version is much terser and if, however, you have to debug it, way simpler... it's KISS. :stuck_out_tongue_winking_eye:

Re: Free & Portable Checksum Tools [file hashing utilities]

Posted: Mon May 11, 2020 6:10 am
by vevy
Midas wrote: Mon May 11, 2020 1:36 am You're right but I think I'll leave that to user discretion. :neutral_face:

A miss in any of those hard to see diacritic chains and all you've got is a jumble of barely readable non-working code to debug.

My preferred version is much terser and, if however you have to debug it, way simpler... it's KISS. :stuck_out_tongue_winking_eye:
Yeah, it is... well... error-prone :wink:.
It's just a preference of mine, that's all.