-1

I have 3 rar files (all with the same md5), and I use this code to change Md5:

@echo off
setlocal enabledelayedexpansion
 set /A Counter=0


FOR %%1 in (*.rar) do (

        set /A Counter= !Counter! + 1
                COPY  %%1+!Counter!
                echo !Counter!

)

pause

When I run the .bat file, it shows 3 row with value: 1, 2, 3 but when I check md5 of 3 rar files, they is same although they were changed. Why ? What wrong I did? Thanks !

  • I don't think you are changing anything, as you forgot to add a destination file in the copy command, as written here. – Doktoro Reichard May 3 '14 at 16:15
  • @DoktoroReichard That's not the actual issue: "When you combine files, [and] you omit Destination, the files are combined and stored under the name of the first file in the list." Source – and31415 May 7 '14 at 11:02
3

Most likely, the files '.\1', '.\2' and '.\3' do not exist. The copy command copy archive.rar+1 appends the content of file '1' to the .rar file. If the file does not exist, it is treated as if it were empty. The hash changes nonetheless because without the '/b' flag, the command also appends the character 0x1A. Since all files found with (*.rar) were the same to begin with, adding the same bytes to every one will result in three exact same .rar files with the exact same MD5.

You can make sure the MD5 values are different after running the script by creating the files '.\1', '.\2' etc. with different contents. If you simply want to append the characters, you may want to use something like echo !Counter! >> %%1 instead. The resulting files will not be valid rar archives anymore, of course. For that, you will need a rar tool with a command line interface.

  • Actually, the user doesn’t say that the three files are identical, only that they have the same MD5 hash. Of course, if he has a way of generating non-identical files with the same MD5 hash, he could probably get that published. – Scott May 3 '14 at 18:21
1

Your batch script has some issues. What you're trying to do is to append a number to the end of the original files in order to change their MD5 hashes. However:

  • The plus sign (+) can only be used to combine multiple existing files.
  • You're dealing with binary files, so you need to use the /b parameter.

    /b directs the command interpreter to read the number of bytes specified by the file size in the directory. /b is the default value for copy, unless copy combines files.

    When /b precedes or follows a list of files on the command line, it applies to all listed files until copy encounters /a.

    Source: Copy

Basically you need to store the number you want to append in a temporary file, and then combine it with the original .rar archive. Here's a fixed, working version:

@echo off
setlocal
setlocal enabledelayedexpansion

set tempFile=%temp%\%random%%random%.tmp
set /a counter=0

for %%G in (*.rar) do (
set /a counter += 1
<nul set /p=!counter!>"%tempFile%"
copy /b "%%~G"+"%tempFile%" >nul
echo !counter!
)

del "%tempFile%" 2>nul    
pause

endlocal & exit /b

Note Replace for with for /r if you want to recursively scan folders.

Further reading

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.