Skip to content

Commit

Permalink
A bunch more updates, install composer, fix build
Browse files Browse the repository at this point in the history
  • Loading branch information
ircmaxell committed Apr 14, 2012
1 parent 6351c18 commit 57d8d83
Show file tree
Hide file tree
Showing 80 changed files with 496 additions and 845 deletions.
107 changes: 33 additions & 74 deletions README.markdown
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#PHP-CryptLib
#PHP-PasswordLib

##Version

Expand All @@ -8,7 +8,7 @@ As this software is **ALPHA**, **Use at your own risk**!

#About

PHP-CryptLib aims to be an all-inclusive cryptographic library for all cryptographic needs. It is meant to be easy to install and use, yet extensible and powerful enough for even the most experienced developer.
PHP-PasswordLib aims to be an all-inclusive cryptographic library for all cryptographic needs. It is meant to be easy to install and use, yet extensible and powerful enough for even the most experienced developer.

##Design Goals

Expand All @@ -26,7 +26,7 @@ PHP-CryptLib aims to be an all-inclusive cryptographic library for all cryptogra

- **Easy To Install**

PHP-CryptLib will support two install methods. The first method is a pear based installer. The second is a single file PHAR archive.
PHP-PasswordLib will support three install methods. The first method is a pear based installer. The second is a single file PHAR archive. The third is support via Composer.

- **Easy To Use**

Expand All @@ -40,137 +40,96 @@ PHP-CryptLib aims to be an all-inclusive cryptographic library for all cryptogra

##Optional Autoloading

If you include CryptLib via a PHAR package, it will automatically autoload all of the classes for you, no extra step necessary. Simply:
If you include PasswordLib via a PHAR package, it will automatically autoload all of the classes for you, no extra step necessary. Simply:

require 'path/to/CryptLib.phar';
require 'path/to/PasswordLib.phar';

If you include CryptLib via a filesystem install, you can use the internal autoloader by either loading the bootstrap.php file, or loading the CryptLib.php file
If you include PasswordLib via a filesystem install, you can use the internal autoloader by either loading the bootstrap.php file, or loading the PasswordLib.php file

require_once 'path/to/CryptLib/bootstrap.php
require_once 'path/to/PasswordLib/bootstrap.php

or

require_once 'path/to/CryptLib/CryptLib.php
require_once 'path/to/PasswordLib/PasswordLib.php

You can also use any [PSR-0] [3] autoloader. CryptLib will automatically detect if an autoloader is setup for its namespace, and will not declare its own if it finds one (it does this by testing if the class CryptLib\Core\AutoLoader can be found. If so, that means that an autoloader was declared already. If not, it loads the core implementation).
You can also use any [PSR-0] [3] autoloader. PasswordLib will automatically detect if an autoloader is setup for its namespace, and will not declare its own if it finds one (it does this by testing if the class PasswordLib\Core\AutoLoader can be found. If so, that means that an autoloader was declared already. If not, it loads the core implementation).

$classLoader = new SplClassLoader('CryptLib', 'path/to/');
$classLoader = new SplClassLoader('PasswordLib', 'path/to/');
$classLoader->register();

Note that the path you supply is the directory which contains the *CryptLib* directory. Not the CryptLib directory itself.
Note that the path you supply is the directory which contains the *PasswordLib* directory. Not the PasswordLib directory itself.

##Secure Random Number/String Generation

PHP-CryptLib implements a method specified in [RFC 4086 - Randomness Requirements for Security] [2]. Basically, it generates randomness from a number of pseudo random sources, and "mixes" them together to get better quality random data out. When you specify the "strength" of random generator, you are actually telling the system which sources you would like to use. The higher the strength, the slower and potentially more fragile the source it will use.
PHP-PasswordLib implements a method specified in [RFC 4086 - Randomness Requirements for Security] [2]. Basically, it generates randomness from a number of pseudo random sources, and "mixes" them together to get better quality random data out. When you specify the "strength" of random generator, you are actually telling the system which sources you would like to use. The higher the strength, the slower and potentially more fragile the source it will use.

The mixing function is also dependent upon the strength required. For non-cryptographic numbers, a simple XOR mixing function is used (for speed). As strength requirements increase, it will use a SHA512 based mixing function, then a DES based mixing function and finally an AES-128 based mixing function at "High" strength.

