Advertisement
relaseed

HASHSUM v1.6

Apr 15th, 2019
182
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Batch 11.63 KB | None | 0 0
  1. @if (@X)==(@Y) @end /* Harmless hybrid line that begins a JScript comment
  2. @goto :Batch
  3.  
  4. ::::
  5. ::::HASHSUM.BAT history
  6. ::::
  7. ::::  v1.6 2019-02-26 - Modify /F and /FR to support non-ASCII characters
  8. ::::  v1.5 2018-02-18 - Added /H, /F, /FR and /NH options.
  9. ::::  v1.4 2016-12-26 - Convert /A value to upper case because some Windows
  10. ::::                    versions are case sensitive. Also improve JScript file
  11. ::::                    read performance by reading 1000000 bytes instead of 1.
  12. ::::  v1.3 2016-12-17 - Bug fixes: Eliminate unwanted \r\n from temp file by
  13. ::::                    reading stdin with JScript instead of FINDSTR.
  14. ::::                    Fix help to ignore history.
  15. ::::  v1.2 2016-12-07 - Bug fixes: Exclude FORFILES directories and
  16. ::::                    correct setlocal/endlocal management in :getOptions
  17. ::::  v1.1 2016-12-06 - New /V option, and minor bug fixes.
  18. ::::  v1.0 2016-12-05 - Original release
  19. :::
  20. :::HASHSUM  [/Option [Value]]... [File]...
  21. :::
  22. :::  Print or check file hashes using any of the following standard
  23. :::  hash algorithms: MD5, SHA1, SHA256, SHA384, or SHA512.
  24. :::
  25. :::  HASHSUM always does a binary read - \r\n is never converted to \n.
  26. :::
  27. :::  In the absence of /C, HASHSUM computes the hash for each File, and writes
  28. :::  a manifest of the results. Each line of output consists of the hash value,
  29. :::  followed by a space and an asterisk, followed by the File name. The default
  30. :::  hash alogrithm is sha256. File may include wildcards, but must not contain
  31. :::  any path information.
  32. :::
  33. :::  If File is not given, then read from standard input and write the hash
  34. :::  value only, without the trailing space, asterisk, or file name.
  35. :::
  36. :::  Options:
  37. :::
  38. :::    /? - Prints this help information to standard output.
  39. :::
  40. :::    /?? - Prints paged help using MORE.
  41. :::
  42. :::    /V - Prints the HASHSUM.BAT version.
  43. :::
  44. :::    /H - Prints the HASHSUM.BAT history.
  45. :::
  46. :::    /A Algorithm
  47. :::
  48. :::         Specifies one of the following hash algorithms:
  49. :::         MD5, SHA1, SHA256, SHA384, SHA512
  50. :::
  51. :::    /P RootPath
  52. :::
  53. :::         Specifies the root path for operations.
  54. :::         The default is the current directory.
  55. :::
  56. :::    /S - Recurse into all Subdirectories. The relative path from the root
  57. :::         is included in the file name output.
  58. :::         This option is ignored if used with /C.
  59. :::
  60. :::    /I - Include the RootPath in the file name output.
  61. :::         This option is ignored if used with /C.
  62. :::
  63. :::    /T - Writes a space before each file name, rather than an
  64. :::         asterisk. However, files are still read in binary mode.
  65. :::         This option is ignored if used with /C.
  66. :::
  67. :::    /C - Read hash values and file names from File (the manifest), and verify
  68. :::         that local files match. File may include path information with /C.
  69. :::
  70. :::         If File is not given, then read hash and file names from standard
  71. :::         input. Each line of input must have a hash, followed by two spaces,
  72. :::         or a space and an asterisk, followed by a file name.
  73. :::
  74. :::         If /A is not specified, then the algorithm is determined by the
  75. :::         File extension. If the extension is not a valid algorithm, then
  76. :::         the algorithm is derived based on the length of the first hash
  77. :::         within File.
  78. :::
  79. :::         Returns ERRORLEVEL 1 if any manifest File is not found or is invalid,
  80. :::         or if any local file is missing or does not match the hash value in
  81. :::         the manifest. If all files are found and match, then returns 0.
  82. :::
  83. :::    /F FileName
  84. :::
  85. :::         When using /C, only check lines within the manifest that contain the
  86. :::         string FileName. The search ignores case.
  87. :::
  88. :::    /FR FileRegEx
  89. :::
  90. :::         When using /C, only check lines within the manifest that match the
  91. :::         FINDSTR regular expression FileRegEx. The search ignores case.
  92. :::
  93. :::    /NH - (No Headers)  Suppresses listing of manifest name(s) when using /C.
  94. :::
  95. :::    /NE - (No Errors) Suppresses error messages when using /C.
  96. :::
  97. :::    /NM - (No Matches) Suppresses listing of matching files when using /C.
  98. :::
  99. :::    /NS - (No Summary) Suppresses summary information when using /C.
  100. :::
  101. :::    /Q  - (Quiet) Suppresses all output when using /C.
  102. :::
  103. :::HASHSUM.BAT version 1.6 was written by Dave Benham
  104. :::maintained at http://www.dostips.com/forum/viewtopic.php?f=3&t=7592
  105.  
  106. ============= :Batch portion ===========
  107. @echo off
  108. setlocal disableDelayedExpansion
  109.  
  110. :: Define options
  111. set "options= /A:"" /C: /I: /P:"" /S: /T: /?: /??: /NH: /NE: /NM: /NS: /Q: /V: /H: /F:"" /FR:"" "
  112.  
  113. :: Set default option values
  114. for %%O in (%options%) do for /f "tokens=1,* delims=:" %%A in ("%%O") do set "%%A=%%~B"
  115. set "/?="
  116. set "/??="
  117.  
  118. :getOptions
  119. if not "%~1"=="" (
  120.   set "test=%~1"
  121.   setlocal enableDelayedExpansion
  122.   if "!test:~0,1!" neq "/" endlocal & goto :endOptions
  123.   set "test=!options:*%~1:=! "
  124.   if "!test!"=="!options! " (
  125.       endlocal
  126.       >&2 echo Invalid option %~1
  127.       exit /b 1
  128.   ) else if "!test:~0,1!"==" " (
  129.       endlocal
  130.       set "%~1=1"
  131.   ) else (
  132.       endlocal
  133.       set "%~1=%~2"
  134.       shift /1
  135.   )
  136.   shift /1
  137.   goto :getOptions
  138. )
  139. :endOptions
  140.  
  141. :: Display paged help
  142. if defined /?? (
  143.   (for /f "delims=: tokens=*" %%A in ('findstr "^:::[^:] ^:::$" "%~f0"') do @echo(%%A)|more /e
  144.   exit /b 0
  145. ) 2>nul
  146.  
  147. :: Display help
  148. if defined /? (
  149.   for /f "delims=: tokens=*" %%A in ('findstr "^:::[^:] ^:::$" "%~f0"') do echo(%%A
  150.   exit /b 0
  151. )
  152.  
  153. :: Display version
  154. if defined /V (
  155.   for /f "delims=: tokens=*" %%A in ('findstr /ric:"^:::%~nx0 version" "%~f0"') do echo(%%A
  156.   exit /b 0
  157. )
  158.  
  159. :: Display history
  160. if defined /H (
  161.   for /f "delims=: tokens=*" %%A in ('findstr "^::::" "%~f0"') do echo(%%A
  162.   exit /b 0
  163. )
  164.  
  165. :: If no file specified, then read stdin and write to a temp file
  166. set "tempFile="
  167. if "%~1" equ "" set "tempFile=%~nx0.%time::=_%.%random%.tmp"
  168. if defined tempFile cscript //nologo //E:JScript "%~f0" "%temp%\%tempFile%"
  169.  
  170. if defined /P cd /d "%/P%" || exit /b 1
  171. if defined /C goto :check
  172.  
  173. :generate
  174. if defined tempFile cd /d "%temp%"
  175. if not defined /A set "/A=sha256"
  176. if defined /S set "/S=/s"
  177. if defined /T (set "/T= ") else set "/T=*"
  178. call :defineEmpty
  179. if not defined /P goto :generateLoop
  180. if not defined /I goto :generateLoop
  181. if "%/P:~-1%" equ "\" (set "/I=%/P:\=/%") else set "/I=%/P:\=/%/"
  182. set "rtn=0"
  183.  
  184. :generateLoop
  185. (
  186.   for /f "delims=" %%F in (
  187.     'forfiles %/s% /m "%tempFile%%~1" /c "cmd /c if @isdir==FALSE echo @relpath" 2^>nul'
  188.   ) do for /f "delims=" %%A in (
  189.     'certutil.exe -hashfile %%F %/A% ^| find /v ":" ^|^| if %%~zF gtr 0 (echo X^) else echo %empty%'
  190.   ) do (
  191.     set "file=%%~F"
  192.     set "hash=%%A"
  193.     setlocal enableDelayedExpansion
  194.     set "file=!file:~2!"
  195.     if defined tempFile (
  196.       if !hash! equ X (
  197.         set "rtn=1"
  198.         echo ERROR
  199.       ) else echo !hash: =!
  200.     ) else (
  201.       if !hash! equ X (
  202.         set "rtn=1"
  203.         echo ERROR: !/I!!file!
  204.       ) else echo !hash: =! !/T!!/I!!file:\=/!
  205.     )
  206.     endlocal
  207.   )
  208. ) || (
  209.   set "rtn=1"
  210.   echo MISSING: %/T%%1
  211. )
  212. shift /1
  213. if "%~1" neq "" goto :generateLoop
  214. if defined tempFile del "%tempFile%"
  215. exit /b %rtn%
  216.  
  217. :check
  218. if defined /Q for %%V in (/NE /NM /NS /NH) do set "%%V=1"
  219. if defined /F if defined /FR (
  220.   >&2 echo ERROR: /F and /FR cannot be combined
  221.   exit /b 1
  222. )
  223. set "searchTemp="
  224. if defined /F (
  225.   set "searchTemp=%temp%\%~nx0.%time::=_%.%random%.search.tmp"
  226.   setlocal enableDelayedExpansion
  227.   (echo(!/F!) > "!%searchTemp!"
  228.   endlocal
  229.   set "file="    & set "freg=rem" & set "norm=rem"
  230. ) else if defined /FR (
  231.   set "searchTemp=%temp%\%~nx0.%time::=_%.%random%.search.tmp"
  232.   setlocal enableDelayedExpansion
  233.   (echo(!/FR!) > "!%searchTemp!"
  234.   endlocal
  235.   set "file=rem" & set "freg="    & set "norm=rem"
  236. ) else (
  237.   set "file=rem" & set "freg=rem" & set "norm="
  238. )
  239. set /a manifestCnt=missingManifestCnt=invalidCnt=missingCnt=failCnt=okCnt=0
  240.  
  241. :checkLoop
  242. set "alogorithm=%/A%"
  243. if defined tempFile set "tempFile=%temp%\%tempFile%"
  244. for %%F in ("%tempFile%%~1") do call :checkFile "%%~F"
  245. if defined tempFile del "%tempFile%"
  246. shift /1
  247. if "%~1" neq "" goto :checkLoop
  248.  
  249. if defined searchTemp del "%searchTemp%"
  250.  
  251. if not defined /NS (
  252.   echo ==========  SUMMARY  ==========
  253.   echo Total manifests   = %manifestCnt%
  254.   echo Matched files     = %okCnt%
  255.   echo(
  256.   if %missingManifestCnt% gtr 0 echo Missing manifests = %missingManifestCnt%
  257.   if %invalidCnt% gtr 0         echo Invalid manifests = %invalidCnt%
  258.   if %missingCnt% gtr 0         echo Missing files     = %missingCnt%
  259.   if %failCnt% gtr 0            echo Failed files      = %failCnt%
  260. )
  261. set /a "1/(missingManifestCnt+invalidCnt+missingCnt+failCnt)" 2>nul && (
  262.   echo(
  263.   exit /b 1
  264. )
  265. exit /b 0
  266.  
  267. :checkFile
  268. set /a manifestCnt+=1
  269. if not defined /NH if defined tempfile (echo ----------  ^<stdin^>  ----------) else echo ----------  %1  ----------
  270. if not defined algorithm set "/A="
  271. if not defined /A echo *.md5*.sha1*.sha256*.sha384*.sha512*|find /i "*%~x1*" >nul && for /f "delims=." %%A in ("%~x1") do set "/A=%%A"
  272. findstr /virc:"^[0123456789abcdef][0123456789abcdef]* [ *][^ *?|<>]" %1 >nul 2>nul && (
  273.   if not defined /NE if defined tempFile (echo *INVALID: ^<stdin^>) else echo *INVALID: %1
  274.   set /a invalidCnt+=1
  275.   exit /b
  276. )
  277. (
  278.   %norm% for /f "usebackq tokens=1* delims=* " %%A in (%1) do (
  279.   %file% for /f "tokens=1* delims=* " %%A in ('type %1 ^| findstr /ilg:"%searchTemp%"') do (
  280.   %freg% for /f "tokens=1* delims=* " %%A in ('type %1 ^| findstr /irg:"%searchTemp%"') do (
  281.     set "hash0=%%A"
  282.     set "fileName=%%B"
  283.     if defined /A (call :defineEmpty) else call :determineFormat
  284.     setlocal enableDelayedExpansion
  285.     set "fileName=!fileName:/=\!"
  286.     for /f "tokens=1* delims=" %%C in (
  287.       'certutil.exe -hashfile "!fileName!" !/A! ^| find /v ":" ^|^| if exist "!fileName!" (echo !empty!^) else echo X'
  288.     ) do set "hash=%%C"
  289.     if /i "!hash0!" equ "!hash: =!" (
  290.       if not defined /NM echo OK: !fileName!
  291.       endlocal
  292.       set /a okCnt+=1
  293.     ) else if !hash! equ X (
  294.       if not defined /NE echo *MISSING: !fileName!
  295.       endlocal
  296.       set /a missingCnt+=1
  297.     ) else (
  298.       if not defined /NE echo *FAILED: !fileName!
  299.       endlocal
  300.       set /a failCnt+=1
  301.     )
  302.   )
  303. ) 2>nul || if not defined /F if not defined /FR (
  304.   if not defined /NE echo *MISSING: %1
  305.   set /a missingManifestCnt+=1
  306. )
  307. exit /b
  308.  
  309. :determineFormat
  310. if "%hash0:~127%" neq "" (
  311.   set "/A=SHA512"
  312. ) else if "%hash0:~95%" neq "" (
  313.   set "/A=SHA384"
  314. ) else if "%hash0:~63%" neq "" (
  315.   set "/A=SHA256"
  316. ) else if "%hash0:~39%" neq "" (
  317.   set "/A=SHA1"
  318. ) else set "/A=MD5"
  319.  
  320. :defineEmpty
  321. if /i "%/A%"=="md5" (
  322.   set "empty=d41d8cd98f00b204e9800998ecf8427e"
  323.   set "/A=MD5"
  324. ) else if /i "%/A%"=="sha1" (
  325.   set "empty=da39a3ee5e6b4b0d3255bfef95601890afd80709"
  326.   set "/A=SHA1"
  327. ) else if /i "%/A%"=="sha256" (
  328.   set "empty=e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"
  329.   set "/A=SHA256"
  330. ) else if /i "%/A%"=="sha384" (
  331.   set "empty=38b060a751ac96384cd9327eb1b1e36a21fdb71114be07434c0cc7bf63f6e1da274edebfe76f65fbd51ad2f14898b95b"
  332.   set "/A=SHA384"
  333. ) else if /i "%/A%"=="sha512" (
  334.   set "empty=cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e"
  335.   set "/A=SHA512"
  336. ) else (
  337.   echo ERROR: Invalid /A algorithm>&2
  338.   (goto) 2>nul&exit /b 1
  339. )
  340. exit /b
  341.  
  342.  
  343. ************* JScript portion **********/
  344. var fso = new ActiveXObject("Scripting.FileSystemObject");
  345. var out = fso.OpenTextFile(WScript.Arguments(0),2,true);
  346. var chr;
  347. while( !WScript.StdIn.AtEndOfStream ) {
  348.   chr=WScript.StdIn.Read(1000000);
  349.   out.Write(chr);
  350. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement