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! |
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!
|
|
|
|
Building a software-as-a-service solution requires addressing a few key technical challenges. In this webcast, we'll focus on the role of IBM Tivoli Directory Server and WebSphere Portlet Factory in creating a Software as a Service solution. We will demonstrate how to use Tivoli Directory Server to prevent the user population of one tenant from accessing the virtual portal and portlet components of another tenant. We will also use the dynamic profile capability of WebSphere Portlet Factory to create multiple highly customized applications from one code base. FREE! Go There Now!
|
|
|
|
Hear how IBM Rational Project and Portfolio Management integrated solutions help teams put the right tools and processes in place to maximize the effectiveness and efficiency of project teams and ensure that the business vision is being executed correctly. Learn how to automate and integrate requirements prioritization, top-down project planning, communications and controls, and methodology deployment to keep your scope, costs, and schedules under control. Tackle with an end-to-end approach the management of scope and scope changes, usage of methodology to control and empower project teams, and optimization of resources to align activity costs with the overall project plan. FREE! Go There Now!
|
|
|
|
Hold your calendar on January 30, 2008 for this free webcast on the new i5/OS. Rational's Enterprise Modernization products will be discussed at this webcast as they help to drive the application development environment for this new System i OS. <br />And learn how i5/OS will take you to the next step of efficient, resilient business processing. You will hear about the new i5/OS capabilities as it will be the most significant i5/OS release in years. If you cannot join the webcast on 1/30/08 you can still use this link to listen to the replay.<br /> FREE! Go There Now!
|
|
|
|
This tutorial discusses the concepts of REST and the Atom Publishing Protocol (APP) and shows how they apply to services. It also shows how to use Java technology to implement REST/APP-based services. FREE! Go There Now!
|
|
|
|
This demonstration gives you an overview of IBM® Rational® Build Forge Express Edition, a global offering that provides a framework to automate and execute software processes. Rational Build Forge provides a software assembly line that can support all of your tools, technologies, and platforms so you can achieve a repeatable, reliable, and traceable build and release process. FREE! Go There Now!
|
|
|
|
Discover how Rational tools and best practices for testing can make your job easier. The new Rational Testing eKits provide you with valuable resources – including demos, webcasts, tutorials, and articles – that help you address your specific testing needs across the software lifecycle. Five new eKits are available covering the topics of Requirements and Test Management, Functional Testing, Performance Testing, Code Quality and Embedded Systems, and SOA and Web Services Testing. FREE! Go There Now!
|
|
|
|
Viper 2 brings a great value to developer communities including SQL, XML, PHP, Ruby, .NET and Java. You probably already know that DB2 Express-C is free for developers to develop, deploy and distribute. Viper 2 provides a variety of means that help move your application from the development stage to deployment more rapidly. This webcast shows how to best utilize the latest tools available for developing DB2 applications. 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!
|
|
|
|
Set up a PHP Web interface for the Java(TM) business application using a database created in earlier in this series. The PHP Web interface collects information from users and sends the session data to the Java business application for processing and for a response. FREE! Go There Now!
|
|
|
|
All FREE IBM® developerWorks Tools! |