[Batch] CalcCheckHash (Hash Calculator & Checker)

Discussion in 'Scripting' started by NormieLyfe, Oct 4, 2017.

  1. NormieLyfe

    NormieLyfe MDL Novice

    May 1, 2017
    37
    33
    0
    #1 NormieLyfe, Oct 4, 2017
    Last edited: Dec 2, 2017
    My very first thread... I'm new at MDL and I hope this helps some of you

    - Info
    Calculates and Check Hashed files using "certutil.exe"
    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!"
    

    - Supports
    It works for Windows 7/8/8.1/10

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


    - Special Thanks to
    @abbodi1406 for a huge help and guidance with this project
    @s1ave77 for fixing the directory error


    - UPDATE: CalcCheckHash-v1.34-Auto.cmd
    Minor tweaks and added tweakable method if user wants to use Powershell or CertUtil to calculate/check Hash
    Code:
    ::Set "PowerShell" or "Certutil" method use to calculate/check hash
    set "Method=Certutil"
    
    powershell (get-filehash -Path '%~2' -algorithm %~1).Hash>"%HashTemp%"
    for /f %%a in ('type "%HashTemp%"') do set "output=%%a"
    exit /b
    


    - Changelogs
    CalcCheckHash-v1.34.cmd
    User defined File or Directory, requested by @dhjohns. All hash calculation will be stored within the hashoutput and if new file or directory is detected will automatically change from checking to calculating, if hash calculations are missing from a directory will auto calculate it again.

    CalcCheckHash-v1.30-Auto.cmd
    A Big help from @abbodi1406 for the new method by using "certutil.exe" it's a much efficient way and it doesn't have compatibility issue with Windows 7 unlike before.

    NOTE: I set the CalcCheckHash to Auto because for the ease of use but you can tweak it in whatever way you want, "right-click, Edit"

    - Minor Update1: CalcCheckHash-v1.31-Auto.cmd
    I added changed and added new options, MD4 & MD2 will be on by default if MD5 is turned off and some minor aesthetics
    Code:
    ::Set "Enabled" or "Disabled" types of Hash Algorithm
    set "SHA2=Enabled" &:: SHA512, SHA384, SHA256
    set "SHA1=Enabled" &:: SHA1
    set "MD5=Enabled" &:: MD5
    
    - Minor Update2: CalcCheckHash-v1.32-Auto.cmd
    I added override option for MD4 & MD2 and minor tweak to lessen space and length
    Code:
    ::Set "Enabled" to Enabled MD4 & MD2 if Disabled or Set it to "Default"
    set "MD=Default"
    
    - Minor Update3: CalcCheckHash-v1.33-Auto.cmd
    Fixed errors when File name has symbols & spaces, Added error logs when errors are found while checking
    Code:
    set "errorLog=CalcCheckError.log"
    


    - CODE
    v1.34 Auto
    Code:
    @echo off & setlocal EnableExtensions EnableDelayedExpansion
    
    set "Scriptname=CalcCheckHash v1.34"
    title %Scriptname% & cd /d "%~dp0" & color 1F
    
    ::Set "PowerShell" or "Certutil" method use to calculate/check hash
    set "Method=Certutil"
    ::Set "Enabled" or "Disabled" types of Hash Algorithm
    set "SHA2=Enabled"
    set "SHA1=Enabled"
    set "MD5=Enabled"
    ::Set "Enabled" to Enabled MD4 & MD2 if Disabled or Set it to "Default"
    set "MD=Enabled"
    ::Set "Enabled" or "Disabled" to calculate & check within subfolders (NOTE: subfolder name with spaces will occur error)
    set "SubDirs=Enabled"
    ::Set "Open" or "Hidden" to hiden or unhide Hash Output
    set "HashHidden=Open"
    ::Name of Hash Output & ErrorLog & HashTemp for Powershell
    set "HashFile=CalcCheckHashFile.md5"
    set "errorLog=CalcCheckError.log"
    set "HashTemp=%temp%\HashOutputTemp.txt"
    echo =======================================&echo ====      %Scriptname%      ====&echo =======================================&echo.
    if exist "%HashFile%" (set switch=check) else (set switch=calculate)
    echo Method:        %Method%
    echo SHA2:         %SHA2%
    echo SHA1:         %SHA1%
    echo MD5:         %MD5%
    echo MD4 ^& MD2:    %MD%
    echo Subfolders:     %SubDirs%
    echo Hash File:     %HashHidden%
    if "%switch%"=="check" (set status=Checking) else (set status=Calculating)
    echo. & echo ================== [%status%] ================== & echo.
    if "%switch%" equ "check" (call :doubleHash) else (echo %hashfile% ^(%date%^) & echo.)> %hashfile%
    if /i "%SubDirs%"=="Enabled" (for /r . %%g in (*) do set "p=%%g" & call :Hashing "!p:%cd%\=!") else (for /f "delims=" %%g in ('dir /b /a:-d *') do set "p=%%g" & call :Hashing "!p!")
    if /i "%switch%"=="calculate" (call :doubleHash & echo All Files Calculated... & if /i "%HashHidden%"=="hidden" attrib +h +r +s "%HashFile%") else (
        if /i "%Integrity%"=="false" (echo There was some Errors found while Checking file Integrity...) else (echo All Files Are Good...)
        if "%dHash%"=="1" (echo WARNING: %HashFile% Integrity wasn't verified, the file may have been tampered with...)
        if /i "%HashHidden%"=="hidden" (attrib +h +r +s "%HashFile%") else (attrib -h -r -s "%HashFile%")
    )
    echo. & pause & exit
    
    :doubleHash
    if "%switch%"=="check" (echo Checking: "%HashFile%") else (echo.)>> %~nx0
    call :checkHash calculateDHash & echo.
    exit /b
    
    :calculateDHash
    if "%switch%"=="check" (findstr /i "%1" "%HashFile%">nul || exit /b)
    if /i "%method%"=="powershell" (call :PwrShllHash %1 "%HashFile%") else (call :CertUtilHash %1 "%HashFile%")
    if "%switch%"=="check" (findstr /i "%output%" "%~nx0">nul && (echo     HashOutput Integrity %1 [CHECK]) || (echo     HashOutput Integrity %1 [ERROR] & (echo Hash File Integrity [ERROR] & echo.)>%errorLog% & set dHash=1))
    if "%switch%"=="calculate" echo %1^: %output%>> %~nx0
    exit /b
    
    :Hashing
    if "%~1"=="%~nx0" exit /b
    if "%~1"=="%HashFile%" exit /b
    if "%~1"=="%errorLog%" exit /b
    if "%switch%"=="calculate" (echo File: "%~1">> %HashFile%) else (echo Checking: "%~1")
    call :checkHash checkCurHash "%~1"
    if "%switch%"=="check" (call :CheckFile "%~1" %fileH% & echo.& if /i "%HashHidden%"=="hidden" attrib +h "%HashFile%") else (echo.>> %HashFile% & echo Hash calculation complete "%~1")
    exit /b
    
    :checkHash
    if /i "%SHA2%"=="Enabled" for %%a in (512,384,256) do call :%1 SHA%%a "%~2"
    if /i "%SHA1%"=="Enabled" call :%1 SHA1 "%~2"
    if /i "%MD5%"=="Enabled" (call :%1 MD5 "%~2" & if /i "%Method%"=="certutil" if /i "%MD%"=="Enabled" for %%a in (4,2) do call :%1 MD%%a "%~2") else if /i "%Method%"=="certutil" (for %%a in (4,2) do call :%1 MD%%a "%~2")
    if /i "%Method%"=="powershell" if exist "%HashTemp%" (del /f /q "%HashTemp%")
    exit /b
    
    :checkCurHash
    if "%switch%"=="check" findstr /i %1 "%HashFile%">nul || exit /b
    if /i "%method%"=="powershell" (call :PwrShllHash %1 "%~2") else if /i "%Method%"=="certutil" (call :CertUtilHash %1 "%~2") else (echo ERROR: No Hash method was choosen)
    if "%switch%"=="check" (call :CheckSum %1 %output% "%~2") else if "%switch%"=="calculate" (echo %1: %output%>> %HashFile%) else (echo %output%)
    exit /b
    
    :PwrShllHash
    powershell (get-filehash -Path '%~2' -algorithm %~1).Hash>"%HashTemp%"
    for /f %%a in ('type "%HashTemp%"') do set "output=%%a"
    exit /b
    
    :CertUtilHash
    for /f "skip=1 tokens=* delims=" %%a in ('certutil -hashfile "%~2" %1^|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!"
    exit /b
    
    :CheckFile
    find /i "%~1" "%HashFile%">nul && (echo     Integrity Check File: [CHECK] & exit /b) || (echo     Integrity Check File: [ERROR] Incorrect File ^& Directory & echo Incorrect File ^& Directory: "%~1">>%errorLog% & set Integrity=false)
    exit /b
    
    :CheckSum
    for %%a in (SHA512,SHA384,SHA256,SHA1,MD5,MD4,MD2) do if "%1"=="%%a" (
    findstr /i "%2" "%HashFile%">nul && (echo     Integrity Check %1: [CHECK]) || (echo     Integrity Check %1: [ERROR] Incorrect Hash & echo Incorrect %1: %2>>%errorLog% & set "check=1" & set Integrity=false)
    )
    goto :EOF
    
    :HashHistory
    

    v1.34 Spinoff (User defined File or Directory)
    Code:
    @echo off & setlocal EnableExtensions EnableDelayedExpansion
    
    set "Scriptname=CalcCheckHash v1.34"
    title %Scriptname% & cd /d "%~dp0" & color 1F
    
    ::Set "Enabled" or "Disabled" types of Hash Algorithm
    set "SHA2=Enabled"
    set "SHA1=Enabled"
    set "MD5=Enabled"
    ::Set "Enabled" to Enabled MD4 & MD2 if Disabled or Set it to "Default"
    set "MD=Default"
    ::Set "Enabled" or "Disabled" to calculate & check within subfolders (NOTE: subfolder name with spaces will occur error)
    set "SubDirs=Enabled"
    ::Name of Hash Output & ErrorLog
    set "HashFile=CalcCheckHashFile.md5"
    set "errorLog=CalcCheckError.log"
    :Menu
    cls
    echo =======================================&echo ====      %Scriptname%      ====&echo =======================================&echo.
    echo SHA2:         %SHA2%
    echo SHA1:         %SHA1%
    echo MD5:         %MD5%
    echo MD4 ^& MD2:    %MD%
    echo Subfolders:     %SubDirs%
    echo. & echo Place custom File or Directory: & echo.
    set "select=" & set /p select=%=%
    if "%select%"=="" goto Menu
    if exist "%select%\*" (
    if "%select%\" neq "%~dp0" (cd /d "%select%" & set "HashFile=%~dp0%HashFile%" & set "errorLog=%~dp0%errorLog%")
    if /i "%SubDirs%"=="Enabled" (for /r . %%g in (*) do set "p=%%g" & call :Hashing "!p:%select%\=!") else (for /f "delims=" %%g in ('dir /b /a:-d *') do set "p=%%g" & call :Hashing "!p!")
    goto Well
    ) else if exist "%select%" (call :Hashing "%select%" & goto Well)
    goto Menu
    :Well
    echo. & pause & goto Menu
    
    :Hashing
    if "%~1"=="%HashFile%" exit /b
    set "switch=check"
    if exist "%HashFile%" (find /i "%~1" "%HashFile%">nul || set switch=calculate) else (set switch=calculate)
    if "%switch%"=="calculate" (echo File: "%~1">> %HashFile%) else (echo. & echo Checking: "%~1")
    call :checkHash checkCurHash "%~1"
    if "%switch%"=="calculate" (echo.>> %HashFile% & echo. & echo Hash calculation complete "%~1")
    exit /b
    
    :checkHash
    if /i "%SHA2%"=="Enabled" for %%a in (512,384,256) do call :%1 SHA%%a "%~2"
    if /i "%SHA1%"=="Enabled" call :%1 SHA1 "%~2"
    if /i "%MD5%"=="Enabled" (call :%1 MD5 "%~2" & if /i "%MD%"=="Enabled" for %%a in (4,2) do call :%1 MD%%a "%~2") else (for %%a in (4,2) do call :%1 MD%%a "%~2")
    exit /b
    
    :checkCurHash
    if "%switch%" equ "check" findstr /i %1 "%HashFile%">nul || exit /b
    call :CalcCurrentHash %1 "%~2"
    exit /b
    
    :CalcCurrentHash
    for /f "skip=1 tokens=* delims=" %%a in ('certutil -hashfile "%~2" %1^|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!"
    if "%switch%" equ "check" (call :CheckSum %1 %output% "%~2") else if "%switch%" equ "calculate" (echo %1: %output%>> %HashFile%) else (echo %output%)
    exit /b
    
    :CheckSum
    set check=0
    findstr /i "%2" "%HashFile%">nul && (set algoHash=%2) || (echo     Integrity Check %1: [ERROR] Hash Not Found & echo %1  Hash Not Found: %3>>%errorLog% & set "Integrity=false" & exit /b)
    for %%a in (SHA512,SHA384,SHA256,SHA1,MD5,MD4,MD2) do if "%1"=="%%a" if "%2" neq "%algoHash%" (echo     Integrity Check %1: [ERROR] Incorrect Hash & echo Incorrect %1: %2>>%errorLog% & set "check=1" & set Integrity=false)
    if "%check%"=="0" (echo     Integrity Check %1: [CHECK] & set fileH=true) else (set check=0)
    goto :EOF
    

    v1.33 Auto
    Code:
    @echo off & setlocal EnableExtensions EnableDelayedExpansion
    
    set "Scriptname=CalcCheckHash v1.33"
    title %Scriptname% & cd /d "%~dp0" & color 1F
    
    ::Set "Enabled" or "Disabled" types of Hash Algorithm
    set "SHA2=Enabled"
    set "SHA1=Enabled"
    set "MD5=Enabled"
    ::Set "Enabled" to Enabled MD4 & MD2 if Disabled or Set it to "Default"
    set "MD=Default"
    ::Set "Enabled" or "Disabled" to calculate & check within subfolders (NOTE: subfolder name with spaces will occur error)
    set "SubDirs=Enabled"
    ::Set "Open" or "Hidden" to hiden or unhide Hash Output
    set "HashHidden=Open"
    ::Name of Hash Output & ErrorLog
    set "HashFile=CalcCheckHashFile.md5"
    set "errorLog=CalcCheckError.log"
    echo =======================================&echo ====      %Scriptname%      ====&echo =======================================&echo.
    if exist %HashFile% (set switch=check) else (set switch=calculate)
    echo SHA2:         %SHA2%
    echo SHA1:         %SHA1%
    echo MD5:         %MD5%
    echo MD4 ^& MD2:    %MD%
    echo Subfolders:     %SubDirs%
    echo Hash File:     %HashHidden%
    if "%switch%"=="check" (set status=Checking) else (set status=Calculating)
    echo. & echo ================== [%status%] ================== & echo.
    if "%switch%" equ "check" (call :doubleHash) else (echo %hashfile% ^(%date%^) & echo.)> %hashfile%
    if /i "%SubDirs%"=="Enabled" (for /r . %%g in (*) do set "p=%%g" & call :Hashing "!p:%cd%\=!") else (for /f "delims=" %%g in ('dir /b /a:-d *') do set "p=%%g" & call :Hashing "!p!")
    if /i "%switch%"=="calculate" (call :doubleHash & echo All Files Calculated... & if /i "%HashHidden%"=="hidden" attrib +h +r +s "%HashFile%") else (
    if /i "%Integrity%"=="false" (echo There was some Errors found while Checking file Integrity...) else (echo All Files Are Good...)
    if "%dHash%"=="1" (echo WARNING: %HashFile% Integrity wasn't verified, the file may have been tampered with...)
    if /i "%HashHidden%"=="hidden" (attrib +h +r +s "%HashFile%") else (attrib -h -r -s "%HashFile%")
    )
    echo. & pause & exit
    
    :doubleHash
    if "%switch%"=="check" (echo Checking: "%HashFile%") else (echo.)>> %~nx0
    call :checkHash checkDHash "%~1" & echo.
    exit /b
    
    :checkDHash
    if "%switch%"=="check" findstr /i "%1" "%HashFile%">nul || exit /b
    call :calculateDHash %1 "%2"
    exit /b
    
    :calculateDHash
    for /f "skip=1 tokens=* delims=" %%a in ('certutil -hashfile "%HashFile%" %1^|findstr /i /v CertUtil') do set "hash=%%a"
    set "doubleH=%hash: =%"
    for %%b in (A,B,C,D,E,F) do set "doubleH=!doubleH:%%b=%%b!"
    if "%switch%" equ "check" (findstr /i "%doubleH%" "%~nx0">nul && (echo     HashOutput Integrity %1 [CHECK]) || (echo     HashOutput Integrity %1 [ERROR] & (echo Hash File Integrity [ERROR] & echo.)>%errorLog% & set dHash=1))
    if "%switch%" equ "calculate" echo %1^: %doubleH%>> %~nx0
    exit /b
    
    :Hashing
    if "%~1"=="%~nx0" exit /b
    if "%~1"=="%HashFile%" exit /b
    if "%~1"=="%errorLog%" exit /b
    if "%switch%"=="calculate" (echo File: "%~1">> %HashFile%) else (echo Checking: "%~1")
    call :checkHash checkCurHash "%~1"
    if "%switch%"=="check" (call :CheckFile "%~1" %fileH% & echo.& if /i "%HashHidden%"=="hidden" attrib +h "%HashFile%") else (echo.>> %HashFile% & echo Hash calculation complete "%~1")
    exit /b
    
    :checkHash
    if /i "%SHA2%"=="Enabled" for %%a in (512,384,256) do call :%1 SHA%%a "%~2"
    if /i "%SHA1%"=="Enabled" call :%1 SHA1 "%~2"
    if /i "%MD5%"=="Enabled" (call :%1 MD5 "%~2" & if /i "%MD%"=="Enabled" for %%a in (4,2) do call :%1 MD%%a "%~2") else (for %%a in (4,2) do call :%1 MD%%a "%~2")
    exit /b
    
    :checkCurHash
    if "%switch%" equ "check" findstr /i %1 "%HashFile%">nul || exit /b
    call :CalcCurrentHash %1 "%~2"
    exit /b
    
    :CalcCurrentHash
    for /f "skip=1 tokens=* delims=" %%a in ('certutil -hashfile "%~2" %1^|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!"
    if "%switch%" equ "check" (call :CheckSum %1 %output% "%~2") else if "%switch%" equ "calculate" (echo %1: %output%>> %HashFile%) else (echo %output%)
    exit /b
    
    :CheckFile
    find /i "%~1" "%HashFile%">nul && (echo     Integrity Check File: [CHECK] & exit /b) || (echo     Integrity Check File: [ERROR] Incorrect File ^& Directory & echo Incorrect File ^& Directory: "%~1">>%errorLog% & set Integrity=false)
    exit /b
    
    :CheckSum
    set check=0
    findstr /i "%2" "%HashFile%">nul && (set algoHash=%2) || (echo     Integrity Check %1: [ERROR] Hash Not Found & echo %1  Hash Not Found: %3>>%errorLog% & set "Integrity=false" & exit /b)
    for %%a in (SHA512,SHA384,SHA256,SHA1,MD5,MD4,MD2) do if "%1"=="%%a" if "%2" neq "%algoHash%" (echo     Integrity Check %1: [ERROR] Incorrect Hash & echo Incorrect %1: %2>>%errorLog% & set "check=1" & set Integrity=false)
    if "%check%"=="0" (echo     Integrity Check %1: [CHECK] & set fileH=true) else (set check=0)
    goto :EOF
    
    :HashHistory
    

    v1.32 Auto
    Code:
    @echo off & setlocal EnableExtensions EnableDelayedExpansion
    
    set "Scriptname=CalcCheckHash v1.32"
    title %Scriptname% & cd /d "%~dp0" & color 1F
    
    ::Set "Enabled" or "Disabled" types of Hash Algorithm
    set "SHA2=Enabled"
    set "SHA1=Enabled"
    set "MD5=Enabled"
    ::Set "Enabled" to Enabled MD4 & MD2 if Disabled or Set it to "Default"
    set "MD=Default"
    ::Set "Enabled" or "Disabled" to calculate & check within subfolders (NOTE: subfolder name with spaces will occur error)
    set "SubDirs=Enabled"
    ::Set "Open" or "Hidden" to hiden or unhide Hash Output
    set "HashHidden=Open"
    ::Name of Hash Output
    set "HashFile=CalcCheckHashFile.md5"
    
    echo =======================================&echo ====      %Scriptname%      ====&echo =======================================&echo. & set "spc=    "
    if exist %HashFile% (set "switch=check") else (set "switch=calculate")
    echo SHA2:         %SHA2%
    echo SHA1:         %SHA1%
    echo MD5:         %MD5%
    echo MD4 ^& MD2:    %MD%
    echo Subfolders:     %SubDirs%
    echo Hash File:     %HashHidden%
    if "%switch%"=="check" (set status=Checking) else (set status=Calculating)
    echo. & echo ================== [%status%] ================== & echo.
    if "%switch%" equ "check" (call :doubleHash) else (echo %hashfile% ^(%date%^) & echo.)> %hashfile%
    if /i "%SubDirs%"=="Enabled" (for /r . %%g in (*) do set "p=%%g" & call :Hashing "!p:%cd%\=!") else (for /f "delims=" %%g in ('dir /b /a:-d *') do set "p=%%g" & call :Hashing "!p!")
    if /i "%switch%"=="check" (
    if /i "%Integrity%"=="false" (echo There was some Errors found while Checking file Integrity...) else (echo All Files Are Good...)
    if /i "%HashHidden%"=="hidden" (attrib +h +r +s "%HashFile%") else (attrib -h -r -s "%HashFile%")
    ) else (call :doubleHash & echo All Files Calculated... & if /i "%HashHidden%"=="hidden" attrib +h +r +s "%HashFile%")
    echo. & pause & exit
    
    :doubleHash
    if "%switch%" equ "check" (echo Checking: "%HashFile%") else (echo.)>> %~nx0
    call :checkHash checkDHash "%~1"
    if "%switch%" equ "check" (echo. & if '%dHash%' equ '1' echo %HashFile% Integrity [ERROR] Hashoutput may be tampered with... & echo.)
    exit /b
    
    :checkDHash
    if "%switch%" equ "check" findstr /i "%1" "%HashFile%">nul || exit /b
    call :calculateDHash %1 "%2"
    exit /b
    
    :calculateDHash
    for /f "skip=1 tokens=* delims=" %%a in ('certutil -hashfile "%HashFile%" %1^|findstr /i /v CertUtil') do set "hash=%%a"
    set "doubleH=%hash: =%"
    for %%b in (A,B,C,D,E,F) do set "doubleH=!doubleH:%%b=%%b!"
    if "%switch%" equ "check" (call :ChechHashOutput %doubleH% %1) else (echo %1^: %doubleH%)>> %~nx0
    exit /b
    
    :ChechHashOutput
    findstr /i "%1" "%~nx0" > nul && (echo %spc%HashOutput Integrity %2 [CHECK]) || (echo %spc%HashOutput Integrity %2 [ERROR] & set dHash=1)
    exit /b
    
    :Hashing
    if "%~1" equ "%~nx0" exit /b
    if "%~1" equ "%HashFile%" exit /b
    if "%switch%" equ "calculate" (echo File: %~1>> %HashFile%) else (echo Checking: "%~1")
    call :checkHash checkCurHash "%~1"
    if "%switch%" equ "check" (call :CheckFile %~1 %fileH% & echo. & if /i "%HashHidden%"=="hidden" attrib +h "%HashFile%") else (echo. >> %HashFile% & echo Hash calculation complete "%~1")
    exit /b
    
    :checkHash
    if /i "%SHA2%"=="Enabled" for %%a in (512,384,256) do call :%1 SHA%%a "%~2"
    if /i "%SHA1%"=="Enabled" call :%1 SHA1 "%~2"
    if /i "%MD5%"=="Enabled" (call :%1 MD5 "%~2" & if /i "%MD%"=="Enabled" for %%a in (4,2) do call :%1 MD%%a "%~2") else (for %%a in (4,2) do call :%1 MD%%a "%~2")
    exit /b
    
    :checkCurHash
    if "%switch%" equ "check" findstr /i "%1" "%HashFile%">nul || exit /b
    call :CalcCurrentHash %1 "%2"
    exit /b
    
    :CalcCurrentHash
    for /f "skip=1 tokens=* delims=" %%a in ('certutil -hashfile "%~2" %1^|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!"
    if "%switch%" equ "check" (call :CheckSum %1 %output%) else if "%switch%" equ "calculate" (echo %1: %output%>> %HashFile%) else (echo %output%)
    exit /b
    
    :CheckFile
    find /i "%1" "%HashFile%"> nul && (echo %spc%Integrity Check File: [CHECK] & goto :EOF) || (
        if "%~2" equ "true" (echo %spc%Integrity Check File: [ERROR] Modified FileName/FileDirectory) else (echo %spc%Integrity Check File: [ERROR] Incorrect File/Directory & set "Integrity=false")
    )
    exit /b
    
    :CheckSum
    set "check=0"
    findstr /i "%2" "%HashFile%"> nul && (set algoHash=%2) || (set "Integrity=false" & echo %spc%Integrity Check %1: [ERROR] Hash Not Found & exit /b)
    for %%a in (SHA512,SHA384,SHA256,SHA1,MD5,MD4,MD2) do if "%1" equ "%%a" if "%2" neq "%algoHash%" (echo %spc%Integrity Check %1: [ERROR] Incorrect Hash & set "Integrity=false" & set "check=1")
    if "%check%" equ "0" (echo     Integrity Check %1: [CHECK] & set fileH=true) else (set check=0)
    goto :EOF
    
    :HashHistory
    

    v1.31 Auto
    Code:
    @echo off & setlocal EnableExtensions EnableDelayedExpansion
    
    set "Scriptname=CalcCheckHash v1.31"
    title %Scriptname% & cd /d "%~dp0" & color 1F
    
    ::Set "Enabled" or "Disabled" types of Hash Algorithm
    set "SHA2=Enabled"
    set "SHA1=Enabled"
    set "MD5=Enabled"
    ::Set "Enabled" or "Disabled" to calculate or check within subfolders (NOTE: subfolder name with spaces will occur error)
    set "SubDirs=Enabled"
    ::Set "Open" or "Hidden" to hiden or unhide Hash Output
    set "HashHidden=Open"
    ::Name of Hash Output
    set "HashFile=CalcCheckHashFile.md5"
    
    echo =======================================&echo ====      %Scriptname%      ====&echo =======================================&echo. & set "spc=    "
    if exist %HashFile% (set "switch=check") else (set "switch=calculate")
    echo SHA2:         %SHA2%
    echo SHA1:         %SHA1%
    echo MD5:         %MD5%
    echo Subfolders:     %SubDirs%
    echo Hash File:     %HashHidden%
    if "%switch%"=="check" (set status=Checking) else (set status=Calculating)
    echo. & echo ================== [%status%] ================== & echo.
    if "%switch%" equ "check" (call :doubleHash) else (echo %hashfile% ^(%date%^))> %hashfile%
    if /i "%SubDirs%"=="disabled" (
        for /f "delims=" %%g in ('dir /b /a:-d *') do set "p=%%g" & call :Hashing "!p!"
    ) else (for /r . %%g in (*) do set "p=%%g" & call :Hashing "!p:%cd%\=!")
    
    if /i "%switch%"=="check" (
    if /i "%Integrity%"=="false" (echo There was some Errors found while Checking file Integrity...) else (echo All Files Are Good...)
    if /i "%HashHidden%"=="hidden" (attrib +h +r +s "%HashFile%") else (attrib -h -r -s "%HashFile%")
    ) else (call :doubleHash & echo All Files Calculated... & if /i "%HashHidden%"=="hidden" attrib +h +r +s "%HashFile%")
    echo. & pause & exit
    
    :doubleHash
    if "%switch%" equ "check" (echo Checking: "%HashFile%") else (echo.)>> %~nx0
    if /i "%SHA2%"=="enabled" call :SHA2D %~1
    if /i "%SHA1%"=="enabled" call :SHA1D %~1
    if /i "%MD5%"=="enabled" (call :MD5D %~1) else (for %%a in (4,2) do call :checkDoubleHash MD%%a "%~1")
    if "%switch%" equ "check" (echo. & if '%dHash%' equ '1' echo %HashFile% Integrity [ERROR] Hashoutput may be tampered with... & echo.)
    exit /b
    
    :SHA2D
    if "%switch%" equ "check" for %%a in (512,384,256) do findstr /i "SHA%%a" "%HashFile%">nul || exit /b
    for %%a in (512,384,256) do call :checkDoubleHash SHA%%a "%1"
    exit /b
    
    :SHA1D
    if "%switch%" equ "check" findstr /i "SHA1" "%HashFile%">nul || exit /b
    call :checkDoubleHash SHA1 "%1"
    exit /b
    
    :MD5D
    if "%switch%" equ "check" findstr /i "MD5" "%HashFile%">nul || exit /b
    call :checkDoubleHash MD5 "%1"
    exit /b
    
    :checkDoubleHash
    for /f "skip=1 tokens=* delims=" %%a in ('certutil -hashfile "%HashFile%" %1^|findstr /i /v CertUtil') do set "hash=%%a"
    set "doubleH=%hash: =%"
    for %%b in (A,B,C,D,E,F) do set "doubleH=!doubleH:%%b=%%b!"
    if "%switch%" equ "check" (call :ChechHashOutput %doubleH% %1) else (echo %1^: %doubleH%)>> %~nx0
    exit /b
    
    :ChechHashOutput
    findstr /i "%1" "%~nx0" > nul && (echo %spc%HashOutput Integrity %2 [CHECK]) || (echo %spc%HashOutput Integrity %2 [ERROR] & set dHash=1)
    exit /b
    
    :Hashing
    if "%~1" equ "%~nx0" exit /b
    if "%~1" equ "%HashFile%" exit /b
    if "%switch%" equ "calculate" (echo File: %~1>> %HashFile%) else (echo Checking: "%~1")
    if /i "%SHA2%"=="enabled" call :SHA2 %~1
    if /i "%SHA1%"=="enabled" call :SHA1 %~1
    if /i "%MD5%"=="enabled" (call :MD5 %~1) else (for %%a in (4,2) do call :CalcCurrentHash MD%%a "%~1")
    if "%switch%" equ "check" (call :CheckFile %~1 %fileH% & if /i "%HashHidden%"=="hidden" attrib +h "%HashFile%")
    if "%switch%" equ "calculate" (echo. >> %HashFile% & echo Hash calculation complete "%~1") else (echo.)
    exit /b
    
    :SHA2
    if "%switch%" equ "check" for %%a in (512,384,256) do findstr /i "SHA%%a" "%HashFile%">nul || exit /b
    for %%a in (512,384,256) do call :CalcCurrentHash SHA%%a "%1"
    exit /b
    
    :SHA1
    if "%switch%" equ "check" findstr /i "SHA1" "%HashFile%">nul || exit /b
    call :CalcCurrentHash SHA1 "%1"
    exit /b
    
    :MD5
    if "%switch%" equ "check" findstr /i "MD5" "%HashFile%">nul || exit /b
    call :CalcCurrentHash MD5 "%1"
    exit /b
    
    :CalcCurrentHash
    for /f "skip=1 tokens=* delims=" %%a in ('certutil -hashfile "%~2" %1^|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!"
    if "%switch%" equ "check" (call :CheckSum %1 %output%) else if "%switch%" equ "calculate" (echo %1: %output%>> %HashFile%) else (echo %output%)
    exit /b
    
    :CheckFile
    find /i "%1" "%HashFile%"> nul && (echo %spc%Integrity Check File: [CHECK] & goto :EOF) || (
        if "%~2" equ "true" (echo %spc%Integrity Check File: [ERROR] Modified FileName/FileDirectory) else (echo %spc%Integrity Check File: [ERROR] Incorrect File/Directory & set "Integrity=false")
    )
    exit /b
    
    :CheckSum
    set "check=0"
    findstr /i "%2" "%HashFile%"> nul && (set algoHash=%2) || (set "Integrity=false" & echo %spc%Integrity Check %1: [ERROR] Hash Not Found & exit /b)
    for %%a in (SHA512,SHA384,SHA256,SHA1,MD5,MD4,MD2) do if "%1" equ "%%a" if "%2" neq "%algoHash%" (echo %spc%Integrity Check %1: [ERROR] Incorrect Hash & set "Integrity=false" & set "check=1")
    if "%check%" equ "0" (echo     Integrity Check %1: [CHECK] & set fileH=true) else (set check=0)
    goto :EOF
    
    :HashHistory
    

    v1.30 Auto
    Code:
    @echo off & setlocal EnableExtensions EnableDelayedExpansion
    
    set "Scriptname=CalcCheckHash v1.30"
    title %Scriptname% & cd /d "%~dp0" & color 1F
    
    ::Set "Enabled" or "Disabled" for SHA512 & SHA384
    set "moreSHA2=Enabled"
    ::Set "Enabled" or "Disabled" to calculate or check within subfolders (NOTE: subfolder name with spaces will occur error)
    set "SubDirs=Enabled"
    ::Set "Open" or "Hidden" to hiden or unhide Hash Output
    set "HashHidden=Open"
    ::Name of Hash Output
    set "HashFile=CalcCheckHashFile.md5"
    
    echo =======================================&echo ====      %Scriptname%      ====&echo =======================================&echo. & set "spc=    "
    if exist %HashFile% (set "switch=check") else (set "switch=calculate")
    echo More SHA2:     %moreSHA2%
    echo Subfolders:     %SubDirs%
    echo Hash File:     %HashHidden%
    echo.
    if "%switch%" equ "check" (call :doubleHash) else (echo %hashfile% ^(%date%^) SHA2 512 ^& 384: %moreSHA2% & echo.)> %hashfile%
    if /i "%SubDirs%"=="disabled" (
        for /f "delims=" %%g in ('dir /b /a:-d *') do set "p=%%g" & call :Hashing "!p!"
    ) else (for /r . %%g in (*) do set "p=%%g" & call :Hashing "!p:%cd%\=!")
    
    if "%switch%" equ "check" (
    echo All Files Are Good... & if /i "%HashHidden%"=="hidden" (attrib +h +r +s "%HashFile%") else (attrib -h -r -s "%HashFile%")
    ) else (call :doubleHash & echo All Files Calculated... & if /i "%HashHidden%"=="hidden" attrib +h +r +s "%HashFile%")
    if "%Integrity%" equ "false" (echo There was some Errors found while Checking file Integrity...)
    echo. & pause & exit
    
    :doubleHash
    if "%switch%" equ "check" (echo Checking: "%HashFile%") else (echo.)>> %~nx0
    if "%switch%" equ "check" findstr /i "enabled" "%HashFile%">nul && (if /i "%moreSHA2%"=="enabled" for %%a in (SHA512,SHA384) do call :checkDoubleHash %%a)
    if "%switch%" equ "calculate" if /i "%moreSHA2%"=="enabled" for %%a in (SHA512,SHA384) do call :checkDoubleHash %%a
    for %%a in (SHA256,SHA1,MD5) do call :checkDoubleHash %%a
    if "%switch%" equ "check" (echo. & if '%dHash%' equ '1' echo %HashFile% Integrity [ERROR] Hashoutput may be tampered with... & echo.)
    exit /b
    
    :checkDoubleHash
    for /f "skip=1 tokens=* delims=" %%a in ('certutil -hashfile "%HashFile%" %1^|findstr /i /v CertUtil') do set "hash=%%a"
    set "doubleH=%hash: =%"
    for %%b in (A,B,C,D,E,F) do set "doubleH=!doubleH:%%b=%%b!"
    if "%switch%" equ "check" (call :ChechHashOutput %doubleH% %1) else (echo %1^: %doubleH%)>> %~nx0
    exit /b
    
    :ChechHashOutput
    findstr /i "%1" "%~nx0" > nul && (echo %spc%HashOutput Integrity %2 [CHECK]) || (echo %spc%HashOutput Integrity %2 [ERROR] & set dHash=1)
    exit /b
    
    :Hashing
    if "%~1" equ "%~nx0" exit /b
    if "%~1" equ "%HashFile%" exit /b
    if "%switch%" equ "calculate" (echo File: %~1>> %HashFile%) else (echo Checking: "%~1")
    if /i "%moreSHA2%"=="enabled" call :moreSHA2 %~1
    for %%a in (SHA256,SHA1,MD5) do call :CalcCurrentHash %%a "%1"
    if "%switch%" equ "check" (call :CheckFile %~1 %fileH% & if /i "%HashHidden%"=="hidden" attrib +h "%HashFile%")
    if "%switch%" equ "calculate" (echo. >> %HashFile% & echo Hash calculation complete "%~1") else (echo.)
    exit /b
    
    :moreSHA2
    if "%switch%" equ "check" findstr /i "enabled" "%HashFile%">nul || exit /b
    for %%a in (SHA512,SHA384) do call :CalcCurrentHash %%a "%1"
    exit /b
    
    :CalcCurrentHash
    for /f "skip=1 tokens=* delims=" %%a in ('certutil -hashfile "%~2" %1^|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!"
    if "%switch%" equ "check" (call :CheckSum %1 %output%) else if "%switch%" equ "calculate" (echo %1: %output%>> %HashFile%) else (echo %output%)
    exit /b
    
    :CheckFile
    find /i "%1" "%HashFile%"> nul && (echo %spc%Integrity Check File: [CHECK] & goto :EOF) || (
        if "%~2" equ "true" (echo %spc%Integrity Check File: [ERROR] Modified FileName/FileDirectory) else (echo %spc%Integrity Check File: [ERROR] Incorrect File/Directory & set "Integrity=false")
    )
    exit /b
    
    :CheckSum
    set "check=0"
    findstr /i "%2" "%HashFile%"> nul && (set algoHash=%2)
    if "%1" equ "SHA512" if "%2" neq "%algoHash%" (call :hashError %1)
    if "%1" equ "SHA384" if "%2" neq "%algoHash%" (call :hashError %1)
    if "%1" equ "SHA256" if "%2" neq "%algoHash%" (call :hashError %1)
    if "%1" equ "SHA1" if "%2" neq "%algoHash%" (call :hashError %1)
    if "%1" equ "MD5" if "%2" neq "%algoHash%" (call :hashError %1)
    if "%check%" equ "0" (echo     Integrity Check %1: [CHECK] & set fileH=true) else (set check=0)
    goto :EOF
    
    :hashError
    set "Integrity=false" & set "check=1"
    echo %spc%Integrity Check %1: [ERROR] Incorrect Hash
    exit /b
    
    :HashHistory
    
     
  2. abbodi1406

    abbodi1406 MDL KB0000001

    Feb 19, 2011
    8,391
    28,863
    270
    Glad to see your first contribution
    keep up the fine work :)
     
  3. NormieLyfe

    NormieLyfe MDL Novice

    May 1, 2017
    37
    33
    0
    Thank You
     
  4. NormieLyfe

    NormieLyfe MDL Novice

    May 1, 2017
    37
    33
    0
    UPDATE: (6/10/17)

    I forgot to add the Hidden Hash Output option for the, w/ user interaction....

    Code:
    :menu
    call :watermark
    if "%SHA2%" equ "true" (set "SHA2status=Enabled") else (set "SHA2status=Disabled")
    if "%SubDirs%" equ "true" (set "SubDirsstatus=Enabled") else (set "SubDirsstatus=Disabled")
    if "%HashHidden%" equ "true" (set "HashHiddenstatus=Enable") else (set "HashHiddenstatus=Disable")
    echo What would you like to do? & echo.
    echo SHA2 Status: %SHA2status%
    echo Subfolder Status: %SubDirsstatus%
    echo Hidden Hash Output: %HashHiddenstatus% & echo.
    echo [0] Enable/Disable SHA-2
    echo [1] Enable/Disable Subfolder Files
    echo [2] Enable/Disable Hidden Hash Output
    echo [3] Calculate Hash
    echo [4] Check Hash
    echo [X] Exit
    echo.
    choice /C 01234X /N /M ""
    if errorlevel 6 exit
    if errorlevel 5 (set "switch=check" & cls & goto check)
    if errorlevel 4 (set "switch=calculate" & cls & goto check)
    if errorlevel 3 if "%HashHidden%" equ "true" (set "HashHidden=false" & goto menu) else if "%HashHidden%" equ "false" (set "HashHidden=true" & goto menu)
    if errorlevel 2 if "%SubDirs%" equ "true" (set "SubDirs=false" & goto menu) else if "%SubDirs%" equ "false" (set "SubDirs=true" & goto menu)
    if errorlevel 1 if "%SHA2%" equ "true" (set "SHA2=false" & goto menu) else if "%SHA2%" equ "false" (set "SHA2=true" & goto menu)
    goto :EOF
    
     
  5. NormieLyfe

    NormieLyfe MDL Novice

    May 1, 2017
    37
    33
    0
    UPDATE: 13/10/17

    Sorry if I tried and circumventing the "you must have 5 posts before posting any links" rule, I change some minor workings in it, that's all

    Code:
    - DOWNLOAD
    Name: CalcCheckHash-v1.27.zip
    SHA1: CFD3EDDD2051290810964A8E3B147323FEA6EE75
    MD5: 1C9DE8F26F76A2BF6D2755E6D0A7FD85
    
    Link: https:// racaty.com/9oxptwx9x1hi
    (Sorry If I'm circumventing the 5 posts before link thing)
    
     
  6. tonto11

    tonto11 MDL Addicted

    Jun 18, 2012
    605
    272
    30
    Good work , but

    Be careful that you really get what you want from this site
    racaty tries to give you hdvideoplayer, and adobe flash before calccheckhash

    ...T
     
  7. NormieLyfe

    NormieLyfe MDL Novice

    May 1, 2017
    37
    33
    0
    #7 NormieLyfe, Oct 13, 2017
    Last edited: Oct 13, 2017
    (OP)
    I used the sites I saw from @abbodi1406's post but I didn't experience racaty suggesting me programs before showing the download link
     
  8. NormieLyfe

    NormieLyfe MDL Novice

    May 1, 2017
    37
    33
    0
    #8 NormieLyfe, Nov 21, 2017
    Last edited: Nov 21, 2017
    (OP)
    UPDATE: CalcCheckHash-v1.30-Auto.cmd

    New hashing method used (Huge Thanks to @Addobi1406)
    Minor Tweaks


    MINOR UPDATE1 CalcCheckHash-v1.31-Auto.cmd

    Added options for SHA2, SHA1 & MD5 and some aesthetic

    MINOR UPDATE2 CalcCheckHash-v1.32-Auto.cmd

    Added override option for MD4 & MD2 and small cleaning for redundant codes
     
  9. abbodi1406

    abbodi1406 MDL KB0000001

    Feb 19, 2011
    8,391
    28,863
    270
  10. NormieLyfe

    NormieLyfe MDL Novice

    May 1, 2017
    37
    33
    0
    MINOR UPDATE3 CalcCheckHash-v1.33-Auto.cmd

    Fixed errors when file name or sub folder has spaces or symbols and added error log when error(s) is(are) found file checking
     
  11. dhjohns

    dhjohns MDL Guru

    Sep 5, 2013
    3,276
    1,731
    120
    Would like it better if you would have an option to paste the path of the file you wish to check or calculate hash.
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  12. NormieLyfe

    NormieLyfe MDL Novice

    May 1, 2017
    37
    33
    0
    Hmmmm... I dunno if it's possible yet but in the mean time, you can copy paste CalcCheckHash to your desired directory and run it from there and it'll calculate and/or check the files and subfolders (if enabled) in that destination
     
  13. NormieLyfe

    NormieLyfe MDL Novice

    May 1, 2017
    37
    33
    0
    #13 NormieLyfe, Nov 22, 2017
    Last edited: Nov 22, 2017
    (OP)
    - UPDATE: CalcCheckHash-v1.34.cmd
    User defined File or Directory, requested by @dhjohns. All hash calculation will be stored within the hashoutput and if new file or directory is detected will automatically change from checking to calculating, if hash calculations are missing from a directory will auto calculate it again.
    Code:
    :Menu
    echo. & echo Place custom File or Directory: & echo.
    set "select=" & set /p select=%=%
    if "%select%"=="" goto Menu
    if exist "%select%\*" (
    if "%select%\" neq "%~dp0" (cd /d "%select%" & set "HashFile=%~dp0%HashFile%" & set "errorLog=%~dp0%errorLog%")
    if /i "%SubDirs%"=="Enabled" (for /r . %%g in (*) do set "p=%%g" & call :Hashing "!p:%select%\=!") else (for /f "delims=" %%g in ('dir /b /a:-d *') do set "p=%%g" & call :Hashing "!p!")
    goto Well
    ) else if exist "%select%" (call :Hashing "%select%" & goto Well)
    goto Menu
    :Well
    echo. & pause & goto Menu
    
     
  14. dhjohns

    dhjohns MDL Guru

    Sep 5, 2013
    3,276
    1,731
    120
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  15. NormieLyfe

    NormieLyfe MDL Novice

    May 1, 2017
    37
    33
    0
    I found out some bugs and updated it a bit, the bug occurs when the directory or file is itself
     
  16. NormieLyfe

    NormieLyfe MDL Novice

    May 1, 2017
    37
    33
    0
    - UPDATE: CalcCheckHash-v1.34-Auto.cmd
    Minor tweaks and Added customizable method for calculating/checking hash (but due note of the compatibility issue with powershell in win 7 compared to win 8/8.1/10)