How to emulate password generation in PHP with PlpgSQL?

Started by Andre Lopesalmost 16 years ago2 messagesgeneral
Jump to latest
#1Andre Lopes
lopes80andre@gmail.com

Hi,

I need to create users in a database function. I'am dealing with a PHP
application, the code that generate the password is this:

[code]
public function salt()
{
return substr(md5(uniqid(rand(), true)), 0, 10);
}

public function hash_password($password, $salt=false)
{
if (empty($password))
{
return FALSE;
}

if (FALSE && $salt)
{
return sha1($password . $salt);
}
else
{
$salt = $this->salt();
return $salt . substr(sha1($salt . $password), 0, -10);
}
}
[/code]

It is possible to emulate this in a PlpgSQL function?

I have a function that generates the SHA1 codes

[code]
CREATE OR REPLACE FUNCTION sha1(bytea) returns text AS $$
SELECT encode(digest($1, 'sha1'), 'hex')
$$ LANGUAGE SQL STRICT IMMUTABLE;
[/code]

But I'am not getting how to generate the SALT. Can someone give me a clue on
how to do this.

Best Regards,

#2Josh Kupershmidt
schmiddy@gmail.com
In reply to: Andre Lopes (#1)
Re: How to emulate password generation in PHP with PlpgSQL?

On Sun, Jun 13, 2010 at 8:45 AM, Andre Lopes <lopes80andre@gmail.com> wrote:

But I'am not getting how to generate the SALT. Can someone give me a clue on
how to do this.

The salt() function you posted returns 10 random hexadecimal digits.
You could mimic it with something like:
SELECT substr(md5(RANDOM()::text), 0, 11);

Josh