password generation algo

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
phpcoder
Forum Contributor
Posts: 158
Joined: Sat Nov 02, 2002 1:18 pm
Location: Manchester, UK

password generation algo

Post by phpcoder »

i want some algorithm to generate password for scratch card my card serial # is atrt from 0001 onward pl help me
m3rajk
DevNet Resident
Posts: 1191
Joined: Mon Jun 02, 2003 3:37 pm

Post by m3rajk »

do you have any ideas on how to do it? i have something that does letters and numbers mixed rather well but i'm not simply going to give you something. show me that you've got a clue as to what you're doing and you need to be pointed in a direction, then i will help you get where you want, and if you're close enough just give you what i have
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Post by JAM »

jason
Site Admin
Posts: 1767
Joined: Thu Apr 18, 2002 3:14 pm
Location: Montreal, CA
Contact:

Post by jason »

http://www.evilwalrus.com/viewcode.php?codeEx=535 is what I normally use if I need to create on the fly passwords.
Drachlen
Forum Contributor
Posts: 153
Joined: Fri Apr 25, 2003 1:16 am

Post by Drachlen »

Code: Select all

<?php

	for($x=0;$x<32;$x++){
		
		$random = rand(33,105);
		
		$password .= chr($random);

	}
	
	echo md5($password);

?>
User avatar
scorphus
Forum Regular
Posts: 589
Joined: Fri May 09, 2003 11:53 pm
Location: Belo Horizonte, Brazil
Contact:

Post by scorphus »

Mixing Jason's and Drachlen's solutions, I came into this:

Code: Select all

<?
function random_password ($length = 0) {
	$dx = mt_rand(0, 32 - $length);
	$length_ini = empty($length) ? mt_rand(0, 19) : $dx;
	$length_fin = empty($length) ? mt_rand(6, 12) : $length;
	for ($x = 0; $x < 32; $x++) {
		$random = rand(33, 105);
		$password .= chr($random);
	}
	$password = md5($password);
	$password = substr(md5($password), $length_ini, $length_fin);
	return $password;
}

echo random_password(10);
echo random_password(8);
echo random_password(6);
echo random_password(4);
?>
An output:

Code: Select all

5500cc4e2c
8b6ff745
b5ab68
af9c
Specify the length of the password, from 1 (not really) to 32 in random_password function call. If the $length parameter is set to 0, the function returns a 6 to 12 length password.

Cheers,
Scorphus.
jason
Site Admin
Posts: 1767
Joined: Thu Apr 18, 2002 3:14 pm
Location: Montreal, CA
Contact:

Post by jason »

Hey, that's my password!!
User avatar
scorphus
Forum Regular
Posts: 589
Joined: Fri May 09, 2003 11:53 pm
Location: Belo Horizonte, Brazil
Contact:

Post by scorphus »

Sure it is! I think you missed the very first line of my post...

Your function was repeating some passwords from time to time, then I merged Drachlen's solution to yours.

I was in search for such an algo... thanks!

Regards,
Scorphus.
jason
Site Admin
Posts: 1767
Joined: Thu Apr 18, 2002 3:14 pm
Location: Montreal, CA
Contact:

Post by jason »

No, I meant, that is my PASSWORD, as in haha, funny funny.

As in one of the 4 examples you displayed was my password. =p
Post Reply