Script keeps outputing different hashes for same password!

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
cap2cap10
Forum Contributor
Posts: 158
Joined: Mon Apr 14, 2008 11:06 pm

Script keeps outputing different hashes for same password!

Post by cap2cap10 »

Greetings PHP technorati. I hope all is well with everyone. I seek guidance on a code that keeps outputing different hashes for the same password input. Can someone show me how to edit code so that it will produce the same hash for the same password input? thanks in advance.

Code: Select all


function myObscurepass($userpassword, $saltHash=NULL, $mode='sha1'){
        // hash the text //
        $textHash = hash($mode, $userpassword);
        // set where salt will appear in hash //
        $saltStart = strlen($userpassword);
        // if no salt given create random one //
        if($saltHash == NULL) {
            $saltHash = hash($mode, uniqid(rand(), true));
        }
        // add salt into text hash at pass length position and hash it //
        if($saltStart > 0 && $saltStart < strlen($saltHash)) {
            $textHashStart = substr($textHash,0,$saltStart);
            $textHashEnd = substr($textHash,$saltStart,strlen($saltHash));
            $outHash = hash($mode, $textHashEnd.$saltHash.$textHashStart);
        } elseif($saltStart > (strlen($saltHash)-1)) {
            $outHash = hash($mode, $textHash.$saltHash);
        } else {
            $outHash = hash($mode, $saltHash.$textHash);
        }
        // put salt at front of hash //
        $output = $saltHash.$outHash;
        return $output;
    }
Sincerely,

Batoe
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Script keeps outputing different hashes for same passwor

Post by Celauran »

It produces the same output every time if you pass it the same salt every time. If you don't specify a salt, it generates a random one.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Script keeps outputing different hashes for same passwor

Post by Celauran »

Why not just use password_hash()?
User avatar
cap2cap10
Forum Contributor
Posts: 158
Joined: Mon Apr 14, 2008 11:06 pm

Re: Script keeps outputing different hashes for same passwor

Post by cap2cap10 »

Ahhhhh. Hence the NULL value. Thanks for enlightening my ignorance.

Sincerely,

Batoe
Post Reply