random password generate

Hello,

I want to generate random password when user register my site.




$length = 10;

$chars = array_merge(range(0,9), range('a','z'), range('A','Z'));

shuffle($chars);

$password = implode(array_slice($chars, 0, $length));



Thanks, it’s working

I built a class that generates all kinds of hashes.

http://code.google.com/p/ps-yii-extensions/source/browse/trunk/extensions/pogostick/helpers/CPSHash.php

– Jerry

As I say everytime :D why do you hash a random (pseudo-random) string? Hashing something doesn’t make it “more random”, instead it is less random because of the nature of an hashing algorithm (there could be collisions).


$password = uniqid('', true);

How to DECRYPT password? bcz I want to send DECRYPT password to user.

That would be a plain password, randomly generated.

If you plan to have user submitted passwords, then you should hash them like




$salt = uniqid('', true);

$hash = hash('sha256', $salt.$_POST['password']); // here $_POST['password'] is the submitted pass



and for security reasons it is not “decryptable” (it’s an hash, a one way function).

If you want a new password you have to regenerate it.

Well, you could also use an encryption function instead of an hashing function so you could decrypt passwords, but the encryption key must be strictly private and with a good robustness, take a look here for info http://www.php.net/manual/en/ref.mcrypt.php