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

1

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

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

However, sometimes it works, it sends 2 images separated by a different name, but at other times it sends the same image with the same name, and when it inserted more than 2 images, it generated the first two and three different ones. So I changed md5 date to "system ('date +% s% N');" and now it's working normally.

My question is if 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 if it is impossible to generate two equal sequences with md5 even though the base ) is the same?

    
asked by anonymous 17.05.2017 / 22:55

1 answer

1

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

Example: 1 - > c4ca4238a0b923820dcc509a6f75849b abc - > 900150983cd24fb0d6963f7d28e17f72

As you described in the question, the logic was inconsistent since using the current date might imply a duplicate name conflict because a loop repetition usually runs the processes in milliseconds.

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

It's more consistent to guarantee 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

That's how you tried to do it. Note that 10 iterations were performed in less than 1 second, so the values repeat.

An 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 you're deploying 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 that is something hardly or almost impossible for the purpose of the query context.

To ensure greater integrity, write the data unencrypted. It will take up less space and be free of collisions even though they are rare for this case.

If it is important not to save the numeric date format, you can mask it with "short id". These are short YouTube-like IDs or URL shortening services. But that's out of the question's focus.

    
17.05.2017 / 23:28