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! |
Effective governance for lean development isn’t about command and control. Instead, the focus is on enabling the right behaviors and practices through collaborative and supportive techniques. Hear from Scott Ambler on how it is far more effective to motivate people to do the right thing than it is to force them to do so. Learn how to form a lightweight, collaboration-based framework that reflects the realities of modern IT organizations. FREE! Go There Now!
|
|
|
|
Visit IBM developerWorks to download a free trial version of WebSphere Business Modeler Advanced V6.1.1, IBM’s premier business process modeling and analysis tool for business users that offers process modeling, simulation, and analysis capabilities. IBM WebSphere Business Modeler helps you visualize, understand, and document business processes for continuous improvement. FREE! Go There Now!
|
|
|
|
Download the IBM WebSphere Portal V6.1 beta code and learn more about the rich features and enhancements in IBM WebSphere Portal V6.1. WebSphere Portal provides a composite application or business mashup framework and the advanced tooling needed to build flexible, SOA-based solutions, and scalability to meet the needs of any size organization. FREE! Go There Now!
|
|
|
|
Join this webcast to see how IBM Data Studio Developer and pureQuery can take the pain out of Java data access. uApplications developed using both Java and SQL have become a common requirement. Database connectivity using Java Database Connectivity (JDBC) to create an application is a multi-step tedious process, and tooling that covers both SQL and Java has been unavailable, until now. IBM Data Studio introduces the pureQuery platform: a high-performance, Java data access platform focused on simplifying the tasks of developing, managing, and optimizing database applications and services. FREE! Go There Now!
|
|
|
|
As systems increase in complexity, communication between systems and software teams becomes more and more difficult. Now, there’s a way to improve product quality and communication.<br />Read the “Model Driven Systems Development” white paper to see how. Also included in this kit are more educational white papers, customer examples, tutorials, informative Webcasts, and best practices for designing, building and managing systems.<br /> FREE! Go There Now!
|
|
|
|
Asset Reuse is a key strategy for companies looking to create innovative solutions to solve complex software development problems. Searching for, identifying, updating, using and deploying software assets can be a difficult challenge. Listen to this webcast, to learn about strategies and tools that you can leverage for a successful project, including Rational Asset Manager, Rational Software Architect and WebSphere Service Registry and Repository. FREE! Go There Now!
|
|
|
|
Ken Krugler, co-founder of code search company Krugle, and Laura Merling, vice president of Marketing and Business Development for Krugle, join to talk about the ins and outs of code search and what it means as a new feature for developerWorks users. FREE! Go There Now!
|
|
|
|
This whitepaper provides areas to consider when evaluating any software configuration management solution. It addresses how the IBM solutions (Rational ClearCase and Rational ClearQuest) meet the needs and requirements of both project leaders and developers to provide successful Software Change and Configuration Management. FREE! Go There Now!
|
|
|
|
Get a free trial download of the latest version of IBM Rational Method Composer V7.2 which helps you deliver customized yet consistent process guidance to your project teams and IT organization, and includes the latest version of IBM Rational Unified Process (RUP), which has provided process guidance to teams since 1996. FREE! Go There Now!
|
|
|
|
Join this webcast to learn how IBM Rational's Functional Testing solution enables you to implement automation your way, at your pace, with your existing staff. In this webcast, you’ll learn how you can eliminate redundancy of manual test scripts, reduce errors, and increase test coverage through test automation. After this presentation you will understand how IBM Rational Functional Testing solution can streamline your manual testing and make test automation easily attainable. FREE! Go There Now!
|
|
|
|
All FREE IBM® developerWorks Tools! |