Jump to content

Is this correct? OOP class to get salted PW hash?


nakins

Recommended Posts

<?php

class Phash
{
public function __construct(){

const saltLenght = 20;
public $_salt;

}

public function getSalted($_POST['password'], $_salt)
{
    if ($_salt === null)
    {
        $_salt = substr(md5(uniqid(rand(), true)), 0, saltLenght);
    }
    else
    {
        $_salt = substr($_salt, 0, saltLenght);
    }

    return $_salt . sha1($_salt . $_POST['password']);
}

}

Link to comment
Share on other sites

As thorpe mentioned, the functionality in here is very limited to be a class of it's own. If you have a user class or whatever, just stick the salting in there. Anyway, to get to the question, I rewrote your class to the one below:

 

<?php
class PHash {
private $salt_length = 20;

public function makeSalt ($password, $salt) {
	if ($salt === NULL) {
		$salt = substr(md5(uniqid(rand(), true)), 0, $this->salt_length);
	} else {
		$salt = substr($salt, 0, $this->salt_length);	
	}

	return sha1($salt . $password);
}
}

//object initialization
$phash = new PHash;
$new_pass = $phash->makeSalt('myPASSword2011', 'phpfreaks.com');
?>

 

Simple enough, but at least it works. Keep in mind that a class is just a definition and it's not supposed to process data outside its scope (as the POST superglobal is). An object does that! Plus, you have set a superglobal array element as a method parameter, which adds to the confusion.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.