Generates random password for a selected length from a selected set (numbers, lowercase, uppercase, lower+upper, lower+upper+numbers, almost full keyboard).
By : bjpalmer
<?php
//Random Password Function
function generatePassword ($passwordLength, $characterSet)
{
//Random password generator if anyone needs one
//B Palmer 2007 - Distribute, manipulate, just dont sell, not that its worth anything anyway!
//Character Sets
//1 - numbers only (48[0] to 57[9]) - 10
//2 - lowercase only (97[a] to 122[z]) - 26
//3 - uppercase only (65[A] to 90[Z]) - 26
//4 - lowercase (97[a] to 122[z]) + uppercase (65[A] to 90[Z]) - 52
//5 - lowercase (97[a] to 122[z]) + uppercase (65[A] to 90[Z]) + numbers (48[0] to 57[9]) - 62
//6 - full keyboard set (32 to 126) less space (32[space], 34["], 39['], 96[`]) - 91
//1 - numbers only (48[0] to 57[9]) - 10
if($characterSet==1)
{
$passwordString = "";
for($i=0; $i<$passwordLength; $i++)
{
$selectedChar = rand(0, 9);
$passwordString .= $selectedChar;
}
}
//2 - lowercase only (97[a] to 122[z]) - 26
if($characterSet==2)
{
$passwordString = "";
for($i=0; $i<$passwordLength; $i++)
{
$selectedChar = rand(97, 122);
$selectedChar = chr($selectedChar);
$passwordString .= $selectedChar;
}
}
//3 - uppercase only (65[A] to 90[Z]) - 26
if($characterSet==3)
{
$passwordString = "";
for($i=0; $i<$passwordLength; $i++)
{
$selectedChar = rand(65, 90);
$selectedChar = chr($selectedChar);
$passwordString .= $selectedChar;
}
}
//4 - lowercase (97[a] to 122[z]) + uppercase (65[A] to 90[Z]) - 52
if($characterSet==4)
{
$passwordString = "";
for($i=0; $i<$passwordLength; $i++)
{
$selectedChar = rand(1, 52);
if($selectedChar>=1&&$selectedChar<=26)
{
$selectedChar = $selectedChar + 64;
$selectedChar = chr($selectedChar);
}
else
{
$selectedChar = $selectedChar + 70;
$selectedChar = chr($selectedChar);
}
$passwordString .= $selectedChar;
}
}
//5 - lowercase (97[a] to 122[z]) + uppercase (65[A] to 90[Z]) + numbers (48[0] to 57[9]) - 62
if($characterSet==5)
{
$passwordString = "";
for($i=0; $i<$passwordLength; $i++)
{
$selectedChar = rand(1, 62);
if($selectedChar>=1&&$selectedChar<=10)
{
$selectedChar = $selectedChar + 47;
$selectedChar = chr($selectedChar);
}
elseif($selectedChar>=11&&$selectedChar<=36)
{
$selectedChar = $selectedChar + 54;
$selectedChar = chr($selectedChar);
}
else
{
$selectedChar = $selectedChar + 60;
$selectedChar = chr($selectedChar);
}
$passwordString .= $selectedChar;
}
}
//6 - full keyboard set (32 to 126) less space (32[space], 34["], 39['], 96[`]) - 91
if($characterSet==6)
{
$passwordString = "";
for($i=0; $i<$passwordLength; $i++)
{
$selectedChar = rand(1, 91);
if($selectedChar==1)
{
$selectedChar = $selectedChar + 32;
$selectedChar = chr($selectedChar);
}
elseif($selectedChar>=2&&$selectedChar<=5)
{
$selectedChar = $selectedChar + 33;
$selectedChar = chr($selectedChar);
}
elseif($selectedChar>=6&&$selectedChar<=61)
{
$selectedChar = $selectedChar + 34;
$selectedChar = chr($selectedChar);
}
else
{
$selectedChar = $selectedChar + 35;
$selectedChar = chr($selectedChar);
}
$passwordString .= $selectedChar;
}
}
return $passwordString;
}
//Example Usage
$randomPass = generatePassword (12, 1);
echo "Set 1: ".$randomPass."<br />";
$randomPass = generatePassword (12, 2);
echo "Set 2: ".$randomPass."<br />";
$randomPass = generatePassword (12, 3);
echo "Set 3: ".$randomPass."<br />";
$randomPass = generatePassword (12, 4);
echo "Set 4: ".$randomPass."<br />";
$randomPass = generatePassword (12, 5);
echo "Set 5: ".$randomPass."<br />";
$randomPass = generatePassword (12, 6);
echo "Set 6: ".$randomPass."<br />";
?>
| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |
More Miscellaneous Code Articles
More By Codewalkers
developerWorks - FREE Tools! |
<a href="http://zeus.developershed.com/shonuff.php?blackbird=3853&zoneid=442&source=&dest=http%3A%2F%2Fwww.ibm.com%2Fdeveloperworks%2Fspaces%2Fjazz%3FS_TACT%3D105AGY31%26S_CMP%3DDEVSHED&ismap="><img src="http://images.devshed.com/corp/img/news/jazz01.gif" alt="developerWorks Jazz space" align="left"></a>You've heard the buzz about Jazz... want to know more about it from a developer's perspective? Check out the Jazz space on developerWorks. This space is an up-to-date resource for developers, including technical information about Jazz and products built on Jazz, like Rational Team Concert Express. The Jazz space includes content from a wide variety of sources, including links, feeds, and comments from experts. FREE! Go There Now!
|
|
|
|
David Barnes, Lead Evangelist for IBM Emerging Internet Technologies will discuss aspects of Web 2.0 that bring value to corporations, academia, and government. He'll also discuss IBM's vision around Web 2.0, including the importance of remixability and consumability. The discussion will culminate with examples of various IBM Software Group solutions you can use to get ahead of the Web 2.0 adoption curve. FREE! Go There Now!
|
|
|
|
Learn to enable users to both rate existing animations and to combine existing animations into new snippets. This is the third in a series of three tutorials that chronicle the building of a site that enables collaborative discussion and animation building using Domino and OpenLaszlo. FREE! Go There Now!
|
|
|
|
Build secure Web services with transport-level security using IBM Rational Application Developer V7 and IBM WebSphere Application Server V6.1. Follow this three-part series for step-by-step instructions about how to develop Web services and clients, configure HTTP basic authentication, and configure HTTP over SSL (HTTPS). This first part of the series walks you through building a Web service for a simple calculator application. You generate and test two different types of Web services clients: a Java Platform, Enterprise Edition (Java EE) client and a stand-alone Java client. You also handle user-defined exceptions in Web services. FREE! Go There Now!
|
|
|
|
Download a free trial version of IBM Rational Developer for System i V7.1, which provides a complete development environment for traditional i5/OS application development. IBM Rational Developer for System i is a new eclipse-based workstation offering for i5/OS application development that provides a comprehensive Integrated Development Environment for edit/compile/debug of traditional RPG/COBOL/C/C++ i5/OS applications. FREE! Go There Now!
|
|
|
|
This Fall, IBM Rational talks to you directly through a special teleconference series giving you access to the best minds in IBM Rational - product experts and market thought leaders who will answer your questions during these pre-scheduled telephone conference calls. Register today! FREE! Go There Now!
|
|
|
|
Join this Rational Talks to You teleconference on December 4 at 1:00 pm ET to discuss how Rational Method Composer can help meet your compliance objectives. Get your questions answered! FREE! Go There Now!
|
|
|
|
Informix Dynamic Server (IDS) Express Edition offers outstanding online transaction processing (OLTP) database performance, while helping to simplify and automate many of the tasks associated with deploying databases for small business applications. IDS 11 further extends the ease of management and applications integration with the Admin API and Scheduler, high availability with Continuous Log Restore for backup server recovery in case of a primary server failure, and column level encryption to protect personal and company private data. FREE! Go There Now!
|
|
|
|
Visit IBM developerWorks to try the IBM SOA Sandbox for process. The SOA Sandbox for process focuses on providing a trial environment with the necessary tooling and components required to gain a better understanding of business processes and how to best improve existing business processes to derive value quickly. FREE! Go There Now!
|
|
|
|
IBM Lotus Notes 8 provides a wide range of developers the ability to provide customized, integrated user interfaces via composite applications and via custom sidebar and toolbar plug-ins. This webcast provides you with tips and techniques to use with out-of-the-box capabilities of Lotus Notes 8, and survey how you can share useful components within your own company and within a larger community. FREE! Go There Now!
|
|
|
|
All FREE IBM® developerWorks Tools! |