Thread: How to emulate password generation in PHP with PlpgSQL?

How to emulate password generation in PHP with PlpgSQL?

From
Andre Lopes
Date:
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,

Re: How to emulate password generation in PHP with PlpgSQL?

From
Josh Kupershmidt
Date:
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