Jump to content

Change md5 hash of a file using php


siddscool19

Recommended Posts

  • 1 month later...

I know this post is a bit old, but it's one of the first that pops up and I think it's a fair question that needs an answer. :)

 

It's easy to do it via shell, so if you have access that way, do it:

<?php
$file = "./file.zip";
echo "MD5 before: ".md5_file($file)."<br />\n";
system("dd if=/dev/zero ibs=1 count=1 >> ".$file);
echo "MD5 after: ".md5_file($file)."<br />\n";
?>

 

But it's also possible to do it via "real PHP":

<?php
$filename = "./file.zip";

$file = fopen($filename, "a");
fwrite($file,"\n0");
fclose($file);
?>

Link to comment
Share on other sites

Because you're modifying the file, that won't work for plain text files:

 

daniel@daniel0:~$ cat > test.php

<?php

echo 'Hello World' . PHP_EOL;

daniel@daniel0:~$ php test.php

Hello World

daniel@daniel0:~$ dd if=/dev/zero ibs=1 count=1 >> test.php

1+0 records in

0+1 records out

1 byte (1 B) copied, 2,1655e-05 s, 46,2 kB/s

daniel@daniel0:~$ php test.php

Warning: Unexpected character in input:  ' in /home/daniel/test.php on line 3

Hello World

daniel@daniel0:~$

 

You modify it by editing the file. You could probably append a newline, that shouldn't give any problems. Do note that it's technically speaking not the same file anymore though, hence the reason why you'll get a different hash.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.