How to change MD5 of a file

26,933

Solution 1

md5sum calculates and verifies 128-bit MD5 hashes. The MD5 hash functions as a compact digital fingerprint of a file. It is very unlikely that any two non-identical files in the real world will have the same MD5 hash, unless they have been specifically created to have the same hash.

md5sum is used to verify the integrity of files, as virtually any change to a file will cause its MD5 hash to change. Most commonly, md5sum is used to verify that a file has not changed as a result of a faulty file transfer, a disk error or non-malicious meddling.

For more see Wikipedia

Could it be changed?

No. You can not change md5sum of a file as long as the contents of the files are same. And that is the sole purpose of it. You can change the md5sum value of a file by making any change in its content only.

Solution 2

md5sum calculates MD5 hash of file contents. MD5 algorithm doesn't use any randomness (it's deterministic). It's basically a list of mathematical instructions to perform on the input (file contents). Every time you provide it with the same input, it processes it in exactly the same way and you get the same output. Just like maths: 3×7 will always yield 21, no matter how many times you try.

The only way to change output is to change the input.

Solution 3

While I agree with @sauravc, there is a way to do it if you don’t mind the possibility of corrupting the file.

If you change the file in any way, you can recalculate the MD5.

You can potentially change a file by opening it in your preferred editor, making an addition or subtraction, then saving it again.

If you want a quick way to do this via command line, you can use either dd or truncate like this:

dd if=/dev/zero bs=1 count=10 >> <yourfile>.<ext>

or

truncate -s +10 <yourfile>.<ext>

Either command should add 10 bytes to the end of your file. This should mean the MD5 (when next calculated) should be different.

Beware

This has the potential to corrupt your files, and should be tested thoroughly first.

Share:
26,933

Related videos on Youtube

Rong Nguyen
Author by

Rong Nguyen

My favorite technologies: ERP: Apache OFBiz(Sales, Purchasing, Accounting, HRM, Inventory, POS, CRM) Telco Application: core network, roaming Java Database: Oracle, Postgresql JSF/Primefaces Docker, Apache Mesos, Flocker, K8S Integration, OSGi and Messaging: Apache Camel, Apache Karaf and Apache ActiveMq GNU/Linux Git My profile: http://careers.stackoverflow.com/rongnk Now, I am a freelancer, feel free to contact me if you want :) Email: [email protected] Website: https://iamsoftware.com.vn

Updated on September 18, 2022

Comments

  • Rong Nguyen
    Rong Nguyen almost 2 years

    We can get MD5 of a file by using md5sum so my questions are:

    1. Could the MD5 be changed?
    2. How to change the MD5?
  • Rong Nguyen
    Rong Nguyen over 7 years
    Thank you. But I found a tool(on window) and it can change MD5 of a file.
  • sourav c.
    sourav c. over 7 years
    You are welcome. But surely it is changing any file may be slightly. You never know the file will work as before or not. In the process there is always a chance of file corruption.
  • Tschallacka
    Tschallacka over 7 years
    just pad a few null bytes at the end of the file
  • Bakuriu
    Bakuriu over 7 years
    @Tschallacka If the file is a script this would probably raise a syntax error, rendering the script useless..
  • David Foerster
    David Foerster over 7 years
    @Tschallacka: Whether that works depends entirely on the file format and/or the leniency of its decoder. Many binary formats ignore trailing null bytes, many others don't. Text-based file formats will probably stop working if you append null bytes.
  • leftaroundabout
    leftaroundabout over 7 years
    @DavidFoerster actually I'd expect many programs to ignore anything after a null byte in a text input, on account of using standard C strings internally (which are null-terminated).
  • David Foerster
    David Foerster over 7 years
    @leftaroundabout: Sure, many do exactly that. Many others don't.