Skip to content

Commit

Permalink
Restructure bootstrap system, add optional autoloader
Browse files Browse the repository at this point in the history
  • Loading branch information
ircmaxell committed Jul 20, 2011
1 parent be32bf0 commit 83a7bbc
Show file tree
Hide file tree
Showing 14 changed files with 130 additions and 353 deletions.
27 changes: 5 additions & 22 deletions build/phar.stub.php
Expand Up @@ -18,30 +18,13 @@

namespace CryptLib;

if (defined('\\CryptLib\\BOOTSTRAPPED')) {
return;
}

define('BOOTSTRAPPED', true);

\Phar::mapPhar('CryptLib.phar');
\Phar::interceptFileFuncs();

/**
* The simple autoloader for the CryptLib library.
*
* @param string $class The class name to load
*
* @return void
*/
spl_autoload_register(function ($class) {
if (substr($class, 0, strlen(__NAMESPACE__)) == __NAMESPACE__) {
$path = str_replace('\\', '/', $class);
$path = 'phar://CryptLib.phar/' . $path . '.php';
if (file_exists($path)) {
require $path;
}
}
});
require_once 'phar://CryptLib.phar/CryptLib/Core/AutoLoader.php';

$autoloader = new \CryptLib\Core\AutoLoader(__NAMESPACE__, 'phar://CryptLib.phar');

$autoloader->register();

__HALT_COMPILER();
16 changes: 8 additions & 8 deletions build/phing/package.xml
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<project name="package" default="package" basedir="../../">
<property file="build/build.properties" />

<target name="package">
<delete dir="${path.results/lib" />
<delete dir="${path.package}" />
Expand All @@ -18,11 +18,11 @@
<phingcall target="packageFiles" />
<phingcall target="packagePhar" />
</target>

<target name="copyFilesToResultsLib">
<delete dir="${path.results}/lib" />
<mkdir dir="${path.results}/lib" />

<copy todir="${path.results}/lib">
<filterchain>
<replacetokens begintoken="@@" endtoken="@@">
Expand All @@ -34,7 +34,7 @@
</fileset>
</copy>
</target>

<target name="packageFiles">
<zip destfile="${path.package}/CryptLib.zip">
<fileset dir="${path.results}/lib">
Expand Down Expand Up @@ -66,7 +66,7 @@
<filehash file="${path.package}/CryptLib.tar.bz2" hashtype="1" propertyname="filehash" />
<echo message="${filehash}" file="${path.package}/CryptLib.tar.bz2.sha1" />
</target>

<target name="packagePear">
<pearpkg name="CryptLib" dir="${path.results}/lib" destfile="${path.results}/lib/package.xml">
<fileset dir="${path.results}/lib">
Expand All @@ -87,11 +87,11 @@
</mapping>
</pearpkg>
</target>

<target name="packagePhar">
<pharpackage
destfile="${path.package}/CryptLib.phar"
basedir="${path.lib}"
basedir="${path.results}/lib"
stub="${path.build}/phar.stub.php"
signature="sha1"
>
Expand All @@ -101,7 +101,7 @@
<metadata>
<element name="version" value="${version.number}" />
</metadata>

</pharpackage>
</target>
</project>
6 changes: 3 additions & 3 deletions examples/Password/drupal.php
Expand Up @@ -32,7 +32,7 @@

//It's safe to print, so let's output it:
printf(
"Password: %s, Hash: %s\n",
"Password: %s\nHash: %s\n\n",
$password,
$hash
);
Expand All @@ -48,12 +48,12 @@
/**
* Next, we verify the hash with the expected password.
*/
$test = $hasher2->verify($hash, $password);
$test = $hasher2->verify($password, $hash);

/**
* $test should now contain a boolean value as to the validity of the hash
*/
printf(
"Verification was %s",
"Verification was %s\n\n",
$test ? "Successful!" : "Failed!"
);
6 changes: 3 additions & 3 deletions examples/Random/numbers.php
Expand Up @@ -16,7 +16,7 @@


/**
* Let's generate some random integers! For this, we'll use the
* Let's generate some random integers! For this, we'll use the
* CryptLib\Random\Factory class to build a random number generator to suit
* our needs here.
*/
Expand All @@ -28,11 +28,11 @@
$factory = new \CryptLib\Random\Factory;

/**
* Now, since we want a low strength random number, let's get a low strength
* Now, since we want a low strength random number, let's get a low strength
* generator from the factory.
*
* If we wanted stronger random numbers, we could change this to medium or high
* but both use significantly more resources to generate, so let's just stick
* but both use significantly more resources to generate, so let's just stick
* with low for the purposes of this example:
*/
$generator = $factory->getLowStrengthGenerator();
Expand Down
8 changes: 4 additions & 4 deletions examples/Random/strings.php
Expand Up @@ -15,7 +15,7 @@
namespace CryptLibExamples\Random;

