3

I need to create an windows "batch" file with following conditions. Convert the text to md5sum value I tried with below .bat file. But not working

@echo off

setlocal ENABLEDELAYEDEXPANSION

set hashmd5=$(echo -n welcome1 | md5sum | cut -c 1-32);

printpass=$printpass"#####MD5: echo.%%hashmd5 "\n"
PoX
  • 1,229
  • 19
  • 32
munna
  • 31
  • 1
  • 2
  • 1
    You might find [this answer](http://stackoverflow.com/a/29216039/1683264) useful. – rojo Feb 03 '16 at 14:45

2 Answers2

3

Here's another version, also using certutil to generate the MD5 hash. It'll give you the MD5sum of a file or of a string argument, and can accept one line of input via the pipeline. This solution doesn't depend on an accompanying helper script.

@echo off & setlocal

set "plaintext=%~1"
set "file=%temp%\%~n0.tmp"
set md5=

if "%~1"=="/?" (
    echo Usage: %~nx0 file^|string
    echo    or: command ^| %~nx0
    echo;
    echo Example: set /P "=password" ^<NUL ^| %~nx0
    goto :EOF
)

if not defined plaintext set /P "plaintext="

if exist "%plaintext%" (
    set "file=%plaintext%"
) else for %%I in ("%file%") do if %%~zI equ 0 (
    <NUL >"%file%" set /P "=%plaintext%"
)

for /f "skip=1 delims=" %%I in ('certutil -hashfile "%file%" MD5') do (
    if not defined md5 set "md5=%%I"
)

2>NUL del "%temp%\%~n0.tmp"

echo %md5: =%

Just because I felt like the challenge, here's yet another version that can accept multiple lines via the pipeline, as well as input from file redirection. (It wasn't easy preventing StdIn.AtEndOfStream from prompting for keyboard input when the buffer was empty.)

@if (@CodeSection == @Batch) @then
@echo off & setlocal & goto main

:usage
echo Usage: %~nx0 file^|string
echo    or: command ^| %~nx0
echo    or: %~nx0 ^< file
echo;
echo Example: set /P "=password" ^<NUL ^| %~nx0
goto :EOF

:main
set "plaintext=%~1"
set "file=%temp%\%~n0.tmp"
set md5=

rem // check for input via the pipeline; timeout nearly instantly if none
start /b "" cscript /nologo /e:JScript "%~f0" "%file%" watcher
cscript /nologo /e:JScript "%~f0" "%file%"

if "%~1"=="" for %%I in ("%file%") do if %%~zI equ 0 goto usage
if "%~1"=="/?" goto usage

if not defined plaintext set /P "plaintext="

if exist "%plaintext%" (
    set "file=%plaintext%"
) else for %%I in ("%file%") do if %%~zI equ 0 (
    <NUL >"%file%" set /P "=%plaintext%"
)

for /f "skip=1 delims=" %%I in ('certutil -hashfile "%file%" MD5') do (
    if not defined md5 set "md5=%%I"
)

2>NUL del "%temp%\%~n0.tmp"

echo %md5: =%
goto :EOF

@end // end Batch / begin JScript hybrid code

var fso = WSH.CreateObject('scripting.filesystemobject');
if (WSH.Arguments.Length > 1) {
    WSH.Sleep(1);
    if (!fso.FileExists(WSH.Arguments(0)))
        WSH.CreateObject('WScript.Shell').Exec('taskkill /im "cscript.exe" /f');
} else if (!WSH.StdIn.AtEndOfStream) {
    var file = fso.CreateTextFile(WSH.Arguments(0), 1);
    file.Write(WSH.StdIn.ReadAll());
    file.Close();
}
rojo
  • 24,000
  • 5
  • 55
  • 101
  • 1
    Nice one, thanks. Just to note, to get your first example to work I had to make two small changes: 1. line 11: `echo Example: set /P "=password" ^ – Eric G Dec 22 '16 at 23:19
1

check MD5.bat:

@echo off 
set "string=The quick brown fox jumps over the lazy dog"
(break|set/p=%string%)>~
call md5 ~ m5sting
echo %m5sting%
exit /b 0

Though whatever I do I cant get the same result as from some online tools that convert string to MD5 (but often they differ from each other). This will always put an EOF character at the end of the string as it converts a file but not a pure string.

At the moment the script above gives the same result as (seen in rojo's comment) powershell "[Security.Cryptography.HashAlgorithm]::Create('MD5').ComputeHash([Text.Encoding]::UTF8.GetBytes('%string%')) | %%{write-host -n $_.tostring('x2')}" though still differs from PHP and some Javascript libraries

npocmaka
  • 55,367
  • 18
  • 148
  • 187