And all of this is hidden behind a simple API.

To generate user-readable strings, you can use the CryptLib class (which generates medium strength numbers by default):
To generate user-readable strings, you can use the PasswordLib class (which generates medium strength numbers by default):

$crypt = new CryptLib\CryptLib;
$crypt = new PasswordLib\PasswordLib;
$token = $crypt->getRandomToken(16);

Or you can use the core generator to get more control:

$factory = new CryptLib\Random\Factory;
$factory = new PasswordLib\Random\Factory;
$generator = $factory->getHighStrengthGenerator();
$token = $generator->generateString(16);

To generate salts, simple use CryptLib::getRandomString() or Generator::generate()
To generate salts, simple use PasswordLib::getRandomString() or Generator::generate()

##Password Hashing And Validation

A number of password hashing algorithms are supported. When creating a new hash, the algorithm is chosen via a prefix (a CRYPT() style prefix). The library will do the rest (salt generation, etc):

$crypt = new CryptLib\CryptLib;
$crypt = new PasswordLib\PasswordLib;
$hash = $crypt->createPasswordHash($password, '$2a$'); // Blowfish
$hash = $crypt->createPasswordHash($password, '$S$'); // Drupal

When validating password hashes, where possible, the library will actually auto-detect the algorithm used from the format and verify. That means it's as simple as:

$crypt = new CryptLib\CryptLib;
$crypt = new PasswordLib\PasswordLib;
if (!$crypt->verifyPasswordHash($password, $hash)) {
//Invalid Password!
}

You can bypass the auto-detection and manually verify:

$hasher = new CryptLib\Password\Implementation\Joomla;
$hasher = new PasswordLib\Password\Implementation\Joomla;
$hash = $hasher->create($password);
if (!$hasher->verify($password, $hash)) {
//Invalid Hash!
}

#Specifications

- Supported Portable Block Ciphers
- **aes-128**
- **aes-192**
- **aes-256**
- **rijndael-128**
- **rijndael-160**
- **rijndael-192**
- **rijndael-224**
- **rijndael-256**
- **des**
- **tripledes**

- Supported Portable Cipher Modes Of Operation
- **CBC** - Encryption (Cipher Block Chaining)
- **CCM** - Encryption and Authentication (Counter Cipher Block Chaining)
- **CFB** - Encryption (Cipher FeedBack)
- **CTR** - Encryption (Counter)
- **ECB** - Encryption (Electronic CodeBook)
- **NOFB** - Encryption (Output FeedBack - Variable Block Size)

- Supported Packing Modes
- **ANSI-923**
- **ISO-10126**
- **PKCS-7**
- **Zeros** - (Null Padding)

- Supported Key Derivation Functions
- **KDF1**
- **KDF2**
- **KDF3**

