Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP hashing method, same output every time

Tags:

php

hash

So if I do something like sha1($data) the result will be BLAHBLAH123. However if I do it again it will be BLAHAHS316. The same thing happens with md5. So my question is, what is a consistent way to hash values?

So like function($data) will return BLAHBLAHBLAH123 each time it is evaluated with the same $data parameter.

EDIT: I have a specific purpose in mind for this that isn't passwords so security isn't a concern.

EDIT: For example, md5($data) will not return BLAHBLAH every time, sometimes it'll return BLAHHHAL. I don't want that. I want it to return the same thing, BLAHBLAH everytime!

like image 991
MrDummu Avatar asked Sep 13 '25 02:09

MrDummu


1 Answers

The output of a hashing operation will only change if the input has changed.

For example:

echo sha1( 'test' );

a94a8fe5ccb19ba61c4c0873d391e987982fbbd3

If you wish it to change everytime, you could append a timestamp to the input:

echo sha1( 'test'.time() )

3d68b7693768f199623f31f820b1ba29b0a58769
like image 157
pcnate Avatar answered Sep 14 '25 19:09

pcnate