[Batch] HashShell (Hash Add-on for Context Menu)

Discussion in 'Scripting' started by NormieLyfe, Nov 27, 2017.

  1. NormieLyfe

    NormieLyfe MDL Novice

    May 1, 2017
    37
    37
    0
    #1 NormieLyfe, Nov 27, 2017
    Last edited: Nov 27, 2017
    Hello everbody... this is my second post and I'm happy to contribute something here at MDL and a huge Thanks to @abbodi1406 for the concept of this project


    - About:
    HashShell is a batch script that creates an an add-on in the context menu to calculate a hash of a file and copies it to clipboard, similar to CRC SHA in 7zip. This script uses the same method as "CalcCheckHash"
    Code:
    for /f "skip=1 tokens=* delims=" %%a in ('certutil -hashfile "!!!File!!!" !!!HashAlgo!!!^|findstr /i /v CertUtil') do set "hash=%%a"
    set "output=%hash: =%"
    for %%b in (A,B,C,D,E,F) do set "output=!output:%%b=%%b!"
    

    - Support:
    It Supports Windows 7/8/8.1/10


    - Special Thanks
    @abbodi1406 for the concept and fixed the beta version

    - How to Run or Save as
    Code:
    Copy and Paste in Notepad (or Notepad++, Sublime, or any text editing software)
    Save as... "Install-Uninstall-HashShell.cmd" then Run it
    Tweak it if you want to
    

    - CODE
    Install-Uninstall-HashShell.cmd
    Code:
    @echo off & color 1F & cd /d "%~dp0"
    
    :CheckAdminRights
    set UAC="%temp%\GetAdmin.vbs"
    fltmc >nul 2>&1 || (title Requesting Admin Privileges & (echo Set UAC=CreateObject^("Shell.Application"^):UAC.ShellExecute "%~f0","","","runas",1)>%UAC% & %UAC% & del /f /q %UAC% & exit)
    
    :PreCheck
    set "RegKey=Software\Microsoft\Windows Script Host\Settings"
    for %%a in (HKLM,HKCU) do reg query "%%a\%RegKey%" /v "Enabled">nul 2>&1 && (reg delete "%%a\%RegKey%" /f /v "Enabled">nul 2>&1 & goto PreCheck)
    
    :HashShellMain
    set "loc=%windir%\HashShell" & set "arg=WScript.Arguments.Item" & set "path=HKCR\*\shell\HashShell" & set "algo=MD2,MD4,MD5,SHA1,SHA256,SHA384,SHA512" & set "reg=%windir%\system32\reg.exe"
    if exist "%loc%\*" (
        echo Uninstalling HashShell...
        rd /s /q "%loc%">nul && (echo HashShell Files Deleted) || (echo Failed to delete HashShell Files)
    ) else (
        echo Installing HashShell... & md %loc%
        (echo Set WshShell = CreateObject^("WScript.Shell"^) : WshShell.Run "%loc%\HashShell.cmd """ ^& %arg%^(0^) ^& """ """ ^& %arg%^(1^) ^& """", 0)>"%loc%\SilentRun.vbs" && (echo SilentRun.vbs created) || (echo Failed to create SilentRun.vbs)
        (echo MsgBox "Filename & Directory: " ^& vbCrLf ^& %arg%^(2^) ^& vbCrLf ^& vbCrLf ^& %arg%^(0^) ^& " Hash:" ^& vbCrLf ^& %arg%^(1^) ^& vbCrLf ^& vbCrLf ^& "Copied to clipboard", 0, "Checksum Information")>"%loc%\Message.vbs" && (echo Message.vbs created) || (echo Failed to create Message.vbs)
        (echo @echo off ^& setlocal EnableExtensions EnableDelayedExpansion
        echo for /f "skip=1 tokens=* delims=" %%%%a in ^('certutil -hashfile "%%~1" %%~2^^^| findstr /i /v CertUtil'^) do set "hash=%%%%a"
        echo set "hash=%%hash: =%%"
        echo for %%%%b in ^(A,B,C,D,E,F^) do set "hash=!hash:%%%%b=%%%%b!"
        echo echo %%hash%%^|clip^>nul
        echo %loc%\Message.vbs "%%~2" "%%hash%%" "%%~1"
        echo exit)>"%loc%\HashShell.cmd" && (echo HashShell.cmd created) || (echo Failed to create HashShell.cmd)
        (echo @echo off ^& echo Paste Filename ^^^& Directory ^& set /p file=%%=%%
        echo if exist "%%file%%" ^(for %%%%a in ^(%algo%^) do %loc%\SilentRun.vbs "%%file%%" "%%%%a"^)
        echo exit)>"%loc%\Debugger.cmd" && (echo Debugger.cmd created) || (echo Failed to create Debugger.cmd)
    )
    echo.
    %reg% query "%path%">nul 2>&1 && (
        echo Deleting HashShell Registry...
        %reg% delete "%path%" /f>nul && (echo HashShell Registry Deleted) || (echo Failed to delete HashShell Registry)
    ) || (
        echo Creating HashShell Registry...
        %reg% add "%path%" /f /v "MUIVerb" /t REG_SZ /d "Hash">nul
        %reg% add "%path%" /f /v "SubCommands" /t REG_SZ /d "">nul
        for %%a in (%algo%) do (
            %reg% add "%path%\shell\%%a" /f /v "MUIVerb" /t REG_SZ /d "%%a">nul
            %reg% add "%path%\shell\%%a\command" /f /v "" /t REG_SZ /d "%windir%\System32\cscript.exe %windir%\HashShell\SilentRun.vbs \"%%1\" \"%%a\"">nul
        )
        %reg% add "%path%\shell\Debugger" /f /v "MUIVerb" /t REG_SZ /d "Debugger">nul
        %reg% add "%path%\shell\Debugger\command" /f /v "" /t REG_SZ /d "%windir%\HashShell\Debugger.cmd">nul
        echo HashShell Registry created
    )
    echo. & pause & exit
    
    @abbodi1406's Hash.reg
    Code:
    Windows Registry Editor Version 5.00
    
    [HKEY_CLASSES_ROOT\*\shell\hash]
    "MUIVerb"="Hash"
    "SubCommands"=""
    
    [HKEY_CLASSES_ROOT\*\shell\hash\shell\MD5]
    "MUIVerb"="MD5"
    
    [HKEY_CLASSES_ROOT\*\shell\hash\shell\MD5\command]
    @="cmd /v:on /c \"@echo off&(for /f \"tokens=* delims=\" %%a in ('certutil -hashfile \"%1\" MD5^|findstr /i MD5') do echo %%a&for /f \"skip=1 tokens=* delims=\" %%a in ('certutil -hashfile \"%1\" MD5^|findstr /i /v CertUtil') do set \"hash=%%a\"&set \"hash=!hash: =!\"&echo !hash!&echo !hash!|clip)&echo.&pause\""
    
    [HKEY_CLASSES_ROOT\*\shell\hash\shell\SHA1]
    "MUIVerb"="SHA1"
    
    [HKEY_CLASSES_ROOT\*\shell\hash\shell\SHA1\command]
    @="cmd /v:on /c \"@echo off&(for /f \"tokens=* delims=\" %%a in ('certutil -hashfile \"%1\" SHA1^|findstr /i SHA1') do echo %%a&for /f \"skip=1 tokens=* delims=\" %%a in ('certutil -hashfile \"%1\" SHA1^|findstr /i /v CertUtil') do set \"hash=%%a\"&set \"hash=!hash: =!\"&echo !hash!&echo !hash!|clip)&echo.&pause\""
    
    [HKEY_CLASSES_ROOT\*\shell\hash\shell\SHA256]
    "MUIVerb"="SHA256"
    
    [HKEY_CLASSES_ROOT\*\shell\hash\shell\SHA256\command]
    @="cmd /v:on /c \"@echo off&(for /f \"tokens=* delims=\" %%a in ('certutil -hashfile \"%1\" SHA256^|findstr /i SHA256') do echo %%a&for /f \"skip=1 tokens=* delims=\" %%a in ('certutil -hashfile \"%1\" SHA256^|findstr /i /v CertUtil') do set \"hash=%%a\"&set \"hash=!hash: =!\"&echo !hash!&echo !hash!|clip)&echo.&pause\""
    
    [HKEY_CLASSES_ROOT\*\shell\hash\shell\SHA512]
    "MUIVerb"="SHA512"
    
    [HKEY_CLASSES_ROOT\*\shell\hash\shell\SHA512\command]
    @="cmd /v:on /c \"@echo off&(for /f \"tokens=* delims=\" %%a in ('certutil -hashfile \"%1\" SHA512^|findstr /i SHA512') do echo %%a&for /f \"skip=1 tokens=* delims=\" %%a in ('certutil -hashfile \"%1\" SHA512^|findstr /i /v CertUtil') do set \"hash=%%a\"&set \"hash=!hash: =!\"&echo !hash!&echo !hash!|clip)&echo.&pause\""