- Supported Password Based Key Derivation Functions
- **BCrypt**
- **PBKDF1**
- **PBKDF2**
- **SHA256** - (crypt()'s implementation)
- **SHA512** - (crypt()'s implementation)
- **Schneier** (a PBKDF derivative)

- Supported MAC Functions (Message Authentication Code)
- **CMAC** (Cipher MAC)
- **HMAC** (Hash MAC)

- Supported Password Storage Functions
- **APR1** - Apache's internal password function
- **Blowfish** - BCrypt
- **Drupal** - Drupal's SHA512 based algorithm
- **Hash** - Raw md5, sha1, sha256 and sha512 detected by length
- **Joomla** - Joomla's MD5 based algorithm
- **PBKDF** - A PBKDF implementation (which supports any supported password based key derivation)
- **PHPASS** - An implementation of the portable hash from the PHPASS library
- **PHPBB** - PHPBB's MD5 based algorithm
- Supported Password Storage Functions
- **APR1** - Apache's internal password function
- **Blowfish** - BCrypt
- **Crypt** - Crypt DES hashing
- **Drupal** - Drupal's SHA512 based algorithm
- **Hash** - Raw md5, sha1, sha256 and sha512 detected by length
- **Joomla** - Joomla's MD5 based algorithm
- **Crypt MD5** - Support for Crypt's MD5 algorithm
- **PBKDF** - A PBKDF implementation (which supports any supported password based key derivation)
- **PHPASS** - An implementation of the portable hash from the PHPASS library
- **PHPBB** - PHPBB's MD5 based algorithm
- **Crypt SHA256** - Crypt's SHA256 algorithm
- **Crypt SHA512** - Crypt's SHA512 algorithm

- Supported Random Number Sources
- **CAPICOM** - A COM object method call available on Windows systems
- **MTRand** - Generation based upon the mt_rand() functions
- **MicroTime** - A low entropy source based upon the server's microtime
- **OpenSSL** - Generation from the OpenSSL library (if available)
- **Rand** - A low entropy source based upon rand()
- **Random** - Generation from the system's /dev/random source
- **URandom** - Generation from the system's /dev/urandom source
- **UniqID** - A low entropy source based upon uniqid()

#Library Dependencies:

The only dependency PHP-CryptLib has to use as a library is the PHP version. It is made to be completely indepedent of extensions, implementing functionality natively where possible.
The only dependency PHP-PasswordLib has to use as a library is the PHP version. It is made to be completely indepedent of extensions, implementing functionality natively where possible.

##Required

Expand Down
2 changes: 1 addition & 1 deletion build/build.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<project name="PHP-CryptLib" default="build-lite" basedir="../">
<project name="PHP-PasswordLib" default="build-lite" basedir="../">
<property file="build/build.properties" />
<import file="${path.build}/phing/document.xml" />
<import file="${path.build}/phing/package.xml" />
Expand Down
10 changes: 5 additions & 5 deletions build/phar.stub.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,22 @@
*
* PHP version 5.3
*
* @category PHPCryptLib
* @category PHPPasswordLib
* @package Core
* @author Anthony Ferrara <ircmaxell@ircmaxell.com>
* @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 CryptLib;
namespace PasswordLib;

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

require_once 'phar://CryptLib.phar/CryptLib/Core/AutoLoader.php';
require_once 'phar://PasswordLib.phar/PasswordLib/Core/AutoLoader.php';

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

$autoloader->register();

Expand Down
4 changes: 2 additions & 2 deletions build/phing/document.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@
<exec
passthru="true"
command="phpuml
&quot;${path.lib}/CryptLib&quot;
&quot;${path.lib}/PasswordLib&quot;
-f htmlnew
-n CryptLib
-n PasswordLib
--no-deployment-view
-o &quot;${path.results}/api&quot;"
/>
Expand Down
24 changes: 12 additions & 12 deletions build/phing/package.xml
Original file line number Diff line number Diff line change
Expand Up @@ -36,36 +36,36 @@
</target>

<target name="packageFiles">
<zip destfile="${path.package}/CryptLib.zip">
<zip destfile="${path.package}/PasswordLib.zip">
<fileset dir="${path.results}/lib">
<include name="**/**" />
</fileset>
</zip>
<filehash file="${path.package}/CryptLib.zip" hashtype="0" propertyname="filehash" />
<echo message="${filehash}" file="${path.package}/CryptLib.zip.md5" />
<filehash file="${path.package}/CryptLib.zip" hashtype="1" propertyname="filehash" />
<echo message="${filehash}" file="${path.package}/CryptLib.zip.sha1" />
<filehash file="${path.package}/PasswordLib.zip" hashtype="0" propertyname="filehash" />
<echo message="${filehash}" file="${path.package}/PasswordLib.zip.md5" />
<filehash file="${path.package}/PasswordLib.zip" hashtype="1" propertyname="filehash" />
<echo message="${filehash}" file="${path.package}/PasswordLib.zip.sha1" />

<tar destfile="${path.package}/CryptLib.tar.gz" compression="gzip">
<tar destfile="${path.package}/PasswordLib.tar.gz" compression="gzip">
<fileset dir="${path.results}/lib">
<include name="**/**" />
</fileset>
</tar>
<tar destfile="${path.package}/CryptLib.tar.bz2" compression="bzip2">
<tar destfile="${path.package}/PasswordLib.tar.bz2" compression="bzip2">
<fileset dir="${path.results}/lib">
<include name="**/**" />
</fileset>
</tar>
<phingcall target="writeFileHashes">
<property name="filename" value="${path.package}/CryptLib.tar.gz" />
<property name="filename" value="${path.package}/PasswordLib.tar.gz" />
</phingcall>
<phingcall target="writeFileHashes">
<property name="filename" value="${path.package}/CryptLib.tar.bz2" />
<property name="filename" value="${path.package}/PasswordLib.tar.bz2" />
</phingcall>
</target>

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

<target name="packagePhar">
<pharpackage
destfile="${path.package}/CryptLib.phar"
destfile="${path.package}/PasswordLib.phar"
basedir="${path.results}/lib"
stub="${path.build}/phar.stub.php"
signature="sha1"
Expand All @@ -100,7 +100,7 @@
</metadata>
</pharpackage>
<phingcall target="writeFileHashes">
<property name="filename" value="${path.package}/CryptLib.phar" />
<property name="filename" value="${path.package}/PasswordLib.phar" />
</phingcall>
</target>

Expand Down
8 changes: 4 additions & 4 deletions build/phing/quality.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
--report=summary
--report-checkstyle=&quot;${path.logs}/checkstyle.xml&quot;
--standard=&quot;${path.build}/phpcs/ruleset.xml&quot;
&quot;${path.lib}/CryptLib&quot;"
&quot;${path.lib}/PasswordLib&quot;"
passthru="true"
checkreturn="true" />
</target>
Expand All @@ -43,14 +43,14 @@
<exec
command="phploc
--log-csv &quot;${path.logs}/phploc.csv&quot;
&quot;${path.lib}/CryptLib&quot;"
&quot;${path.lib}/PasswordLib&quot;"
/>
</target>

<target name="pmd">
<mkdir dir="${path.results}/logs" />
<phpmd rulesets="${path.build}/phpmd/ruleset.xml">
<fileset dir="${path.lib}/CryptLib">
<fileset dir="${path.lib}/PasswordLib">
<include name="**/*.php" />
</fileset>
<formatter type="xml" outfile="${path.logs}/pmd.xml" />
Expand All @@ -60,7 +60,7 @@

<target name="pdepend">
<mkdir dir="${path.results}/logs" />
<phpdepend file="${path.lib}/CryptLib">
<phpdepend file="${path.lib}/PasswordLib">
<logger type="phpunit-xml" outfile="${path.logs}/metrics.xml" />
<logger type="jdepend-xml" outfile="${path.logs}/jdepend.xml" />
<logger type="jdepend-chart" outfile="${path.logs}/dependencies.svg" />
Expand Down
4 changes: 2 additions & 2 deletions build/phpcs/ruleset.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0"?>
<ruleset name="PHP-CryptLib">
<description>PHP-CryptLib Standard</description>
<ruleset name="PHP-PasswordLib">
<description>PHP-PasswordLib Standard</description>

<rule ref="Generic.CodeAnalysis.ForLoopShouldBeWhileLoop"/>
<rule ref="Generic.CodeAnalysis.ForLoopWithTestFunctionCall"/>
Expand Down
4 changes: 2 additions & 2 deletions build/phpmd/ruleset.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0"?>
<ruleset name="PHP-CryptLib PHPMD Rule Set"
<ruleset name="PHP-PasswordLib PHPMD Rule Set"
xmlns="http://pmd.sf.net/ruleset/1.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://pmd.sf.net/ruleset/1.0.0
Expand All @@ -8,7 +8,7 @@
http://pmd.sf.net/ruleset_xml_schema.xsd">
<description>
A custom PHPMD ruleset for detecting messy code in the
PHP-CryptLib project.
PHP-PasswordLib project.
</description>

<rule ref="rulesets/codesize.xml/CyclomaticComplexity">
Expand Down
23 changes: 23 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"name": "PasswordLib/PasswordLib",
"type": "library",
"description": "A Password Hashing Library",
"keywords": ["password", "hash", "hashing", "random", "salt", "crypt"],
"homepage": "https://github.com/ircmaxell/PHP-PasswordLib",
"license": "MIT",
"authors": [
{
"name": "Anthony Ferrara",
"email": "ircmaxell@ircmaxell.com",
"homepage": "http://blog.ircmaxell.com"
}
],
"require": {
"php": ">=5.3.2"
},
"autoload": {
"psr-0": {
"PasswordLib": "src"
}
}
}
Loading

0 comments on commit 57d8d83

Please sign in to comment.