/**
* Let's generate some random strings! For this, we'll use the
* Let's generate some random strings! For this, we'll use the
* CryptLib\Random\Factory class to build a random number generator to suit
* our needs here.
*/
Expand All @@ -27,11 +27,11 @@
$factory = new \CryptLib\Random\Factory;

/**
* Now, since we want a low strength random number, let's get a low strength
* Now, since we want a low strength random number, let's get a low strength
* generator from the factory.
*
* If we wanted stronger random numbers, we could change this to medium or high
* but both use significantly more resources to generate, so let's just stick
* but both use significantly more resources to generate, so let's just stick
* with low for the purposes of this example:
*/
$generator = $factory->getLowStrengthGenerator();
Expand All @@ -56,7 +56,7 @@
* But, we can also generate random strings against a list of characters. That
* way we can use the random string in user-facing situations: (this can be for
* one-time-use passwords, CRSF tokens, etc).
*
*
* Now, let's define a string of allowable characters to use for token
* generation.
*/
Expand Down
80 changes: 0 additions & 80 deletions lib/CryptLib/Cipher/Block/Cipher/X_OR.php

This file was deleted.

92 changes: 92 additions & 0 deletions lib/CryptLib/Core/AutoLoader.php
@@ -0,0 +1,92 @@
<?php
/**
* An implementation of the PSR-0 Autoloader. This can be replaced at will with
* other implementations if necessary.
*
* PHP version 5.3
*
* @category PHPCryptLib
* @package Core
* @author Anthony Ferrara <ircmaxell@ircmaxell.com>
* @copyright 2011 The Authors
* @license http://www.opensource.org/licenses/mit-license.html MIT License
* @version Build @@version@@
*/

namespace CryptLib\Core;

/**
* An implementation of the PSR-0 Autoloader. This can be replaced at will with
* other implementations if necessary.
*
* @category PHPCryptLib
* @package Core
* @author Anthony Ferrara <ircmaxell@ircmaxell.com>
*/
class AutoLoader {

/**
* @var string The namespace prefix for this instance.
*/
protected $namespace = '';

/**
* @var string The filesystem prefix to use for this instance
*/
protected $path = '';

/**
* Build the instance of the autoloader
*
* @param string $namespace The prefixed namespace this instance will load
* @param string $path The filesystem path to the root of the namespace
*
* @return void
*/
public function __construct($namespace, $path) {
$this->namespace = ltrim($namespace, '\\');
$this->path = rtrim($path, '/\\') . DIRECTORY_SEPARATOR;
}

/**
* Try to load a class
*
* @param string $class The class name to load
*
* @return boolean If the loading was successful
*/
public function load($class) {
$class = ltrim($class, '\\');
if (strpos($class, $this->namespace) === 0) {
$nsparts = explode('\\', $class);
$class = array_pop($nsparts);
$nsparts[] = '';
$path = $this->path . implode(DIRECTORY_SEPARATOR, $nsparts);
$path .= str_replace('_', DIRECTORY_SEPARATOR, $class) . '.php';
if (file_exists($path)) {
require $path;
return true;
}
}
return false;
}

/**
* Register the autoloader to PHP
*
* @return boolean The status of the registration
*/
public function register() {
return spl_autoload_register(array($this, 'load'));
}

/**
* Unregister the autoloader to PHP
*
* @return boolean The status of the unregistration
*/
public function unregister() {
return spl_autoload_unregister(array($this, 'load'));
}

}
8 changes: 6 additions & 2 deletions lib/CryptLib/CryptLib.php
Expand Up @@ -16,9 +16,13 @@
namespace CryptLib;

/**
* Ensure that we're bootstrapped
* The autoloader class will be autoloaded at this point even if another autoloader
* is in use. So if it does not exist at this point, we know we must bootstrap
* the libraries.
*/
require_once __DIR__ . '/bootstrap.php';
if (!class_exists('\\CryptLib\Core\AutoLoader', true)) {
require_once 'bootstrap.php';
}

use CryptLib\Password\Factory as PasswordFactory;
use CryptLib\Random\Factory as RandomFactory;
Expand Down
7 changes: 1 addition & 6 deletions lib/CryptLib/Random/Generator.php
Expand Up @@ -18,11 +18,6 @@

use CryptLib\Core\BaseConverter;

/**
* The number of bits in each byte.
*/
define('BITS_PER_BYTE', 8);

/**
* The Random Number Generator Class
*
Expand Down Expand Up @@ -151,7 +146,7 @@ public function generateString($length, $characters = '') {
'ABCDEFGHIJKLMNOPQRSTUVWXYZ./';
}
//determine how many bytes to generate
$bytes = ceil($length * log(strlen($characters), 2) / BITS_PER_BYTE);
$bytes = ceil($length * log(strlen($characters), 2) / 8);
$rand = $this->generate($bytes);
$result = BaseConverter::convertFromBinary($rand, $characters);
if (strlen($result) < $length) {
Expand Down

0 comments on commit 83a7bbc

Please sign in to comment.