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);
?>

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.