"Random" number being generated along with md5 hash

Asked

Viewed 105 times

0

I am developing an application in Laravel and at a certain moment I need to generate a value in MD5 to assign to a field in the database.

The problem is that when generating this hash of MD5, a "random number" is appearing and is entering the variable, instead of entering the variable the hash created. Behold:

 if($this->emptyField === 0)
 {
   
     //Criando um id do lote
     $fileId = md5(time() . rand(0, 9999) . time());
     print_r($fileId);
 }

See what is generated:

inserir a descrição da imagem aqui

If you notice there, 4 numbers (9634) are generated along with the MD5 hash. When you enter the database, only these 4 numbers enter the column.

Sometimes it happens to only generate the MD5 hash even.

The field in the database is varchar(100). And the database is Mysql.

Could you help me ?

Follow the full code link if you want to consult.

https://paste.laravel.io/2d6d0c04-82db-476b-b1e6-b37b5f792e68

  • How the conversation went on was moved to the chat and you can continue there, if you are interested in reading and/or participating, you can click on the link. About the closure, as what was presented is expected behavior, nothing to be done in the scope of the site. In these cases, you need to provide a [mcve] of a real problem that does not depend on external links. In the absence of these possibilities, the question should not be asked on this site.

1 answer

2


Dear, The hash generated by md5 contains 32 characters, your output in pewview is correct, there is nothing wrong. to check, check the Rand in a separate variable, then join it with the time() in a separate string, print both before generating the md5, generate the md5 and then re-generate with the previous outputs on a website that provides md5 for conference purposes, and you’ll see it’s all right. Thus:

//Criando um id do lote
       $father = rand(0, 9999);
       print_r($father);
       echo "<br>";
       $pre_print = (time() . $father . time());
       print_r($pre_print);
       echo "<bR>";
       $fileId = md5($pre_print);
       print_r($fileId);

You can count the characters in: https://www.quantasletras.com/ And generate md5 in: https://www.md5hashgenerator.com/

  • I followed the hkotsubo tip and stopped to analyze the code using the premise of this code ai. Really those numbers are part of the hash. I’ve really gone soft. Thank you!

  • "This problem cannot be reproduced, or is a typo. Even within the scope of the site, your solution would hardly be useful to other users in the future." This is the argument they gave to close the question. Beauty. But two users were able to reproduce the code I posted and had results (no matter if positive or negative). And another thing, what guarantees that a person can’t be going through the same problem as me ? Actually this question was good, because, throughout the site, there is no other demonstrating a similar problem (which is difficult to happen).

  • ... A person, who is going through a problem like mine can look at Hkotsubo’s answer and that answer here, and perhaps have the solution to their possible doubt. The question was "positive" twice and marked "favorite" by a person. It means to someone it may have been useful.

Browser other questions tagged

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