Reputation: 61
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!
Upvotes: 4
Views: 2585
Reputation: 622
The return value will change as long as your $data variable changes, you can't get same hashing value from different strings
Upvotes: 0
Reputation: 4370
Two different input with same md5
or sha1
output?
That's possible but way to hard. Take a look at here: https://crypto.stackexchange.com/questions/1434/are-there-two-known-strings-which-have-the-same-md5-hash-value
Upvotes: 0
Reputation: 4601
Hashing method - to give the same value, but not easy to predict or decode is what i think you are looking for.
You can use use a constant string val and do a hash of that string to get the same value always, if you want to change the value you can change the constant and get a different hash value
$constStr = "hashThis";
$hashWord = md5($constStr);
// it will return the same value always, as long as the constStr is the same
Upvotes: 0
Reputation: 61875
Hashing function are deterministic (they would be useless if this was not the case).
In computer science, a deterministic algorithm is an algorithm which, given a particular input, will always produce the same output, with the underlying machine always passing through the same sequence of states.
Consider another (eg. time) input to the domain as in sha1(microtime() . $data)
if you want 'different output'. I'm not sure how useful this will be in practice.
Password hash functions use a salt (randomly generated, stored separately) as additional input so the same plain-text password will result in a different stored hash value.
Upvotes: 0
Reputation: 1774
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
Upvotes: 3