I thought it might be useful to have a hash calculator in context menu using certutil.
So I've created 3 types on them. One is for use on individual files, right click on a file choose copy hash, to calculate the hash and copy it to clipboard.
The second and third is available in context menu of directories for generating list of files along with their hash value with a predefined format and copy it to the clipboard, one performs the task only on files which are the direct child of the selected directory and the other will perform the task on the whole subdirectory tree.
I chose SHA1 as the hash algorithm, changing the algorithm is the simple task of performing search and replace over "SHA1".
I appended the registry script to a very simple stub batch code which uses REG.EXE to import the embedded script, addressing the concerns mentioned by Compo regarding UAC prompt by REGEDIT.
It can be easily used to convert a REG script to a self importer script. e.g:
Code: Select all
type Unicode.reg>Ansi.reg & copy ImportStub.bat+Ansi.reg SelfImport.bat & del Ansi.reg
Hash ContextMenus Script:
Code: Select all
:: CmdHashCntxMenu.bat
:: Hash Files/Directories Context Menu
:: SelfImporter Registry Script
@echo off
setlocal EnableExtensions DisableDelayedExpansion
set "TIME="
set "id=%TIME: =0%"
set "id=%~nx0.%id:~0,2%%id:~3,2%%id:~6,2%%id:~-2%"
set "RegFile=%TEMP%\RegImport.%id%.reg.tmp"
(
echo REGEDIT4
echo,
type "%~f0"
)>"%RegFile%" && (
echo Importing registry script...
reg import "%RegFile%"
del /f /q "%RegFile%"
)
pause
goto :EOF
<End of batch script>
Windows Registry Editor Version 5.00
;-------------------------------------------------------------------------------------------------------
## <File SHA1 Value to Clipboard - Comment related sections to exclude from import>
[HKEY_CURRENT_USER\Software\Classes\*\shell\CopyFileHashClip]
@="Copy SHA1 Hash"
[HKEY_CURRENT_USER\Software\Classes\*\shell\CopyFileHashClip\Command]
@="cmd.exe /q /d /c \"title Calculating SHA1 hash&cd /d \"%%SystemRoot%%\\system32\" & echo Generating SHA1 hash over \"%1\" ...& for /f \"delims=\" %%a in ('certutil -hashfile \"%1\" SHA1 ^| find /v \":\"') do set \"_$hash=%%a\"&call set \"_$hash=%%_$hash: =%%\"&<nul set /p \"=%%_$hash%%\" | clip\""
;; Uninstaller - Uncomment to remove the entry from registry
;[-HKEY_CURRENT_USER\Software\Classes\*\shell\CopyFileHashClip]
;-------------------------------------------------------------------------------------------------------
## <DIR SHA1 List to Clipboard - Comment all related to exclude from import>
[HKEY_CURRENT_USER\Software\Classes\Directory\shell\CmdHashListClip]
@="SHA1 List to Clipboard"
[HKEY_CURRENT_USER\Software\Classes\Directory\shell\CmdHashListClip\Command]
@="cmd.exe /d /c \"title Generating SHA1 Hash List to Clipboard...&cd /d \"%%SystemRoot%%\\system32\"&echo Generating SHA1 hash List over files in directory of \"%1\"...&(for /f \"eol=* delims=\" %%f in ('dir \"%1\" /b/a-d 2^^^>nul') do @(for /f \"delims=\" %%h in ('certutil -hashfile \"%1\\%%f\" SHA1 ^^^| find /v \":\"') do @(set \"_$hash=%%h\"&call set \"_$hash=%%_$hash: =%%\"&echo \"%1\\%%f\"...>conOUT$&set \"_$file=%1\\%%f\"&<nul call set /p \"=%%_$file%%:SHA1:%%_$hash%%\"&echo,)))|clip\""
;; Uninstaller - Uncomment to remove the entry from registry
;[-HKEY_CURRENT_USER\Software\Classes\Directory\shell\CmdHashListClip]
;-------------------------------------------------------------------------------------------------------
## <DIR SHA1 List to Clipboard (Tree) - Comment related sections to exclude from import>
[HKEY_CURRENT_USER\Software\Classes\Directory\shell\CmdHashListClipTree]
@="SHA1 List to Clipboard (Tree)"
[HKEY_CURRENT_USER\Software\Classes\Directory\shell\CmdHashListClipTree\Command]
@="cmd.exe /d /c \"title Generating SHA1 Hash List to Clipboard... (Tree)&cd /d \"%%SystemRoot%%\\system32\"&echo Generating SHA1 hash List over files in directory tree of \"%1\"...&(for /f \"delims=\" %%f in ('dir \"%1\" /s/b/a-d 2^^^>nul') do @(for /f \"delims=\" %%h in ('certutil -hashfile \"%%f\" SHA1 ^^^| find /v \":\"') do @(set \"_$hash=%%h\"&call set \"_$hash=%%_$hash: =%%\"&echo \"%%f\"...>conOUT$&set \"_$file=%%f\"&<nul call set /p \"=%%_$file%%:SHA1:%%_$hash%%\"&echo,)))|clip\""
;; Uninstaller - Uncomment to remove the entry from registry
;[-HKEY_CURRENT_USER\Software\Classes\Directory\shell\CmdHashListClipTree]
When I was testing the menu, on one particular directory I couldn't get it to work, nothing was in clipboard, then I saw that clip.bat file in the directory, So I decided to first change the current directory to %SystemRoot%\system32 to be safe.
I opted to not pass
/e:on or
/v:off to cmd, if I want to cover the edge cases then I have to handle the child cmd instances of pipes and FOR /F as well, and that would turn the one-liner command to a very complex and unmanageable beast, and if don't want to handle the childs then there is no point in passing those parameters to the main cmd.
Update 2018-07-23
The directory context menu command which is processing files within the selected directory and not the sub directories, skipped file names beginning with semicolon(;) - Not common for file names but possible - Fixed that.