Skip to content

Instantly share code, notes, and snippets.

@andrewwoods
Last active August 29, 2015 14:09
Show Gist options
  • Save andrewwoods/8372ab18446044db2053 to your computer and use it in GitHub Desktop.
Save andrewwoods/8372ab18446044db2053 to your computer and use it in GitHub Desktop.
Password Generator by Pattern
#!/usr/bin/php
<?php
/**
* Generate a password based on a pattern
*
* @package Command Line
* @subpackage Utilities
* @version 1.0
* @author Andrew Woods <atwoods1@gmail.com>
*
* @see getopt — Gets options from the command line argument list
*/
// parse commnd line arguments
$shortopts = "p:h";
$longopts = array(
"pattern:",
"help",
);
$option = getopt($shortopts, $longopts);
if ( isset($option['h']) || isset($option['help']) ) {
help();
exit(0);
}
$pattern = '';
if ( isset($option['p']) ) {
$pattern = $option['p'];
}
if ( isset($option['pattern']) ) {
$pattern = $option['pattern'];
}
// Create a new password
$creator = new Password_Create();
if ( $pattern ) {
$result = $creator->generate( $pattern );
} else {
$result = $creator->generate();
}
// show the user their new password
echo "Your password is {$result}\n\n";
/**
* Password Creation Class
*
* This password creation class encapsulates and manages the data and logic of creating
* a random password
*
* @package Command Line
* @subpackage Utilities
*
* @author Andrew Woods <atwoods1@gmail.com>
* @access public
*/
class Password_Create
{
private $digits = array();
private $lowercase = array();
private $uppercase = array();
private $punctuation = array();
public function __construct() {
// Initialize the data arrays;
$this->digits = range( 0, 9 );
$this->lowercase = range( 'a', 'z' );
$this->uppercase = range( 'A', 'Z' );
$this->punctuation = array( '~', '!', '@', '#', '$', '%', '^', '&', '*', '(', ')',
'_', '+', '`', '-', '=', '[', ']', '{', '}', '|', ';', "'", ':', '"',
',', '.', '/', '<', '>', '?');
}
/**
* generates the random password based on a pattern
*
* Creates a random password based on a default pattern. User can chose to override
* it by specifying their own pattern.
*
* @since 1.0
*
* @see substr — Return part of a string
* @see array_rand — Pick one or more random entries out of an array
*
* @param string $pattern a coded string that determines the type of character desired.
* @return string the resulting password based on the pattern
*/
public function generate( $pattern = 'AdapAdapAdap' ) {
$output = '';
$id = null;
for ( $i = 0; $i < strlen( $pattern ); $i++ ) {
$item = substr($pattern, $i, 1);
if ( 'a' === $item ) {
$id = array_rand( $this->lowercase);
$output .= $this->lowercase[ $id ];
} else if ( 'A' === $item ) {
$id = array_rand( $this->uppercase);
$output .= $this->uppercase[ $id ];
} else if ( 'd' === $item ) {
$id = array_rand( $this->digits);
$output .= $this->digits[ $id ];
} else if ( 'p' === $item ) {
$id = array_rand( $this->punctuation);
$output .= $this->punctuation[ $id ];
} else {
warn('Unrecognized type in pattern - should be A, a, d, p');
}
}
return $output;
}
}
function help() {
?>
NAME
passwd-gen.php - Create a random password based on a pattern
SYNOPSIS
passwd-gen.php [-h | --help] [-p PATTERN | --pattern=PATTERN]
DESCRIPTION
Compare files line by line.
-h --help
Display help information.
-p PATTERN --pattern=PATTERN
use the PATTERN string to determine the types of characters used in your random password
* A for Uppercase alphabetic characters
* a for lowercase alphabetic characters
* d for digits (integers)
* p for punctuation characters
EXAMPLES
$ passwd-gen.php -h # display the help message
$ passwd-gen.php -p AApd # Create a random password with 2 uppercase letters, a punctuation, and a digit
AUTHOR
Andrew Woods
<?php
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment