@echo off
REM File Hash Calculator using certutil
REM Usage: CreateHash.bat [directory] [algorithm] [output_file]
REM   directory: Directory to scan (default: current directory)
REM   algorithm: SHA1, SHA256, SHA384, SHA512, MD5 (default: SHA256)
REM   output_file: Optional CSV file for results

setlocal enabledelayedexpansion

REM Set default values
set "TARGET_DIR=%1"
if "%TARGET_DIR%"=="" set "TARGET_DIR=."

set "ALGORITHM=%2"
if "%ALGORITHM%"=="" set "ALGORITHM=SHA256"

set "OUTPUT_FILE=%3"

REM Validate algorithm
set "VALID_ALGOS=SHA1 SHA256 SHA384 SHA512 MD5"
set "ALGO_VALID=0"
for %%a in (%VALID_ALGOS%) do (
    if /i "%ALGORITHM%"=="%%a" set "ALGO_VALID=1"
)
if "%ALGO_VALID%"=="0" (
    echo ERROR: Invalid algorithm "%ALGORITHM%"
    echo Valid algorithms: %VALID_ALGOS%
    exit /b 1
)

REM Check if directory exists
if not exist "%TARGET_DIR%" (
    echo ERROR: Directory not found: %TARGET_DIR%
    exit /b 1
)

REM Create temporary files
set "TEMP_FILE=%TEMP%\hash_results_%RANDOM%.txt"
set "CSV_TEMP=%TEMP%\csv_results_%RANDOM%.csv"

REM Initialize counters
set "FILE_COUNT=0"
set "PROCESSED_COUNT=0"

echo Scanning directory: %TARGET_DIR%
echo Hash algorithm: %ALGORITHM%
echo.

REM Find all files and count them
for /r "%TARGET_DIR%" %%f in (*) do (
    set /a FILE_COUNT+=1
)
echo Found %FILE_COUNT% files to process

REM Create CSV header if output file specified
if defined OUTPUT_FILE (
    echo FileName,FilePath,FileSize,LastModified,HashAlgorithm,HashValue > "%CSV_TEMP%"
)

REM Process each file
for /r "%TARGET_DIR%" %%f in (*) do (
    set /a PROCESSED_COUNT+=1
    
    REM Get file info
    set "FILENAME=%%~nxf"
    set "FILEPATH=%%f"
    set "FILESIZE=%%~zf"
    set "FILEDATE=%%~tf"
    
    REM Calculate hash using certutil
    certutil -hashfile "%%f" %ALGORITHM% > "%TEMP_FILE%" 2>nul
    
    REM Extract hash value (skip header lines)
    set "HASH_VALUE="
    for /f "skip=1 tokens=*" %%h in (%TEMP_FILE%) do (
        if not defined HASH_VALUE (
            set "HASH_VALUE=%%h"
            set "HASH_VALUE=!HASH_VALUE: =!"
        )
    )
    
    REM Display progress
    set /a "PERCENT=PROCESSED_COUNT * 100 / FILE_COUNT"
    echo [!PROCESSED_COUNT!/%FILE_COUNT%] !FILENAME!: !HASH_VALUE:~0,16!...
    
    REM Add to CSV if output file specified
    if defined OUTPUT_FILE (
        echo "!FILENAME!","!FILEPATH!",!FILESIZE!,"!FILEDATE!",%ALGORITHM%,"!HASH_VALUE!" >> "%CSV_TEMP%"
    )
    
    REM Clean up temp file
    del "%TEMP_FILE%" 2>nul
)

REM Move CSV to final location if specified
if defined OUTPUT_FILE (
    move "%CSV_TEMP%" "%OUTPUT_FILE%" >nul 2>&1
    if exist "%OUTPUT_FILE%" (
        echo.
        echo Results exported to: %OUTPUT_FILE%
    ) else (
        echo ERROR: Failed to create output file
    )
)

REM Create verification file
set "VERIFICATION_FILE=hash_verification_%date:~-4,4%%date:~-7,2%%date:~-10,2%_%time:~0,2%%time:~3,2%%time:~6,2%.txt"
set "VERIFICATION_FILE=!VERIFICATION_FILE: =0!"

echo # File Hash Verification Report > "%VERIFICATION_FILE%"
echo # Generated: %date% %time% >> "%VERIFICATION_FILE%"
echo # Directory: %TARGET_DIR% >> "%VERIFICATION_FILE%"
echo # Algorithm: %ALGORITHM% >> "%VERIFICATION_FILE%"
echo # Total Files: %PROCESSED_COUNT% >> "%VERIFICATION_FILE%"
echo. >> "%VERIFICATION_FILE%"

REM Re-process files for verification file
for /r "%TARGET_DIR%" %%f in (*) do (
    certutil -hashfile "%%f" %ALGORITHM% > "%TEMP_FILE%" 2>nul
    
    set "HASH_VALUE="
    for /f "skip=1 tokens=*" %%h in (%TEMP_FILE%) do (
        if not defined HASH_VALUE (
            set "HASH_VALUE=%%h"
            set "HASH_VALUE=!HASH_VALUE: =!"
        )
    )
    
    echo !HASH_VALUE!  %%f >> "%VERIFICATION_FILE%"
    del "%TEMP_FILE%" 2>nul
)

echo.
echo Hash calculation completed!
echo Total files processed: %PROCESSED_COUNT%
echo Verification file created: %VERIFICATION_FILE%

REM Cleanup
del "%TEMP_FILE%" 2>nul
del "%CSV_TEMP%" 2>nul

endlocal
