pax_global_header00006660000000000000000000000064121400033020014473gustar00rootroot0000000000000052 comment=80934de3c482dcafb46b5756e59ebece082b6dc7 SecurityLib-1.0.0/000077500000000000000000000000001214000330200137275ustar00rootroot00000000000000SecurityLib-1.0.0/.gitignore000066400000000000000000000000061214000330200157130ustar00rootroot00000000000000vendorSecurityLib-1.0.0/README.md000066400000000000000000000001601214000330200152030ustar00rootroot00000000000000SecurityLib =========== This is a base set of libraries used in other projects. This isn't useful on its own...SecurityLib-1.0.0/composer.json000066400000000000000000000011561214000330200164540ustar00rootroot00000000000000{ "name": "ircmaxell/security-lib", "type": "library", "version": "1.0.0", "description": "A Base Security Library", "keywords": [], "homepage": "https://github.com/ircmaxell/PHP-SecurityLib", "license": "MIT", "authors": [ { "name": "Anthony Ferrara", "email": "ircmaxell@ircmaxell.com", "homepage": "http://blog.ircmaxell.com" } ], "require-dev": { "mikey179/vfsStream": "1.1.*" }, "require": { "php": ">=5.3.2" }, "autoload": { "psr-0": { "SecurityLib": "lib" } } } SecurityLib-1.0.0/composer.lock000066400000000000000000000021741214000330200164340ustar00rootroot00000000000000{ "hash": "c6ce813d02e1f8bc10f33064ac821a79", "packages": [ ], "packages-dev": [ { "name": "mikey179/vfsStream", "version": "v1.1.0", "source": { "type": "git", "url": "https://github.com/mikey179/vfsStream", "reference": "v1.1.0" }, "dist": { "type": "zip", "url": "https://github.com/mikey179/vfsStream/zipball/v1.1.0", "reference": "v1.1.0", "shasum": "" }, "require": { "php": ">=5.3.0" }, "time": "2012-08-25 05:49:29", "type": "library", "autoload": { "psr-0": { "org\\bovigo\\vfs": "src/main/php" } }, "notification-url": "https://packagist.org/downloads/", "license": [ "BSD" ], "homepage": "http://vfs.bovigo.org/" } ], "aliases": [ ], "minimum-stability": "stable", "stability-flags": [ ] } SecurityLib-1.0.0/lib/000077500000000000000000000000001214000330200144755ustar00rootroot00000000000000SecurityLib-1.0.0/lib/SecurityLib/000077500000000000000000000000001214000330200167335ustar00rootroot00000000000000SecurityLib-1.0.0/lib/SecurityLib/AbstractFactory.php000066400000000000000000000050611214000330200225410ustar00rootroot00000000000000 * @copyright 2011 The Authors * @license http://www.opensource.org/licenses/mit-license.html MIT License * @version Build @@version@@ */ namespace SecurityLib; /** * The base abstract factory used by all PasswordLib factories * * @category PHPPasswordLib * @package Core * @author Anthony Ferrara */ abstract class AbstractFactory { /** * Register a type with the factory by name * * This is an internal method to check if a provided class name implements * an interface, and if it does to append that class to an internal array * by name. * * @param string $type The name of the variable to store the class * @param string $implements The interface to validate against * @param string $name The name of this particular class * @param string $class The fully qualified class name * @param boolean $instantiate Should the class be stored instantiated * * @return void * @throws InvalidArgumentException If class does not implement interface */ protected function registerType( $type, $implements, $name, $class, $instantiate = false ) { $name = strtolower($name); $refl = new \ReflectionClass($class); if (!$refl->implementsInterface($implements)) { $message = sprintf('Class must implement %s', $implements); throw new \InvalidArgumentException($message); } if ($instantiate) { $class = new $class; } $this->{$type}[$name] = $class; } /** * Load a set of classes from a directory into the factory * * @param string $directory The directory to search for classes in * @param string $namespace The namespace prefix for any found classes * @param string $callback The callback with which to register the class * * @return void */ protected function loadFiles($directory, $namespace, $callback) { foreach (new \DirectoryIterator($directory) as $file) { $filename = $file->getBasename(); if ($file->isFile() && substr($filename, -4) == '.php') { $name = substr($filename, 0, -4); $class = $namespace . $name; call_user_func($callback, $name, $class); } } } } SecurityLib-1.0.0/lib/SecurityLib/BaseConverter.php000066400000000000000000000073511214000330200222140ustar00rootroot00000000000000 * @copyright 2011 The Authors * @license http://www.opensource.org/licenses/mit-license.html MIT License * @version Build @@version@@ */ namespace SecurityLib; /** * A Utility class for converting between raw binary strings and a given * list of characters * * @category PHPSecurityLib * @package Core * @author Anthony Ferrara */ class BaseConverter { /** * Convert from a raw binary string to a string of characters * * @param string $string The string to convert from * @param string $characters The list of characters to convert to * * @return string The converted string */ public static function convertFromBinary($string, $characters) { if ($string === '' || empty($characters)) { return ''; } $string = str_split($string); $callback = function($str) { return ord($str); }; $string = array_map($callback, $string); $converted = static::baseConvert($string, 256, strlen($characters)); $callback = function ($num) use ($characters) { return $characters[$num]; }; $ret = implode('', array_map($callback, $converted)); return $ret; } /** * Convert to a raw binary string from a string of characters * * @param string $string The string to convert from * @param string $characters The list of characters to convert to * * @return string The converted string */ public static function convertToBinary($string, $characters) { if (empty($string) || empty($characters)) { return ''; } $string = str_split($string); $callback = function($str) use ($characters) { return strpos($characters, $str); }; $string = array_map($callback, $string); $converted = static::baseConvert($string, strlen($characters), 256); $callback = function ($num) { return chr($num); }; return implode('', array_map($callback, $converted)); } /** * Convert an array of input blocks to another numeric base * * This function was modified from an implementation found on StackOverflow. * Special Thanks to @KeithRandall for supplying the implementation. * * @param int[] $source The source number, as an array * @param int $srcBase The source base as an integer * @param int $dstBase The destination base as an integer * * @see http://codegolf.stackexchange.com/questions/1620/arb/1626#1626 * @return int[] An array of integers in the encoded base */ public static function baseConvert(array $source, $srcBase, $dstBase) { if ($dstBase < 2) { $message = sprintf('Invalid Destination Base: %d', $dstBase); throw new \InvalidArgumentException($message); } $result = array(); $count = count($source); while ($count) { $itMax = $count; $remainder = $count = $loop = 0; while($loop < $itMax) { $dividend = $source[$loop++] + $remainder * $srcBase; $remainder = $dividend % $dstBase; $res = ($dividend - $remainder) / $dstBase; if ($count || $res) { $source[$count++] = $res; } } $result[] = $remainder; } return array_reverse($result); } } SecurityLib-1.0.0/lib/SecurityLib/BigMath.php000066400000000000000000000033471214000330200207660ustar00rootroot00000000000000 * @copyright 2011 The Authors * @license http://www.opensource.org/licenses/mit-license.html MIT License * @version Build @@version@@ */ namespace SecurityLib; /** * A class for arbitrary precision math functions * * @category PHPPasswordLib * @package Core * @author Anthony Ferrara */ abstract class BigMath { /** * Get an instance of the big math class * * This is NOT a singleton. It simply loads the proper strategy * given the current server configuration * * @return \PasswordLib\Core\BigMath A big math instance */ public static function createFromServerConfiguration() { //@codeCoverageIgnoreStart if (extension_loaded('gmp')) { return new \SecurityLib\BigMath\GMP(); } elseif (extension_loaded('bcmath')) { return new \SecurityLib\BigMath\BCMath(); } else { return new \SecurityLib\BigMath\PHPMath(); } //@codeCoverageIgnoreEnd } /** * Add two numbers together * * @param string $left The left argument * @param string $right The right argument * * @return A base-10 string of the sum of the two arguments */ abstract public function add($left, $right); /** * Subtract two numbers * * @param string $left The left argument * @param string $right The right argument * * @return A base-10 string of the difference of the two arguments */ abstract public function subtract($left, $right); }SecurityLib-1.0.0/lib/SecurityLib/BigMath/000077500000000000000000000000001214000330200202465ustar00rootroot00000000000000SecurityLib-1.0.0/lib/SecurityLib/BigMath/BCMath.php000066400000000000000000000023411214000330200220550ustar00rootroot00000000000000 * @copyright 2011 The Authors * @license http://www.opensource.org/licenses/mit-license.html MIT License * @version Build @@version@@ */ namespace SecurityLib\BigMath; /** * A class for arbitrary precision math functions implemented using bcmath * * @category PHPPasswordLib * @package Core * @subpackage BigMath */ class BCMath extends \SecurityLib\BigMath { /** * Add two numbers together * * @param string $left The left argument * @param string $right The right argument * * @return A base-10 string of the sum of the two arguments */ public function add($left, $right) { return bcadd($left, $right, 0); } /** * Subtract two numbers * * @param string $left The left argument * @param string $right The right argument * * @return A base-10 string of the difference of the two arguments */ public function subtract($left, $right) { return bcsub($left, $right); } }SecurityLib-1.0.0/lib/SecurityLib/BigMath/GMP.php000066400000000000000000000023611214000330200214040ustar00rootroot00000000000000 * @copyright 2011 The Authors * @license http://www.opensource.org/licenses/mit-license.html MIT License * @version Build @@version@@ */ namespace SecurityLib\BigMath; /** * A class for arbitrary precision math functions implemented using GMP * * @category PHPPasswordLib * @package Core * @subpackage BigMath */ class GMP extends \SecurityLib\BigMath { /** * Add two numbers together * * @param string $left The left argument * @param string $right The right argument * * @return A base-10 string of the sum of the two arguments */ public function add($left, $right) { return gmp_strval(gmp_add($left, $right)); } /** * Subtract two numbers * * @param string $left The left argument * @param string $right The right argument * * @return A base-10 string of the difference of the two arguments */ public function subtract($left, $right) { return gmp_strval(gmp_sub($left, $right)); } }SecurityLib-1.0.0/lib/SecurityLib/BigMath/PHPMath.php000066400000000000000000000117611214000330200222260ustar00rootroot00000000000000 * @copyright 2011 The Authors * @license http://www.opensource.org/licenses/mit-license.html MIT License * @version Build @@version@@ */ namespace SecurityLib\BigMath; use SecurityLib\BaseConverter; /** * A class for arbitrary precision math functions implemented in PHP * * @category PHPPasswordLib * @package Core * @subpackage BigMath */ class PHPMath extends \SecurityLib\BigMath { /** * Add two numbers together * * @param string $left The left argument * @param string $right The right argument * * @return A base-10 string of the sum of the two arguments */ public function add($left, $right) { if (empty($left)) { return $right; } elseif (empty($right)) { return $left; } $negative = ''; if ($left[0] == '-' && $right[0] == '-') { $negative = '-'; $left = substr($left, 1); $right = substr($right, 1); } elseif ($left[0] == '-') { return $this->subtract($right, substr($left, 1)); } elseif ($right[0] == '-') { return $this->subtract($left, substr($right, 1)); } $left = $this->normalize($left); $right = $this->normalize($right); $result = BaseConverter::convertFromBinary( $this->addBinary($left, $right), '0123456789' ); return $negative . $result; } /** * Subtract two numbers * * @param string $left The left argument * @param string $right The right argument * * @return A base-10 string of the difference of the two arguments */ public function subtract($left, $right) { if (empty($left)) { return $right; } elseif (empty($right)) { return $left; } elseif ($right[0] == '-') { return $this->add($left, substr($right, 1)); } elseif ($left[0] == '-') { return '-' . $this->add(ltrim($left, '-'), $right); } $left = $this->normalize($left); $right = $this->normalize($right); $results = $this->subtractBinary($left, $right); $result = BaseConverter::convertFromBinary($results[1], '0123456789'); return $results[0] . $result; } /** * Add two binary strings together * * @param string $left The left argument * @param string $right The right argument * * @return string The binary result */ protected function addBinary($left, $right) { $len = max(strlen($left), strlen($right)); $left = str_pad($left, $len, chr(0), STR_PAD_LEFT); $right = str_pad($right, $len, chr(0), STR_PAD_LEFT); $result = ''; $carry = 0; for ($i = 0; $i < $len; $i++) { $sum = ord($left[$len - $i - 1]) + ord($right[$len - $i - 1]) + $carry; $result .= chr($sum % 256); $carry = $sum >> 8; } while ($carry) { $result .= chr($carry % 256); $carry >>= 8; } return strrev($result); } /** * Subtract two binary strings using 256's compliment * * @param string $left The left argument * @param string $right The right argument * * @return string The binary result */ protected function subtractBinary($left, $right) { $len = max(strlen($left), strlen($right)); $left = str_pad($left, $len, chr(0), STR_PAD_LEFT); $right = str_pad($right, $len, chr(0), STR_PAD_LEFT); $right = $this->compliment($right); $result = $this->addBinary($left, $right); if (strlen($result) > $len) { // Positive Result $carry = substr($result, 0, -1 * $len); $result = substr($result, strlen($carry)); return array( '', $this->addBinary($result, $carry) ); } return array('-', $this->compliment($result)); } /** * Take the 256 base compliment * * @param string $string The binary string to compliment * * @return string The complimented string */ protected function compliment($string) { $result = ''; $len = strlen($string); for ($i = 0; $i < $len; $i++) { $result .= chr(255 - ord($string[$i])); } return $result; } /** * Transform a string number into a binary string using base autodetection * * @param string $string The string to transform * * @return string The binary transformed number */ protected function normalize($string) { return BaseConverter::convertToBinary( $string, '0123456789' ); } }SecurityLib-1.0.0/lib/SecurityLib/Enum.php000066400000000000000000000063301214000330200203520ustar00rootroot00000000000000 * @copyright 2011 The Authors * @license http://www.opensource.org/licenses/mit-license.html MIT License * @version Build @@version@@ */ namespace SecurityLib; use \ReflectionObject; /** * The Enum base class for Enum functionality * * This is based off of the SplEnum class implementation (which is only available * as a PECL extension in 5.3) * * @see http://www.php.net/manual/en/class.splenum.php * @category PHPPasswordLib * @package Core * @author Anthony Ferrara */ abstract class Enum { /** * A default value of null is provided. Override this to set your own default */ const __DEFAULT = null; /** * @var string The name of the constant this instance is using */ protected $name = ''; /** * @var scalar The value of the constant this instance is using. */ protected $value = ''; /** * Creates a new value of the Enum type * * @param mixed $value The value this instance represents * @param boolean $strict Not Implemented at this time * * @return void * @throws UnexpectedValueException If the value is not a constant */ public function __construct($value = null, $strict = false) { if (is_null($value)) { $value = static::__DEFAULT; } $validValues = $this->getConstList(); $this->name = array_search($value, $validValues); if (!$this->name) { throw new \UnexpectedValueException( 'Value not a const in enum ' . get_class($this) ); } $this->value = $value; } /** * Cast the current object to a string and return its value * * @return mixed the current value of the instance */ public function __toString() { return (string) $this->value; } /** * Compare two enums using numeric comparison * * @param Enum $arg The enum to compare this instance to * * @return int 0 if same, 1 if the argument is greater, -1 else */ public function compare(Enum $arg) { if ($this->value == $arg->value) { return 0; } elseif ($this->value > $arg->value) { return -1; } else { return 1; } } /** * Returns all constants (including values) as an associative array * * @param boolean $include_default Include the __default magic value? * * @return array All of the constants found against this instance */ public function getConstList($include_default = false) { static $constCache = array(); $class = get_class($this); if (!isset($constCache[$class])) { $reflector = new ReflectionObject($this); $constCache[$class] = $reflector->getConstants(); } if (!$include_default) { $constants = $constCache[$class]; unset($constants['__DEFAULT']); return $constants; } return $constCache[$class]; } }SecurityLib-1.0.0/lib/SecurityLib/Hash.php000066400000000000000000000244411214000330200203340ustar00rootroot00000000000000 * @copyright 2011 The Authors * @license http://www.opensource.org/licenses/mit-license.html MIT License * @version Build @@version@@ */ namespace SecurityLib; /** * A hash utility data mapper class * * This class's purpose is to store information about hash algorithms that is * otherwise unavailable during runtime. Some information is available (such * as the output size), but is included anyway for performance and completeness * reasons. * * PHP version 5.3 * * @category PHPPasswordLib * @package Hash * @author Anthony Ferrara */ class Hash { /** * This array contains information about each hash function available to PHP * at the present time. Block sizes are not available from functions, so they * must be hard coded. * * The "secure" indicates the strength of the hash and whether or not any known * cryptographic attacks exist for the hash function. This will only apply when * using the hash functions for situations that require cryptographic strength * such as message signing. For other uses the insecure ones can have valid * uses. * * @var array An array of information about each supported hash function */ protected static $hashInfo = array( 'md2' => array( 'HashSize' => 128, 'BlockSize' => 128, 'secure' => false, ), 'md4' => array( 'HashSize' => 128, 'BlockSize' => 512, 'secure' => false, ), 'md5' => array( 'HashSize' => 128, 'BlockSize' => 512, 'secure' => false, ), 'sha1' => array( 'HashSize' => 160, 'BlockSize' => 512, 'secure' => false, ), 'sha224' => array( 'HashSize' => 224, 'BlockSize' => 512, 'secure' => true, ), 'sha256' => array( 'HashSize' => 256, 'BlockSize' => 512, 'secure' => true, ), 'sha384' => array( 'HashSize' => 384, 'BlockSize' => 1024, 'secure' => true, ), 'sha512' => array( 'HashSize' => 512, 'BlockSize' => 1024, 'secure' => true, ), 'ripemd128' => array( 'HashSize' => 128, 'BlockSize' => 512, 'secure' => true, ), 'ripemd160' => array( 'HashSize' => 160, 'BlockSize' => 512, 'secure' => true, ), 'ripemd256' => array( 'HashSize' => 256, 'BlockSize' => 512, 'secure' => true, ), 'ripemd320' => array( 'HashSize' => 320, 'BlockSize' => 512, 'secure' => true, ), 'whirlpool' => array( 'HashSize' => 512, 'BlockSize' => 512, 'secure' => true, ), 'tiger128,3' => array( 'HashSize' => 128, 'BlockSize' => 512, 'secure' => true, ), 'tiger160,3' => array( 'HashSize' => 160, 'BlockSize' => 512, 'secure' => true, ), 'tiger192,3' => array( 'HashSize' => 192, 'BlockSize' => 512, 'secure' => true, ), 'tiger128,4' => array( 'HashSize' => 128, 'BlockSize' => 512, 'secure' => true, ), 'tiger160,4' => array( 'HashSize' => 160, 'BlockSize' => 512, 'secure' => true, ), 'tiger192,4' => array( 'HashSize' => 192, 'BlockSize' => 512, 'secure' => true, ), 'snefru' => array( 'HashSize' => 256, 'BlockSize' => 512, 'secure' => false, ), 'snefru256' => array( 'HashSize' => 256, 'BlockSize' => 512, 'secure' => false, ), 'gost' => array( 'HashSize' => 256, 'BlockSize' => 256, 'secure' => false, ), 'adler32' => array( 'HashSize' => 32, 'BlockSize' => 16, 'secure' => false, ), 'crc32' => array( 'HashSize' => 32, 'BlockSize' => 32, 'secure' => false, ), 'crc32b' => array( 'HashSize' => 32, 'BlockSize' => 32, 'secure' => false, ), 'salsa10' => array( 'HashSize' => 512, 'BlockSize' => 512, 'secure' => true, ), 'salsa20' => array( 'HashSize' => 512, 'BlockSize' => 512, 'secure' => true, ), 'haval128,3' => array( 'HashSize' => 128, 'BlockSize' => 1024, 'secure' => false, ), 'haval160,3' => array( 'HashSize' => 160, 'BlockSize' => 1024, 'secure' => false, ), 'haval192,3' => array( 'HashSize' => 192, 'BlockSize' => 1024, 'secure' => false, ), 'haval224,3' => array( 'HashSize' => 224, 'BlockSize' => 1024, 'secure' => false, ), 'haval256,3' => array( 'HashSize' => 256, 'BlockSize' => 1024, 'secure' => false, ), 'haval128,4' => array( 'HashSize' => 128, 'BlockSize' => 1024, 'secure' => false, ), 'haval160,4' => array( 'HashSize' => 160, 'BlockSize' => 1024, 'secure' => false, ), 'haval192,4' => array( 'HashSize' => 192, 'BlockSize' => 1024, 'secure' => false, ), 'haval224,4' => array( 'HashSize' => 224, 'BlockSize' => 1024, 'secure' => false, ), 'haval256,4' => array( 'HashSize' => 256, 'BlockSize' => 1024, 'secure' => false, ), 'haval128,5' => array( 'HashSize' => 128, 'BlockSize' => 1024, 'secure' => false, ), 'haval160,5' => array( 'HashSize' => 160, 'BlockSize' => 1024, 'secure' => false, ), 'haval192,5' => array( 'HashSize' => 192, 'BlockSize' => 1024, 'secure' => false, ), 'haval224,5' => array( 'HashSize' => 224, 'BlockSize' => 1024, 'secure' => false, ), 'haval256,5' => array( 'HashSize' => 256, 'BlockSize' => 1024, 'secure' => false, ), 'joaat' => array( 'HashSize' => 32, 'BlockSize' => 64, 'secure' => false, ), 'fnv132' => array( 'HashSize' => 32, 'BlockSize' => 32, 'secure' => false, ), 'fnv164' => array( 'HashSize' => 64, 'BlockSize' => 64, 'secure' => false, ), ); /** * Get the block size of the specified function in bytes * * @param string $hash The hash function to look up * * @return int The number of bytes in the block function */ public static function getBlockSize($hash) { return static::getBlockSizeInBits($hash) / 8; } /** * Get the block size of the specified function in bits * * @param string $hash The hash function to look up * * @return int The number of bits in the block function */ public static function getBlockSizeInBits($hash) { if (isset(static::$hashInfo[$hash]['BlockSize'])) { return static::$hashInfo[$hash]['BlockSize']; } return 0; } /** * Get the output size of the specified function in bytes * * @param string $hash The hash function to look up * * @return int The number of bytes outputted by the hash function */ public static function getHashSize($hash) { return static::getHashSizeInBits($hash) / 8; } /** * Get the output size of the specified function in bits * * @param string $hash The hash function to look up * * @return int The number of bits outputted by the hash function */ public static function getHashSizeInBits($hash) { if (isset(static::$hashInfo[$hash]['HashSize'])) { return static::$hashInfo[$hash]['HashSize']; } return 0; } /** * Check to see if the hash function specified is available * * @param string $hash The hash function to look up * * @return boolean If the hash function is available in this version of PHP */ public static function isAvailable($hash) { return in_array($hash, hash_algos()); } /** * Check to see if the specified hash function is secure enough for * cryptographic uses * * The "secure" indicates the strength of the hash and whether or not any known * cryptographic attacks exist for the hash function. This will only apply when * using the hash functions for situations that require cryptographic strength * such as message signing. For other uses the insecure ones can have valid * uses. * * @param string $hash The hash function to look up * * @return bolean If the function is secure */ public static function isSecure($hash) { if (isset(static::$hashInfo[$hash])) { return static::$hashInfo[$hash]['secure']; } return false; } }SecurityLib-1.0.0/lib/SecurityLib/Strength.php000066400000000000000000000027641214000330200212530ustar00rootroot00000000000000 * @copyright 2011 The Authors * @license http://www.opensource.org/licenses/mit-license.html MIT License * @version Build @@version@@ */ namespace SecurityLib; /** * The strength FlyweightEnum class * * All mixing strategies must extend this class * * @category PHPPasswordLib * @package Core * @author Anthony Ferrara */ class Strength extends Enum { /** * We provide a default value of VeryLow so that we don't accidentally over * state the strength if we forget to pass in a value... */ const __DEFAULT = self::VERYLOW; /** * This represents Non-Cryptographic strengths. It should not be used any time * that security or confidentiality is at stake */ const VERYLOW = 1; /** * This represents the bottom line of Cryptographic strengths. It may be used * for low security uses where some strength is required. */ const LOW = 3; /** * This is the general purpose Cryptographical strength. It should be suitable * for all uses except the most sensitive. */ const MEDIUM = 5; /** * This is the highest strength available. It should not be used unless the * high strength is needed, due to hardware constraints (and entropy * limitations). */ const HIGH = 7; } SecurityLib-1.0.0/lib/SecurityLib/composer.json000066400000000000000000000004541214000330200214600ustar00rootroot00000000000000{ "name": "SecurityLib/Core", "description": "Common implementations", "keywords": ["security"], "license": "MIT", "require": { "php": ">=5.3" }, "autoload": { "psr-0": { "SecurityLib\SecurityLib": "" } }, "target-dir": "SecurityLib\SecurityLib" } SecurityLib-1.0.0/phpunit.xml.dist000066400000000000000000000016531214000330200171070ustar00rootroot00000000000000 test/Unit lib/ SecurityLib-1.0.0/test/000077500000000000000000000000001214000330200147065ustar00rootroot00000000000000SecurityLib-1.0.0/test/Mocks/000077500000000000000000000000001214000330200157625ustar00rootroot00000000000000SecurityLib-1.0.0/test/Mocks/AbstractMock.php000066400000000000000000000020001214000330200210400ustar00rootroot00000000000000 * @copyright 2011 The Authors * @license http://opensource.org/licenses/bsd-license.php New BSD License * @license http://www.gnu.org/licenses/lgpl-2.1.html LGPL v 2.1 */ namespace PasswordLibTest\Mocks; /** * The interface that all hash implementations must implement * * @category PHPPasswordLib * @package Hash * @author Anthony Ferrara */ class AbstractMock { protected $callbacks = array(); public static function init() {} public function __construct(array $callbacks = array()) { $this->callbacks = $callbacks; } public function __call($name, array $args = array()) { if (isset($this->callbacks[$name])) { return call_user_func_array($this->callbacks[$name], $args); } return null; } } SecurityLib-1.0.0/test/Mocks/Enum.php000066400000000000000000000013341214000330200174000ustar00rootroot00000000000000 * @copyright 2011 The Authors * @license http://opensource.org/licenses/bsd-license.php New BSD License * @license http://www.gnu.org/licenses/lgpl-2.1.html LGPL v 2.1 */ namespace SecurityLibTest\Mocks; /** * The interface that all hash implementations must implement * * @category PHPSecurityLib * @package Hash * @author Anthony Ferrara */ class Enum extends \SecurityLib\Enum { const Value1 = 1; const Value2 = 2; const Value3 = 3; const Value4 = 4; } SecurityLib-1.0.0/test/Mocks/Factory.php000066400000000000000000000024421214000330200201040ustar00rootroot00000000000000 * @copyright 2011 The Authors * @license http://opensource.org/licenses/bsd-license.php New BSD License * @license http://www.gnu.org/licenses/lgpl-2.1.html LGPL v 2.1 */ namespace SecurityLibTest\Mocks; /** * The interface that all hash implementations must implement * * @category PHPSecurityLib * @package Hash * @author Anthony Ferrara */ class Factory extends \SecurityLib\AbstractFactory { protected $callbacks = array(); public static function init() {} public function __construct(array $callbacks = array()) { $this->callbacks = $callbacks; } public function __call($name, array $args = array()) { if (isset($this->callbacks[$name])) { return call_user_func_array($this->callbacks[$name], $args); } return null; } public function registerType($a1, $a2, $a3, $a4, $a5 = false) { return parent::registerType($a1, $a2, $a3, $a4, $a5); } public function loadFiles($dir, $name, $method) { return parent::loadFiles($dir, $name, $method); } } SecurityLib-1.0.0/test/Mocks/Strength.php000066400000000000000000000013001214000330200202630ustar00rootroot00000000000000 * @copyright 2011 The Authors * @license http://opensource.org/licenses/bsd-license.php New BSD License * @license http://www.gnu.org/licenses/lgpl-2.1.html LGPL v 2.1 */ namespace SecurityLibTest\Mocks; /** * The interface that all hash implementations must implement * * @category PHPSecurityLib * @package Hash * @author Anthony Ferrara */ class Strength extends \SecurityLib\Strength { const MEDIUMLOW = 4; const SUPERHIGH = 999; } SecurityLib-1.0.0/test/Unit/000077500000000000000000000000001214000330200156255ustar00rootroot00000000000000SecurityLib-1.0.0/test/Unit/Core/000077500000000000000000000000001214000330200165155ustar00rootroot00000000000000SecurityLib-1.0.0/test/Unit/Core/AbstractFactoryTest.php000066400000000000000000000036561214000330200231730ustar00rootroot00000000000000at($root); $af = vfsStream::newDirectory('AbstractFactory')->at($core); // Create Files vfsStream::newFile('test.php')->at($af); vfsStream::newFile('Some234Foo234Bar98Name.php')->at($af); vfsStream::newFile('Invalid.csv')->at($af); vfsStream::newFile('badlocation.php')->at($core); } /** * @covers SecurityLib\AbstractFactory::registerType */ public function testRegisterType() { $factory = new Factory; $factory->registerType('test', 'iteratoraggregate', 'foo', 'ArrayObject', false); } /** * @covers SecurityLib\AbstractFactory::registerType * @expectedException InvalidArgumentException */ public function testRegisterTypeFail() { $factory = new Factory; $factory->registerType('test', 'iterator', 'foo', 'ArrayObject', false); } /** * @covers SecurityLib\AbstractFactory::registerType */ public function testRegisterTypeInstantiate() { $factory = new Factory; $factory->registerType('test', 'iteratoraggregate', 'foo', 'ArrayObject', true); } public function testLoadFiles() { $dir = vfsStream::url('SecurityLibTest/Core/AbstractFactory'); $result = array(); $callback = function($name, $class) use (&$result) { $result[$name] = $class; }; $factory = new Factory(); $factory->loadFiles($dir, 'foo\\', $callback); $expect = array( 'test' => 'foo\\test', 'Some234Foo234Bar98Name' => 'foo\\Some234Foo234Bar98Name' ); $this->assertEquals($expect, $result); } } SecurityLib-1.0.0/test/Unit/Core/BaseConverterTest.php000066400000000000000000000042461214000330200226360ustar00rootroot00000000000000assertEquals($expect, $result); } /** * @covers SecurityLib\BaseConverter::convertToBinary * @covers SecurityLib\BaseConverter::baseConvert * @dataProvider provideConvertFromBinary */ public function testConvertToBinary($expect, $from, $str) { $result = BaseConverter::convertToBinary($str, $from); $result = ltrim($result, chr(0)); $expect = ltrim($expect, chr(0)); $this->assertEquals($expect, $result); } /** * @covers SecurityLib\BaseConverter::convertToBinary * @covers SecurityLib\BaseConverter::convertFromBinary * @covers SecurityLib\BaseConverter::baseConvert * @dataProvider provideConvertToFromBinary */ public function testConvertToAndFromBinary($str, $from) { return false; $result1 = BaseConverter::convertFromBinary($str, $from); $result = BaseConverter::convertToBinary($result1, $from); $this->assertEquals($str, $result); } /** * @covers SecurityLib\BaseConverter::baseConvert * @expectedException InvalidArgumentException */ public function testBaseConvertFailure() { BaseConverter::baseConvert(array(1), 1, 1); } } SecurityLib-1.0.0/test/Unit/Core/BigMath/000077500000000000000000000000001214000330200200305ustar00rootroot00000000000000SecurityLib-1.0.0/test/Unit/Core/BigMath/BCMathTest.php000066400000000000000000000014561214000330200225050ustar00rootroot00000000000000markTestSkipped('BCMath is not loaded'); } } /** * @dataProvider provideAddTest */ public function testAdd($left, $right, $expected) { $obj = new \SecurityLib\BigMath\BCMath; $this->assertEquals($expected, $obj->add($left, $right)); } /** * @dataProvider provideSubtractTest */ public function testSubtract($left, $right, $expected) { $obj = new \SecurityLib\BigMath\BCMath; $this->assertEquals($expected, $obj->subtract($left, $right)); } }SecurityLib-1.0.0/test/Unit/Core/BigMath/GMPTest.php000066400000000000000000000014461214000330200220310ustar00rootroot00000000000000markTestSkipped('BCMath is not loaded'); } } /** * @dataProvider provideAddTest */ public function testAdd($left, $right, $expected) { $obj = new \SecurityLib\BigMath\GMP; $this->assertEquals($expected, $obj->add($left, $right)); } /** * @dataProvider provideSubtractTest */ public function testSubtract($left, $right, $expected) { $obj = new \SecurityLib\BigMath\GMP; $this->assertEquals($expected, $obj->subtract($left, $right)); } }SecurityLib-1.0.0/test/Unit/Core/BigMath/PHPMathTest.php000066400000000000000000000012301214000330200226360ustar00rootroot00000000000000assertEquals($expected, $obj->add($left, $right)); } /** * @dataProvider provideSubtractTest */ public function testSubtract($left, $right, $expected) { $obj = new \SecurityLib\BigMath\PHPMath; $this->assertEquals($expected, $obj->subtract($left, $right)); } }SecurityLib-1.0.0/test/Unit/Core/BigMathTest.php000066400000000000000000000027561214000330200214130ustar00rootroot00000000000000assertEquals('SecurityLib\\BigMath\\BCMath', get_class($instance)); } elseif (extension_loaded('gmp')) { $this->assertEquals('SecurityLib\\BigMath\\GMP', get_class($instance)); } else { $this->assertEquals('SecurityLib\\BigMath\\PHPMath', get_class($instance)); } } }SecurityLib-1.0.0/test/Unit/Core/EnumTest.php000066400000000000000000000032271214000330200207760ustar00rootroot00000000000000assertTrue($obj instanceof \SecurityLib\Enum); } public function testToString() { $obj = new Enum(Enum::Value3); $this->assertEquals('3', (string) $obj); } /** * @covers SecurityLib\Core\Enum::compare * @dataProvider provideTestCompare */ public function testCompare(Enum $from, Enum $to, $expected) { $this->assertEquals($expected, $from->compare($to)); } public function testGetConstList() { $obj = new Enum(Enum::Value3); $const = $obj->getConstList(); $this->assertEquals(array( 'Value1' => 1, 'Value2' => 2, 'Value3' => 3, 'Value4' => 4, ), $const); } public function testGetConstListWithDefault() { $obj = new Enum(Enum::Value3); $const = $obj->getConstList(true); $this->assertEquals(array( '__DEFAULT' => null, 'Value1' => 1, 'Value2' => 2, 'Value3' => 3, 'Value4' => 4, ), $const); } }SecurityLib-1.0.0/test/Unit/Core/StrengthTest.php000066400000000000000000000016251214000330200216700ustar00rootroot00000000000000assertTrue($obj instanceof \SecurityLib\Strength); $this->assertTrue($obj instanceof \SecurityLib\Enum); } public function testGetConstList() { $obj = new Strength(); $const = $obj->getConstList(); $this->assertEquals(array( 'VERYLOW' => 1, 'LOW' => 3, 'MEDIUM' => 5, 'HIGH' => 7, ), $const); } public function testGetConstListWithDefault() { $obj = new Strength(); $const = $obj->getConstList(true); $this->assertEquals(array( '__DEFAULT' => 1, 'VERYLOW' => 1, 'LOW' => 3, 'MEDIUM' => 5, 'HIGH' => 7, ), $const); } }SecurityLib-1.0.0/test/bootstrap.php000066400000000000000000000024521214000330200174370ustar00rootroot00000000000000 * @copyright 2011 The Authors * @license http://opensource.org/licenses/bsd-license.php New BSD License * @license http://www.gnu.org/licenses/lgpl-2.1.html LGPL v 2.1 */ namespace SecurityLibTest; ini_set('memory_limit', '1G'); /** * The simple autoloader for the PasswordLibTest libraries. * * This does not use the PRS-0 standards due to the namespace prefix and directory * structure * * @param string $class The class name to load * * @return void */ spl_autoload_register(function ($class) { $nslen = strlen(__NAMESPACE__); if (substr($class, 0, $nslen) != __NAMESPACE__) { //Only autoload libraries from this package return; } $path = substr(str_replace('\\', '/', $class), $nslen); $path = __DIR__ . $path . '.php'; if (file_exists($path)) { require $path; } }); define('PATH_ROOT', dirname(__DIR__)); require_once dirname(__DIR__) . '/vendor/autoload.php';