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! |
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!
|
|
|
|
Join us for this on demand webcast to learn about developing complex systems more quickly and efficiently. We'll cover market drivers for developing, governing and reusing systems software assets and how you can develop system software assets with Rational Asset Manager. FREE! Go There Now!
|
|
|
|
Visit IBM developerWorks to download a free trial version of IBM Rational Business Developer V7.1. Rational Business Developer offers rapid and simplified development of business applications and services through Enterprise Generation Language (EGL) tools, generating Java or mainframe solutions while shielding developers from technical complexities. FREE! Go There Now!
|
|
|
|
Visit IBM developerWorks to download a free trial of the Rational Host Access Transformation Services (HATS) Toolkit. The HATS toolkit provides a set of plug-ins for the IBM Rational Software Delivery Platform to help you easily extend your legacy applications. HATS makes your 3270 and 5250 applications available as HTML through the most popular Web browsers, while converting your host screens to a Web look and feel and it also enables you to develop new Web, portal, and rich-client applications. FREE! Go There Now!
|
|
|
|
Learn how to implement a build management system that uses and extends your existing automation technologies. This tutorial shows, step-by-step, how to install and configure IBM Rational Build Forge to manage builds for Jakarta Tomcat from source code. 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!
|
|
|
|
This webcast outlines the best practices that must be instituted to gain the maximum benefit from SOA while maintaining high quality of service. Whether you are deploying new applications or managing and monitoring your existing infrastructure, learn how you can ensure high quality of services with SOA based solutions from IBM. All registrants who attend this live Web Seminar will receive complimentary access to a white paper titled “Maintaining QoS in an SOA Environment”. FREE! Go There Now!
|
|
|
|
This paper is about the critical role that a discipline called integrated requirements management can play in helping to ensure that your business goals and IT investments are continuously aligned—whether you are sourcing, integrating, building or maintaining software. It also looks at ways that automated IBM Rational® products can work together to help you use requirements in the very best way. FREE! Go There Now!
|
|
|
|
The Eclipse community is constantly working to extend Eclipse's functionality. In this webcast, learn about some of the most important and feature-rich projects under development. From multi-language support to plug-in development, tune in to see what Eclipse is capable of now. FREE! Go There Now!
|
|
|
|
WebSphere Process Server delivers a unique integration framework that simplifies existing IT resources. Often, as IT assets grow to support business demand, so too does their complexity and manageability. In this webcast, we’ll discuss how WebSphere Process Server helps deliver an SOA infrastructure that provides a common model to orchestrate, mediate, connect, map, and execute the underlying IT functions. Discover how WebSphere Process Server simplifies integration of business processes by leveraging existing IT assets as reusable services without the complexities of traditional integration methodologies. FREE! Go There Now!
|
|
|
|
All FREE IBM® developerWorks Tools! |