Is it possible to generate 2 identical sequences with md5 if the same base is used?

Asked

Viewed 251 times

1

I was creating a project with multiple image uploads and I wanted them all to be together, just separated by "," e.g.: image.jpg,imagem2.jpg...

$imagens_name = "";
processo de renomear.. Ele gerava um novo nome com md5 date()

However it sometimes worked sends 2 images separated by , with different name, but other times sent the same image with the same name, and when inserted more than 2 images it generated the first two equal and 3 different. So I switched md5 date to "system('date +%s%N');" and now it’s working normally.

My question is whether the script ended up generating the same sequences in 2 images because the date was the same (since when I switched to microtime it worked), or whether it is impossible to generate two identical sequences with md5 even if the base (date) is the same ?

  • md5 is "static" and that’s why it exists. If H(x) = y, if the same x will always be inserted y, soon "two equal dates" will have exactly the same final hash. So not only is it "possible" but it is the normal and expected behavior of it, it allows checking basic integrity and it is only for this that it serves.

  • https://answall.com/questions/170920/gerando-identifier-%C3%Banico-using-time

  • https://answall.com/questions/96520/gerar-seriais-verifies%C3%A7%C3%A3o-unicos

1 answer

1


The MD5 string will always be the same for the same base string.

Example:
1 -> c4ca4238a0b923820dcc509a6f75849b
abc -> 900150983cd24fb0d6963f7d28e17f72

As described in the question, the logic was inconsistent because using the current date may imply conflict of duplicate names as a loop normally runs the processes in milliseconds.

The function microtime() can also return the same value within that context because if the iteration is too fast, it can return value equal to a previous value.

It is more consistent to ensure unique values by concatenating something more secure.

for($n = 1; $n < 10; $n++) {
    echo date('YmdHis').PHP_EOL;
}

//resultado:
20170518061514
20170518061514
20170518061514
20170518061514
20170518061514
20170518061514
20170518061514
20170518061514
20170518061514

This is how you tried to do it. Note that 10 iterations were executed in less than 1 second, so the values repeat.

One idea to make each value unique is to add some unique identifier.

/*
Nesse exemplo é concatenado o número da iteração corrente.
*/
for($n = 1; $n < 10; $n++) {
    echo date('YmdHis').':'.$n.PHP_EOL;
}

/*
Resultado
*/

20170518061756:1
20170518061756:2
20170518061756:3
20170518061756:4
20170518061756:5
20170518061756:6
20170518061756:7
20170518061756:8
20170518061756:9

See how it is implementing with MD5:

for($n = 1; $n < 10; $n++) {
    echo md5(date('YmdHis').':'.$n).PHP_EOL;
}

475531f945b1fe26a4849ab4f5474fd7
4e0fc04b52d05a86688892dd0ad92552
a58d7865693971aec8f6867de725dc0a
7f9d7c97091b11024295eed5641c99de
2e826491e8b9da0a005777f78ba9550b
ff8ab9eb829a36de0410bb9773a6315e
52e0220c76d2a18a210bfadb29158c39
659ec7a31ae61abd7022b6e21fb008e2
88da547523beb6d5f4f7c2a6aaa79037

Of course, you should be aware that MD5 can produce collisions even with different strings. But this is something hardly or almost impossible for the purpose of the context of the question.

To ensure greater integrity, record data without encrypting. It will take up less space and be collision-free even if it is rare for this case.

If it is important not to save the numeric format of the dates, you can mask with "short id". These are short Ids like Youtube or URL shortening services. But this is out of the question.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.