Page 1 of 1

Script keeps outputing different hashes for same password!

Posted: Mon Aug 25, 2014 2:34 pm
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

Re: Script keeps outputing different hashes for same passwor

Posted: Mon Aug 25, 2014 2:38 pm
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.

Re: Script keeps outputing different hashes for same passwor

Posted: Mon Aug 25, 2014 2:39 pm
by Celauran
Why not just use password_hash()?

Re: Script keeps outputing different hashes for same passwor

Posted: Mon Aug 25, 2014 2:47 pm
by cap2cap10
Ahhhhh. Hence the NULL value. Thanks for enlightening my ignorance.

Sincerely,

Batoe