cvs: pear /Crypt_HMAC2 HMAC2.php generate_package_xml.php package.xml /Crypt_HMAC2/HMAC2 Exception.php /Crypt_HMAC2/docs intro.xml
/Crypt_HMAC2/tests AllTests.php HMAC2Test.php TestConfiguration.php.dist TestHelper.php
| From: | Pádraic Brady | Date: | Sat, 29 Sep 2007 12:12:10 +0000 |
| Subject: | cvs: pear /Crypt_HMAC2 HMAC2.php generate_package_xml.php package.xml /Crypt_HMAC2/HMAC2 Exception.php /Crypt_HMAC2/docs intro.xml /Crypt_HMAC2/tests AllTests.php HMAC2Test.php TestConfiguration.php.dist TestHelper.php |
||
| Groups: | php.pear.cvs | ||
| Request: | Send a blank email to pear-cvs+get-48699@lists.php.net to get a copy of this message | ||
padraic Sat Sep 29 12:12:10 2007 UTC
Added files:
/pear/Crypt_HMAC2 HMAC2.php generate_package_xml.php package.xml
/pear/Crypt_HMAC2/HMAC2 Exception.php
/pear/Crypt_HMAC2/docs intro.xml
/pear/Crypt_HMAC2/tests AllTests.php HMAC2Test.php
TestConfiguration.php.dist TestHelper.php
Log:
HMAC2 initial commit for OpenID ancillary support
http://cvs.php.net/viewvc.cgi/pear/Crypt_HMAC2/HMAC2.php?view=markup&rev=1.1 Index: pear/Crypt_HMAC2/HMAC2.php +++ pear/Crypt_HMAC2/HMAC2.php <?php /** * Implementation of the Hashed Message Authentication Code algorithm with * support for a wide range of hashing algorithms available using either of * the "hash" or "mhash" extensions, or the native md5() and sha1() functions. * * PHP version 5 * * LICENSE: * * Copyright (c) 2005-2007, Pádraic Brady <padraic.brady@yahoo.com> * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * * Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * The name of the author may not be used to endorse or promote products * derived from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * * @category Encryption * @package Crypt_HMAC2 * @author Pádraic Brady <padraic.brady@yahoo.com> * @license http://opensource.org/licenses/bsd-license.php New BSD License * @version $Id: HMAC2.php,v 1.1 2007/09/29 12:12:09 padraic Exp $ * @link http:// */ /** * Crypt_HMAC2 class * * Example usage: * $key = str_repeat("\xaa", 20); // insecure key only for example * $data = "Hello World!"; * $hmac = new Crypt_HMAC2($key, 'SHA256'); * $hmacHash = $hmac->hash($data); * * Supported hashing algorithms are limited by your PHP version access * to the hash, mhash extensions and supported native functions like * md5() and sha1(). * * To obtain raw binary output, set the optional second parameter of * Crypt_HMAC2::hash() to Crypt_HMAC2::BINARY. * * $hmacRawHash = $hmac->hash($data, Crypt_HMAC2::BINARY); * * @category Encryption * @package Crypt_HMAC2 * @author Pádraic Brady <padraic.brady@yahoo.com> * @copyright 2005-2007 Pádraic Brady * @license http://opensource.org/licenses/bsd-license.php New BSD License * @link http:// * @version @package_version@ * @access public */ class Crypt_HMAC2 { /** * The key to use for the hash * * @var string */ protected $_key = null; /** * pack() format to be used for current hashing method * * @var string */ protected $_packFormat = null; /** * Hashing algorithm; can be the md5/sha1 functions or any algorithm name * listed in the output of PHP 5.1.2+ hash_algos(). * * @var string */ protected $_hashAlgorithm = 'md5'; /** * Supported direct hashing functions in PHP * * @var array */ protected $_supportedHashNativeFunctions = array( 'md5', 'sha1', ); /** * List of hash pack formats for each hashing algorithm supported. * Only required when hash or mhash are not available, and we are * using either md5() or sha1(). * * @var array */ protected $_hashPackFormats = array( 'md5' => 'H32', 'sha1' => 'H40' ); /** * List of algorithms supported my mhash() * * @var array */ protected $_supportedMhashAlgorithms = array('adler32',' crc32', 'crc32b', 'gost', 'haval128', 'haval160', 'haval192', 'haval256', 'md4', 'md5', 'ripemd160', 'sha1', 'sha256', 'tiger', 'tiger128', 'tiger160'); /** * Constants representing the output mode of the hash algorithm */ const STRING = 'string'; const BINARY = 'binary'; /** * Constructor; optionally set Key and Hash at this point * * @param string $key * @param string $hash */ public function __construct($key = null, $hash = null) { if (!is_null($key)) { $this->setKey($key); } if (!is_null($hash)) { $this->setHashAlgorithm($hash); } } /** * Set the key to use when hashing * * @param string $key * @return Crypt_HMAC2 */ public function setKey($key) { if (!isset($key) || empty($key)) { require_once 'Crypt/HMAC2/Exception.php'; throw new Crypt_HMAC2_Exception('provided key is null or empty'); } $this->_key = $key; return $this; } /** * Getter to return the currently set key * * @return string */ public function getKey() { if (is_null($this->_key)) { require_once 'Crypt/HMAC2/Exception.php'; throw new Crypt_HMAC2_Exception('key has not yet been set'); } return $this->_key; } /** * Setter for the hash method. Supports md5() and sha1() functions, and if * available the hashing algorithms supported by the hash() PHP5 function or * the mhash extension. * * Since they are so many varied HMAC methods in PHP these days this method * does a lot of checking to figure out what's available and not. * * @param string $hash * @return Crypt_HMAC2 */ public function setHashAlgorithm($hash) { if (!isset($hash) || empty($hash)) { require_once 'Crypt/HMAC2/Exception.php'; throw new Crypt_HMAC2_Exception('provided hash string is null or empty'); } $hash = strtolower($hash); $hashSupported = false; if (function_exists('hash_algos') && in_array($hash, hash_algos())) { $hashSupported = true; } if ($hashSupported === false && function_exists('mhash') && in_array($hash, $this->_supportedMhashAlgorithms)) { $hashSupported = true; } if ($hashSupported === false && in_array($hash, $this->_supportedHashNativeFunctions) && in_array($hash, array_keys($this->_hashPackFormats))) { $this->_packFormat = $this->_hashPackFormats[$hash]; $hashSupported = true; } if ($hashSupported === false) { require_once 'Crypt/HMAC2/Exception.php'; throw new Crypt_HMAC2_Exception('hash algorithm provided is not supported on this PHP instance; please enable the hash or mhash extensions'); } $this->_hashAlgorithm = $hash; return $this; } /** * Return the current hashing algorithm * * @return string */ public function getHashAlgorithm() { return $this->_hashAlgorithm; } /** * Perform HMAC and return the keyed data * * @param string $data * @param bool $internal Option to not use hash() functions for testing * @return string */ public function hash($data, $output = self::STRING, $internal = false) { if (function_exists('hash_hmac') && $internal === false) { if ($output == self::BINARY) { return hash_hmac($this->getHashAlgorithm(), $data, $this->getKey(), 1); } return hash_hmac($this->getHashAlgorithm(), $data, $this->getKey()); } if (function_exists('mhash') && $internal === false) { if ($output == self::BINARY) { return mhash($this->_getMhashDefinition($this->getHashAlgorithm()), $data, $this->getKey()); } $bin = mhash($this->_getMhashDefinition($this->getHashAlgorithm()), $data, $this->getKey()); return bin2hex($bin); } // last ditch effort for MD5 and SHA1 only $key = $this->getKey(); $hash = $this->getHashAlgorithm(); if (strlen($key) < 64) { $key = str_pad($key, 64, chr(0)); } elseif (strlen($key) > 64) { $key = pack($this->_packFormat, $this->_digest($hash, $key, $output)); } $padInner = (substr($key, 0, 64) ^ str_repeat(chr(0x36), 64)); $padOuter = (substr($key, 0, 64) ^ str_repeat(chr(0x5C), 64)); return $this->_digest($hash, $padOuter . pack($this->_packFormat, $this->_digest($hash, $padInner . $data, $output)), $output); } /** * Method of working around the inability to use mhash constants; this * will locate the Constant value of any support Hashing algorithm named * in the string parameter. * * @param string $hashAlgorithm * @return integer */ protected function _getMhashDefinition($hashAlgorithm) { for ($i = 0; $i <= mhash_count(); $i++) { $types[mhash_get_hash_name($i)] = $i; } return $types[strtoupper($hashAlgorithm)]; } /** * Digest method when using native functions which allows the selection * of raw binary output. * * @param string $hash * @param string $key * @param string $mode * @return string */ protected function _digest($hash, $key, $mode) { if ($mode == self::BINARY) { return $hash($key, true); } return $hash($key); } } http://cvs.php.net/viewvc.cgi/pear/Crypt_HMAC2/generate_package_xml.php?view=markup&rev=1.1 Index: pear/Crypt_HMAC2/generate_package_xml.php +++ pear/Crypt_HMAC2/generate_package_xml.php <?php require_once('PEAR/PackageFileManager2.php'); PEAR::setErrorHandling(PEAR_ERROR_DIE); //require_once 'PEAR/Config.php'; //PEAR_Config::singleton('/path/to/unusualpearconfig.ini'); // use the above lines if the channel information is not validating $packagexml = new PEAR_PackageFileManager2; // for an existing package.xml use // $packagexml = {@link importOptions()} instead $e = $packagexml->setOptions( array('baseinstalldir' => 'Crypt', 'packagedirectory' => 'D:/xampp/htdocs/projects/pear/trunk/Crypt_HMAC2/', 'filelistgenerator' => 'file', 'dir_roles' => array('docs' => 'doc', 'tests' => 'test'), 'ignore' => array('generate_package_xml.php', '.svn') ) ); $packagexml->setPackage('Crypt_HMAC2'); $packagexml->setSummary('Implementation of Hashed Message Authentication Code for PHP5'); $packagexml->setDescription("Implementation of Hashed Message Authentication Code for PHP5.\nThis package may use the hash or mhash extensions when enabled to\nextend the range of cryptographic hash functions beyond the natively\nimplemented MD5 and SHA1."); $packagexml->setChannel('pear.php.net'); $packagexml->setAPIVersion('0.1.0'); $packagexml->setReleaseVersion('0.1.0a3'); $packagexml->setReleaseStability('alpha'); $packagexml->setAPIStability('alpha'); $packagexml->setNotes("* This is the proposal version..."); $packagexml->setPackageType('php'); $packagexml->setPhpDep('5.0.0'); $packagexml->setPearinstallerDep('1.4.0'); $packagexml->addMaintainer('lead', 'padraic', 'Pádraic Brady', 'padraic@php.net'); $packagexml->setLicense('New BSD License', 'http://opensource.org/licenses/bsd-license.php'); $packagexml->generateContents(); //$pkg = &$packagexml->exportCompatiblePackageFile1(); // get a PEAR_PackageFile object if (isset($_GET['make']) || (isset($_SERVER['argv']) && @$_SERVER['argv'][1] == 'make')) { //$pkg->writePackageFile(); $packagexml->writePackageFile(); } else { //$pkg->debugPackageFile(); $packagexml->debugPackageFile(); } ?> http://cvs.php.net/viewvc.cgi/pear/Crypt_HMAC2/package.xml?view=markup&rev=1.1 Index: pear/Crypt_HMAC2/package.xml +++ pear/Crypt_HMAC2/package.xml <?xml version="1.0" encoding="UTF-8"?> <package packagerversion="1.4.5" version="2.0" xmlns="http://pear.php.net/dtd/package-2.0" xmlns:tasks="http://pear.php.net/dtd/tasks-1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://pear.php.net/dtd/tasks-1.0 http://pear.php.net/dtd/tasks-1.0.xsd http://pear.php.net/dtd/package-2.0 http://pear.php.net/dtd/package-2.0.xsd"> <name>Crypt_HMAC2</name> <channel>pear.php.net</channel> <summary>Implementation of Hashed Message Authentication Code for PHP5</summary> <description>Implementation of Hashed Message Authentication Code for PHP5. This package may use the hash or mhash extensions when enabled to extend the range of cryptographic hash functions beyond the natively implemented MD5 and SHA1.</description> <lead> <name>Pádraic Brady</name> <user>padraic</user> <email>padraic@php.net</email> <active>yes</active> </lead> <date>2007-07-03</date> <time>17:35:51</time> <version> <release>0.1.0a3</release> <api>0.1.0</api> </version> <stability> <release>alpha</release> <api>alpha</api> </stability> <license uri="http://opensource.org/licenses/bsd-license.php">New BSD License</license> <notes>* This is the proposal version...</notes> <contents> <dir baseinstalldir="Crypt" name="/"> <file baseinstalldir="Crypt" md5sum="f0b669541378acc03ae8e1993f6b2883" name="docs/intro.xml" role="doc" /> <file baseinstalldir="Crypt" md5sum="b8cfb63cbadf398aa9f2ca1190f3ec69" name="HMAC2/Exception.php" role="php" /> <file baseinstalldir="Crypt" md5sum="1a6870e0d794d701645a0707d502bdc7" name="tests/AllTests.php" role="test" /> <file baseinstalldir="Crypt" md5sum="76ec1089073659bec3a3cda80a028bb1" name="tests/HMAC2Test.php" role="test" /> <file baseinstalldir="Crypt" md5sum="b39889db5358a2a1dbe2dcd4f212be67" name="tests/TestConfiguration.php.dist" role="test" /> <file baseinstalldir="Crypt" md5sum="2905836b0402bd79dbc8a54202aa133f" name="tests/TestHelper.php" role="test" /> <file baseinstalldir="Crypt" md5sum="6fe95b67b708b8386b02a88590429e12" name="HMAC2.php" role="php" /> </dir> </contents> <dependencies> <required> <php> <min>5.0.0</min> </php> <pearinstaller> <min>1.4.0</min> </pearinstaller> </required> </dependencies> <phprelease /> <changelog> <release> <version> <release>0.1.0a1</release> <api>0.1.0</api> </version> <stability> <release>alpha</release> <api>alpha</api> </stability> <date>2007-06-21</date> <license>New BSD License</license> <notes>* This is the proposal version...</notes> </release> <release> <version> <release>0.1.0a2</release> <api>0.1.0</api> </version> <stability> <release>alpha</release> <api>alpha</api> </stability> <date>2007-07-03</date> <license>New BSD License</license> <notes>* This is the proposal version...</notes> </release> <release> <version> <release>0.1.0a3</release> <api>0.1.0</api> </version> <stability> <release>alpha</release> <api>alpha</api> </stability> <date>2007-07-03</date> <license>New BSD License</license> <notes>* This is the proposal version...</notes> </release> </changelog> </package> http://cvs.php.net/viewvc.cgi/pear/Crypt_HMAC2/HMAC2/Exception.php?view=markup&rev=1.1 Index: pear/Crypt_HMAC2/HMAC2/Exception.php +++ pear/Crypt_HMAC2/HMAC2/Exception.php <?php /** * Exception subclass of PEAR_Exception for Crypt_HMAC2. * * PHP version 5 * * LICENSE: * * Copyright (c) 2005-2007, Pádraic Brady <padraic.brady@yahoo.com> * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * * Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * The name of the author may not be used to endorse or promote products * derived from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * * @category Encryption * @package Crypt_HMAC2 * @author Pádraic Brady <padraic.brady@yahoo.com> * @license http://opensource.org/licenses/bsd-license.php New BSD License * @version $Id: Exception.php,v 1.1 2007/09/29 12:12:09 padraic Exp $ * @link http:// */ /** * PEAR_Exception */ require_once 'PEAR/Exception.php'; /** * Crypt_HMAC2_Exception * * @category Encryption * @package Crypt_HMAC2 * @author Pádraic Brady <padraic.brady@yahoo.com> * @copyright 2005-2007 Pádraic Brady * @license http://opensource.org/licenses/bsd-license.php New BSD License * @link http:// * @version @package_version@ * @access public */ class Crypt_HMAC2_Exception extends PEAR_Exception {} http://cvs.php.net/viewvc.cgi/pear/Crypt_HMAC2/docs/intro.xml?view=markup&rev=1.1 Index: pear/Crypt_HMAC2/docs/intro.xml +++ pear/Crypt_HMAC2/docs/intro.xml <refentry id="package.encryption.crypt-hmac2.intro"> <refnamediv> <refname>Introduction</refname> <refpurpose> Introduction to <classname>Crypt_HMAC2</classname> </refpurpose> </refnamediv> <refsect1 id="package.encryption.crypt-hmac2.intro.overview"> <title>Overview</title> <para> <classname>Crypt_HMAC2</classname> implements the Key-Message Authentication Code (HMAC) algorithm as defined in <ulink url="http://tools.ietf.org/html/rfc2104">RFC2104</ulink>. This is a type of mechanism for authenticating messages using cryptographic hash algorithms such as MD5, SHA1 or SHA256. It is not limited to these three and can use any iterated hash algorithm. Generally a HMAC's security is reliant on the underlying cryptographic hash algorithm. </para> </refsect1> <refsect1 id="package.encryption.crypt-hmac2.intro.supported-cryptographic-hashes"> <title>Supported Cryptographic Hashes</title> <para> The <classname>Crypt_HMAC2</classname> class is easy to use. It supports, at a minimum, MD5 and SHA1 as the cryptographic hash function since these are always available to PHP5. </para> <para> The range of cryptographic hash algorithms is extended when either of the hash or mhash extensions are enabled. This would, for example, allow for SHA256 HMACs which are gaining in popular usage. </para> </refsect1> <refsect1 id="package.encryption.crypt-hmac2.intro.examples"> <title>Examples</title> <para> All examples utilise a fairly arbitrary key which should not not be considered secure. You can generate a secure key with a second party using some other means, such as Diffie-Hellman Key Exchange. </para> <example> <title>HMAC with SHA1</title> <programlisting role="php"> require_once "Crypt/HMAC2.php"; $key = str_repeat("\xaa",80); $crypt = new Crypt_HMAC2($key, 'SHA1'); $data = 'Test Using Larger Than Block-Size Key and Larger Than One Block-Size Data'; $hmac = $crypt->hash($data); // e8e99d0f45237d786d6bbaa7965c7808bbff1a91 </programlisting> </example> <example> <title>HMAC with RIPEMD160</title> <programlisting role="php"> require_once "Crypt/HMAC2.php"; $key = str_repeat("\x0b", 20); $crypt = new Crypt_HMAC2($key, 'RIPEMD160'); $data = 'Hi There'; $hmac = $crypt->hash($data); // 24cb4bd67d20fc1a5d2ed7732dcc39377f0a5668 </programlisting> </example> </refsect1> </refentry> <!-- Keep this comment at the end of the file Local variables: mode: sgml sgml-omittag:t sgml-shorttag:t sgml-minimize-attributes:nil sgml-always-quote-attributes:t sgml-indent-step:1 sgml-indent-data:t sgml-parent-document:nil sgml-default-dtd-file:"../../../../manual.ced" sgml-exposed-tags:nil sgml-local-catalogs:nil sgml-local-ecat-files:nil End: vim600: syn=xml fen fdm=syntax fdl=2 si vim: et tw=78 syn=sgml vi: ts=1 sw=1 --> http://cvs.php.net/viewvc.cgi/pear/Crypt_HMAC2/tests/AllTests.php?view=markup&rev=1.1 Index: pear/Crypt_HMAC2/tests/AllTests.php +++ pear/Crypt_HMAC2/tests/AllTests.php <?php if (!defined('PHPUnit_MAIN_METHOD')) { define('PHPUnit_MAIN_METHOD', 'AllTests::main'); } require_once 'PHPUnit/Framework/TestSuite.php'; require_once 'PHPUnit/TextUI/TestRunner.php'; require_once 'TestHelper.php'; require_once 'HMAC2Test.php'; class AllTests { public static function main() { $parameters = array(); if (TESTS_GENERATE_REPORT && extension_loaded('xdebug')) { $parameters['reportDirectory'] = TESTS_GENERATE_REPORT_TARGET; } PHPUnit_TextUI_TestRunner::run(self::suite(), $parameters); } public static function suite() { $suite = new PHPUnit_Framework_TestSuite('PEAR - Crypt_HMAC2'); $suite->addTestSuite('Crypt_HMAC2Test'); return $suite; } } if (PHPUnit_MAIN_METHOD == 'AllTests::main') { AllTests::main(); } http://cvs.php.net/viewvc.cgi/pear/Crypt_HMAC2/tests/HMAC2Test.php?view=markup&rev=1.1 Index: pear/Crypt_HMAC2/tests/HMAC2Test.php +++ pear/Crypt_HMAC2/tests/HMAC2Test.php <?php require_once 'HMAC2.php'; require_once 'PHPUnit/Framework/TestCase.php'; /** * Outside the Internal Function tests, tests do not distinguish between hash and mhash * when available. All tests use Hashing algorithms both extensions implement. */ class Crypt_HMAC2Test extends PHPUnit_Framework_TestCase { // MD5 tests taken from RFC 2202 public function testHmacMD5_1() { $data = 'Hi There'; $key = str_repeat("\x0b", 16); $hmac = new Crypt_HMAC2($key, 'MD5'); $this->assertEquals('9294727a3638bb1c13f48ef8158bfc9d', $hmac->hash($data)); } public function testHmacMD5_2() { $data = 'what do ya want for nothing?'; $key = 'Jefe'; $hmac = new Crypt_HMAC2($key, 'MD5'); $this->assertEquals('750c783e6ab0b503eaa86e310a5db738', $hmac->hash($data)); } public function testHmacMD5_3() { $data = str_repeat("\xdd",50); $key = str_repeat("\xaa", 16); $hmac = new Crypt_HMAC2($key, 'MD5'); $this->assertEquals('56be34521d144c88dbb8c733f0e8b3f6', $hmac->hash($data)); } public function testHmacMD5_4() { $data = str_repeat("\xcd",50); $key = "\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19"; $hmac = new Crypt_HMAC2($key, 'MD5'); $this->assertEquals('697eaf0aca3a3aea3a75164746ffaa79', $hmac->hash($data)); } public function testHmacMD5_5() { $data = 'Test With Truncation'; $key = str_repeat("\x0c",16); $hmac = new Crypt_HMAC2($key, 'MD5'); $this->assertEquals('56461ef2342edc00f9bab995690efd4c', $hmac->hash($data)); } public function testHmacMD5_6() { $data = 'Test Using Larger Than Block-Size Key - Hash Key First'; $key = str_repeat("\xaa",80); $hmac = new Crypt_HMAC2($key, 'MD5'); $this->assertEquals('6b1ab7fe4bd7bf8f0b62e6ce61b9d0cd', $hmac->hash($data)); } public function testHmacMD5_7() { $data = 'Test Using Larger Than Block-Size Key and Larger Than One Block-Size Data'; $key = str_repeat("\xaa",80); $hmac = new Crypt_HMAC2($key, 'MD5'); $this->assertEquals('6f630fad67cda0ee1fb1f562db3aa53e', $hmac->hash($data)); } // SHA1 tests taken from RFC 2202 public function testHmacSHA1_1() { $data = 'Hi There'; $key = str_repeat("\x0b", 20); $hmac = new Crypt_HMAC2($key, 'SHA1'); $this->assertEquals('b617318655057264e28bc0b6fb378c8ef146be00', $hmac->hash($data)); } public function testHmacSHA1_2() { $data = 'what do ya want for nothing?'; $key = 'Jefe'; $hmac = new Crypt_HMAC2($key, 'SHA1'); $this->assertEquals('effcdf6ae5eb2fa2d27416d5f184df9c259a7c79', $hmac->hash($data)); } public function testHmacSHA1_3() { $data = str_repeat("\xdd",50); $key = str_repeat("\xaa", 20); $hmac = new Crypt_HMAC2($key, 'SHA1'); $this->assertEquals('125d7342b9ac11cd91a39af48aa17b4f63f175d3', $hmac->hash($data)); } public function testHmacSHA1_4() { $data = str_repeat("\xcd",50); $key = "\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19"; $hmac = new Crypt_HMAC2($key, 'SHA1'); $this->assertEquals('4c9007f4026250c6bc8414f9bf50c86c2d7235da', $hmac->hash($data)); } public function testHmacSHA1_5() { $data = 'Test With Truncation'; $key = str_repeat("\x0c",20); $hmac = new Crypt_HMAC2($key, 'SHA1'); $this->assertEquals('4c1a03424b55e07fe7f27be1d58bb9324a9a5a04', $hmac->hash($data)); } public function testHmacSHA1_6() { $data = 'Test Using Larger Than Block-Size Key - Hash Key First'; $key = str_repeat("\xaa",80); $hmac = new Crypt_HMAC2($key, 'SHA1'); $this->assertEquals('aa4ae5e15272d00e95705637ce8a3b55ed402112', $hmac->hash($data)); } public function testHmacSHA1_7() { $data = 'Test Using Larger Than Block-Size Key and Larger Than One Block-Size Data'; $key = str_repeat("\xaa",80); $hmac = new Crypt_HMAC2($key, 'SHA1'); $this->assertEquals('e8e99d0f45237d786d6bbaa7965c7808bbff1a91', $hmac->hash($data)); } // RIPEMD160 tests taken from RFC 2286 public function testHmacRIPEMD160_1() { $data = 'Hi There'; $key = str_repeat("\x0b", 20); $hmac = new Crypt_HMAC2($key, 'RIPEMD160'); $this->assertEquals('24cb4bd67d20fc1a5d2ed7732dcc39377f0a5668', $hmac->hash($data)); } public function testHmacRIPEMD160_2() { $data = 'what do ya want for nothing?'; $key = 'Jefe'; $hmac = new Crypt_HMAC2($key, 'RIPEMD160'); $this->assertEquals('dda6c0213a485a9e24f4742064a7f033b43c4069', $hmac->hash($data)); } public function testHmacRIPEMD160_3() { $data = str_repeat("\xdd",50); $key = str_repeat("\xaa", 20); $hmac = new Crypt_HMAC2($key, 'RIPEMD160'); $this->assertEquals('b0b105360de759960ab4f35298e116e295d8e7c1', $hmac->hash($data)); } public function testHmacRIPEMD160_4() { $data = str_repeat("\xcd",50); $key = "\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19"; $hmac = new Crypt_HMAC2($key, 'RIPEMD160'); $this->assertEquals('d5ca862f4d21d5e610e18b4cf1beb97a4365ecf4', $hmac->hash($data)); } public function testHmacRIPEMD160_5() { $data = 'Test With Truncation'; $key = str_repeat("\x0c",20); $hmac = new Crypt_HMAC2($key, 'RIPEMD160'); $this->assertEquals('7619693978f91d90539ae786500ff3d8e0518e39', $hmac->hash($data)); } public function testHmacRIPEMD160_6() { $data = 'Test Using Larger Than Block-Size Key - Hash Key First'; $key = str_repeat("\xaa",80); $hmac = new Crypt_HMAC2($key, 'RIPEMD160'); $this->assertEquals('6466ca07ac5eac29e1bd523e5ada7605b791fd8b', $hmac->hash($data)); } public function testHmacRIPEMD160_7() { $data = 'Test Using Larger Than Block-Size Key and Larger Than One Block-Size Data'; $key = str_repeat("\xaa",80); $hmac = new Crypt_HMAC2($key, 'RIPEMD160'); $this->assertEquals('69ea60798d71616cce5fd0871e23754cd75d5a0a', $hmac->hash($data)); } // hash() free testing (excludes ripemd160) // MD5 tests taken from RFC 2202 public function testHmac_InternalMD5_1() { $data = 'Hi There'; $key = str_repeat("\x0b", 16); $hmac = new Crypt_HMAC2($key, 'MD5'); $this->assertEquals('9294727a3638bb1c13f48ef8158bfc9d', $hmac->hash($data), true); } public function testHmac_InternalMD5_2() { $data = 'what do ya want for nothing?'; $key = 'Jefe'; $hmac = new Crypt_HMAC2($key, 'MD5'); $this->assertEquals('750c783e6ab0b503eaa86e310a5db738', $hmac->hash($data), true); } public function testHmac_InternalMD5_3() { $data = str_repeat("\xdd",50); $key = str_repeat("\xaa", 16); $hmac = new Crypt_HMAC2($key, 'MD5'); $this->assertEquals('56be34521d144c88dbb8c733f0e8b3f6', $hmac->hash($data), true); } public function testHmac_InternalMD5_4() { $data = str_repeat("\xcd",50); $key = "\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19"; $hmac = new Crypt_HMAC2($key, 'MD5'); $this->assertEquals('697eaf0aca3a3aea3a75164746ffaa79', $hmac->hash($data), true); } public function testHmac_InternalMD5_5() { $data = 'Test With Truncation'; $key = str_repeat("\x0c",16); $hmac = new Crypt_HMAC2($key, 'MD5'); $this->assertEquals('56461ef2342edc00f9bab995690efd4c', $hmac->hash($data), true); } public function testHmac_InternalMD5_6() { $data = 'Test Using Larger Than Block-Size Key - Hash Key First'; $key = str_repeat("\xaa",80); $hmac = new Crypt_HMAC2($key, 'MD5'); $this->assertEquals('6b1ab7fe4bd7bf8f0b62e6ce61b9d0cd', $hmac->hash($data), true); } public function testHmac_InternalMD5_7() { $data = 'Test Using Larger Than Block-Size Key and Larger Than One Block-Size Data'; $key = str_repeat("\xaa",80); $hmac = new Crypt_HMAC2($key, 'MD5'); $this->assertEquals('6f630fad67cda0ee1fb1f562db3aa53e', $hmac->hash($data), true); } // SHA1 tests taken from RFC 2202 public function testHmac_InternalSHA1_1() { $data = 'Hi There'; $key = str_repeat("\x0b", 20); $hmac = new Crypt_HMAC2($key, 'SHA1'); $this->assertEquals('b617318655057264e28bc0b6fb378c8ef146be00', $hmac->hash($data), true); } public function testHmac_InternalSHA1_2() { $data = 'what do ya want for nothing?'; $key = 'Jefe'; $hmac = new Crypt_HMAC2($key, 'SHA1'); $this->assertEquals('effcdf6ae5eb2fa2d27416d5f184df9c259a7c79', $hmac->hash($data), true); } public function testHmac_InternalSHA1_3() { $data = str_repeat("\xdd",50); $key = str_repeat("\xaa", 20); $hmac = new Crypt_HMAC2($key, 'SHA1'); $this->assertEquals('125d7342b9ac11cd91a39af48aa17b4f63f175d3', $hmac->hash($data), true); } public function testHmac_InternalSHA1_4() { $data = str_repeat("\xcd",50); $key = "\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19"; $hmac = new Crypt_HMAC2($key, 'SHA1'); $this->assertEquals('4c9007f4026250c6bc8414f9bf50c86c2d7235da', $hmac->hash($data), true); } public function testHmac_InternalSHA1_5() { $data = 'Test With Truncation'; $key = str_repeat("\x0c",20); $hmac = new Crypt_HMAC2($key, 'SHA1'); $this->assertEquals('4c1a03424b55e07fe7f27be1d58bb9324a9a5a04', $hmac->hash($data), true); } public function testHmac_InternalSHA1_6() { $data = 'Test Using Larger Than Block-Size Key - Hash Key First'; $key = str_repeat("\xaa",80); $hmac = new Crypt_HMAC2($key, 'SHA1'); $this->assertEquals('aa4ae5e15272d00e95705637ce8a3b55ed402112', $hmac->hash($data), true); } public function testHmac_InternalSHA1_7() { $data = 'Test Using Larger Than Block-Size Key and Larger Than One Block-Size Data'; $key = str_repeat("\xaa",80); $hmac = new Crypt_HMAC2($key, 'SHA1'); $this->assertEquals('e8e99d0f45237d786d6bbaa7965c7808bbff1a91', $hmac->hash($data), true); } } http://cvs.php.net/viewvc.cgi/pear/Crypt_HMAC2/tests/TestConfiguration.php.dist?view=markup&rev=1.1 Index: pear/Crypt_HMAC2/tests/TestConfiguration.php.dist +++ pear/Crypt_HMAC2/tests/TestConfiguration.php.dist <?php define('TESTS_BCMATH_ENABLED', true); define('TESTS_GMP_ENABLED', true); define('TESTS_BIGINT_ENABLED', true); define('TESTS_GENERATE_REPORT', false); define('TESTS_GENERATE_REPORT_TARGET', '/path/to/target'); http://cvs.php.net/viewvc.cgi/pear/Crypt_HMAC2/tests/TestHelper.php?view=markup&rev=1.1 Index: pear/Crypt_HMAC2/tests/TestHelper.php +++ pear/Crypt_HMAC2/tests/TestHelper.php <?php error_reporting( E_ALL | E_STRICT ); date_default_timezone_set('Europe/London'); $prRoot = dirname(dirname(__FILE__)); $prTests = $prRoot . DIRECTORY_SEPARATOR . 'tests'; set_include_path($prRoot . PATH_SEPARATOR . $prTests . PATH_SEPARATOR . get_include_path()); if (is_readable($prTests . DIRECTORY_SEPARATOR . 'TestConfiguration.php')) { require_once 'TestConfiguration.php'; } else { require_once 'TestConfiguration.php.dist'; }
http://cvs.php.net/viewvc.cgi/pear/Crypt_HMAC2/HMAC2.php?view=markup&rev=1.1 Index: pear/Crypt_HMAC2/HMAC2.php +++ pear/Crypt_HMAC2/HMAC2.php <?php /** * Implementation of the Hashed Message Authentication Code algorithm with * support for a wide range of hashing algorithms available using either of * the "hash" or "mhash" extensions, or the native md5() and sha1() functions. * * PHP version 5 * * LICENSE: * * Copyright (c) 2005-2007, Pádraic Brady <padraic.brady@yahoo.com> * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * * Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * The name of the author may not be used to endorse or promote products * derived from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * * @category Encryption * @package Crypt_HMAC2 * @author Pádraic Brady <padraic.brady@yahoo.com> * @license http://opensource.org/licenses/bsd-license.php New BSD License * @version $Id: HMAC2.php,v 1.1 2007/09/29 12:12:09 padraic Exp $ * @link http:// */ /** * Crypt_HMAC2 class * * Example usage: * $key = str_repeat("\xaa", 20); // insecure key only for example * $data = "Hello World!"; * $hmac = new Crypt_HMAC2($key, 'SHA256'); * $hmacHash = $hmac->hash($data); * * Supported hashing algorithms are limited by your PHP version access * to the hash, mhash extensions and supported native functions like * md5() and sha1(). * * To obtain raw binary output, set the optional second parameter of * Crypt_HMAC2::hash() to Crypt_HMAC2::BINARY. * * $hmacRawHash = $hmac->hash($data, Crypt_HMAC2::BINARY); * * @category Encryption * @package Crypt_HMAC2 * @author Pádraic Brady <padraic.brady@yahoo.com> * @copyright 2005-2007 Pádraic Brady * @license http://opensource.org/licenses/bsd-license.php New BSD License * @link http:// * @version @package_version@ * @access public */ class Crypt_HMAC2 { /** * The key to use for the hash * * @var string */ protected $_key = null; /** * pack() format to be used for current hashing method * * @var string */ protected $_packFormat = null; /** * Hashing algorithm; can be the md5/sha1 functions or any algorithm name * listed in the output of PHP 5.1.2+ hash_algos(). * * @var string */ protected $_hashAlgorithm = 'md5'; /** * Supported direct hashing functions in PHP * * @var array */ protected $_supportedHashNativeFunctions = array( 'md5', 'sha1', ); /** * List of hash pack formats for each hashing algorithm supported. * Only required when hash or mhash are not available, and we are * using either md5() or sha1(). * * @var array */ protected $_hashPackFormats = array( 'md5' => 'H32', 'sha1' => 'H40' ); /** * List of algorithms supported my mhash() * * @var array */ protected $_supportedMhashAlgorithms = array('adler32',' crc32', 'crc32b', 'gost', 'haval128', 'haval160', 'haval192', 'haval256', 'md4', 'md5', 'ripemd160', 'sha1', 'sha256', 'tiger', 'tiger128', 'tiger160'); /** * Constants representing the output mode of the hash algorithm */ const STRING = 'string'; const BINARY = 'binary'; /** * Constructor; optionally set Key and Hash at this point * * @param string $key * @param string $hash */ public function __construct($key = null, $hash = null) { if (!is_null($key)) { $this->setKey($key); } if (!is_null($hash)) { $this->setHashAlgorithm($hash); } } /** * Set the key to use when hashing * * @param string $key * @return Crypt_HMAC2 */ public function setKey($key) { if (!isset($key) || empty($key)) { require_once 'Crypt/HMAC2/Exception.php'; throw new Crypt_HMAC2_Exception('provided key is null or empty'); } $this->_key = $key; return $this; } /** * Getter to return the currently set key * * @return string */ public function getKey() { if (is_null($this->_key)) { require_once 'Crypt/HMAC2/Exception.php'; throw new Crypt_HMAC2_Exception('key has not yet been set'); } return $this->_key; } /** * Setter for the hash method. Supports md5() and sha1() functions, and if * available the hashing algorithms supported by the hash() PHP5 function or * the mhash extension. * * Since they are so many varied HMAC methods in PHP these days this method * does a lot of checking to figure out what's available and not. * * @param string $hash * @return Crypt_HMAC2 */ public function setHashAlgorithm($hash) { if (!isset($hash) || empty($hash)) { require_once 'Crypt/HMAC2/Exception.php'; throw new Crypt_HMAC2_Exception('provided hash string is null or empty'); } $hash = strtolower($hash); $hashSupported = false; if (function_exists('hash_algos') && in_array($hash, hash_algos())) { $hashSupported = true; } if ($hashSupported === false && function_exists('mhash') && in_array($hash, $this->_supportedMhashAlgorithms)) { $hashSupported = true; } if ($hashSupported === false && in_array($hash, $this->_supportedHashNativeFunctions) && in_array($hash, array_keys($this->_hashPackFormats))) { $this->_packFormat = $this->_hashPackFormats[$hash]; $hashSupported = true; } if ($hashSupported === false) { require_once 'Crypt/HMAC2/Exception.php'; throw new Crypt_HMAC2_Exception('hash algorithm provided is not supported on this PHP instance; please enable the hash or mhash extensions'); } $this->_hashAlgorithm = $hash; return $this; } /** * Return the current hashing algorithm * * @return string */ public function getHashAlgorithm() { return $this->_hashAlgorithm; } /** * Perform HMAC and return the keyed data * * @param string $data * @param bool $internal Option to not use hash() functions for testing * @return string */ public function hash($data, $output = self::STRING, $internal = false) { if (function_exists('hash_hmac') && $internal === false) { if ($output == self::BINARY) { return hash_hmac($this->getHashAlgorithm(), $data, $this->getKey(), 1); } return hash_hmac($this->getHashAlgorithm(), $data, $this->getKey()); } if (function_exists('mhash') && $internal === false) { if ($output == self::BINARY) { return mhash($this->_getMhashDefinition($this->getHashAlgorithm()), $data, $this->getKey()); } $bin = mhash($this->_getMhashDefinition($this->getHashAlgorithm()), $data, $this->getKey()); return bin2hex($bin); } // last ditch effort for MD5 and SHA1 only $key = $this->getKey(); $hash = $this->getHashAlgorithm(); if (strlen($key) < 64) { $key = str_pad($key, 64, chr(0)); } elseif (strlen($key) > 64) { $key = pack($this->_packFormat, $this->_digest($hash, $key, $output)); } $padInner = (substr($key, 0, 64) ^ str_repeat(chr(0x36), 64)); $padOuter = (substr($key, 0, 64) ^ str_repeat(chr(0x5C), 64)); return $this->_digest($hash, $padOuter . pack($this->_packFormat, $this->_digest($hash, $padInner . $data, $output)), $output); } /** * Method of working around the inability to use mhash constants; this * will locate the Constant value of any support Hashing algorithm named * in the string parameter. * * @param string $hashAlgorithm * @return integer */ protected function _getMhashDefinition($hashAlgorithm) { for ($i = 0; $i <= mhash_count(); $i++) { $types[mhash_get_hash_name($i)] = $i; } return $types[strtoupper($hashAlgorithm)]; } /** * Digest method when using native functions which allows the selection * of raw binary output. * * @param string $hash * @param string $key * @param string $mode * @return string */ protected function _digest($hash, $key, $mode) { if ($mode == self::BINARY) { return $hash($key, true); } return $hash($key); } } http://cvs.php.net/viewvc.cgi/pear/Crypt_HMAC2/generate_package_xml.php?view=markup&rev=1.1 Index: pear/Crypt_HMAC2/generate_package_xml.php +++ pear/Crypt_HMAC2/generate_package_xml.php <?php require_once('PEAR/PackageFileManager2.php'); PEAR::setErrorHandling(PEAR_ERROR_DIE); //require_once 'PEAR/Config.php'; //PEAR_Config::singleton('/path/to/unusualpearconfig.ini'); // use the above lines if the channel information is not validating $packagexml = new PEAR_PackageFileManager2; // for an existing package.xml use // $packagexml = {@link importOptions()} instead $e = $packagexml->setOptions( array('baseinstalldir' => 'Crypt', 'packagedirectory' => 'D:/xampp/htdocs/projects/pear/trunk/Crypt_HMAC2/', 'filelistgenerator' => 'file', 'dir_roles' => array('docs' => 'doc', 'tests' => 'test'), 'ignore' => array('generate_package_xml.php', '.svn') ) ); $packagexml->setPackage('Crypt_HMAC2'); $packagexml->setSummary('Implementation of Hashed Message Authentication Code for PHP5'); $packagexml->setDescription("Implementation of Hashed Message Authentication Code for PHP5.\nThis package may use the hash or mhash extensions when enabled to\nextend the range of cryptographic hash functions beyond the natively\nimplemented MD5 and SHA1."); $packagexml->setChannel('pear.php.net'); $packagexml->setAPIVersion('0.1.0'); $packagexml->setReleaseVersion('0.1.0a3'); $packagexml->setReleaseStability('alpha'); $packagexml->setAPIStability('alpha'); $packagexml->setNotes("* This is the proposal version..."); $packagexml->setPackageType('php'); $packagexml->setPhpDep('5.0.0'); $packagexml->setPearinstallerDep('1.4.0'); $packagexml->addMaintainer('lead', 'padraic', 'Pádraic Brady', 'padraic@php.net'); $packagexml->setLicense('New BSD License', 'http://opensource.org/licenses/bsd-license.php'); $packagexml->generateContents(); //$pkg = &$packagexml->exportCompatiblePackageFile1(); // get a PEAR_PackageFile object if (isset($_GET['make']) || (isset($_SERVER['argv']) && @$_SERVER['argv'][1] == 'make')) { //$pkg->writePackageFile(); $packagexml->writePackageFile(); } else { //$pkg->debugPackageFile(); $packagexml->debugPackageFile(); } ?> http://cvs.php.net/viewvc.cgi/pear/Crypt_HMAC2/package.xml?view=markup&rev=1.1 Index: pear/Crypt_HMAC2/package.xml +++ pear/Crypt_HMAC2/package.xml <?xml version="1.0" encoding="UTF-8"?> <package packagerversion="1.4.5" version="2.0" xmlns="http://pear.php.net/dtd/package-2.0" xmlns:tasks="http://pear.php.net/dtd/tasks-1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://pear.php.net/dtd/tasks-1.0 http://pear.php.net/dtd/tasks-1.0.xsd http://pear.php.net/dtd/package-2.0 http://pear.php.net/dtd/package-2.0.xsd"> <name>Crypt_HMAC2</name> <channel>pear.php.net</channel> <summary>Implementation of Hashed Message Authentication Code for PHP5</summary> <description>Implementation of Hashed Message Authentication Code for PHP5. This package may use the hash or mhash extensions when enabled to extend the range of cryptographic hash functions beyond the natively implemented MD5 and SHA1.</description> <lead> <name>Pádraic Brady</name> <user>padraic</user> <email>padraic@php.net</email> <active>yes</active> </lead> <date>2007-07-03</date> <time>17:35:51</time> <version> <release>0.1.0a3</release> <api>0.1.0</api> </version> <stability> <release>alpha</release> <api>alpha</api> </stability> <license uri="http://opensource.org/licenses/bsd-license.php">New BSD License</license> <notes>* This is the proposal version...</notes> <contents> <dir baseinstalldir="Crypt" name="/"> <file baseinstalldir="Crypt" md5sum="f0b669541378acc03ae8e1993f6b2883" name="docs/intro.xml" role="doc" /> <file baseinstalldir="Crypt" md5sum="b8cfb63cbadf398aa9f2ca1190f3ec69" name="HMAC2/Exception.php" role="php" /> <file baseinstalldir="Crypt" md5sum="1a6870e0d794d701645a0707d502bdc7" name="tests/AllTests.php" role="test" /> <file baseinstalldir="Crypt" md5sum="76ec1089073659bec3a3cda80a028bb1" name="tests/HMAC2Test.php" role="test" /> <file baseinstalldir="Crypt" md5sum="b39889db5358a2a1dbe2dcd4f212be67" name="tests/TestConfiguration.php.dist" role="test" /> <file baseinstalldir="Crypt" md5sum="2905836b0402bd79dbc8a54202aa133f" name="tests/TestHelper.php" role="test" /> <file baseinstalldir="Crypt" md5sum="6fe95b67b708b8386b02a88590429e12" name="HMAC2.php" role="php" /> </dir> </contents> <dependencies> <required> <php> <min>5.0.0</min> </php> <pearinstaller> <min>1.4.0</min> </pearinstaller> </required> </dependencies> <phprelease /> <changelog> <release> <version> <release>0.1.0a1</release> <api>0.1.0</api> </version> <stability> <release>alpha</release> <api>alpha</api> </stability> <date>2007-06-21</date> <license>New BSD License</license> <notes>* This is the proposal version...</notes> </release> <release> <version> <release>0.1.0a2</release> <api>0.1.0</api> </version> <stability> <release>alpha</release> <api>alpha</api> </stability> <date>2007-07-03</date> <license>New BSD License</license> <notes>* This is the proposal version...</notes> </release> <release> <version> <release>0.1.0a3</release> <api>0.1.0</api> </version> <stability> <release>alpha</release> <api>alpha</api> </stability> <date>2007-07-03</date> <license>New BSD License</license> <notes>* This is the proposal version...</notes> </release> </changelog> </package> http://cvs.php.net/viewvc.cgi/pear/Crypt_HMAC2/HMAC2/Exception.php?view=markup&rev=1.1 Index: pear/Crypt_HMAC2/HMAC2/Exception.php +++ pear/Crypt_HMAC2/HMAC2/Exception.php <?php /** * Exception subclass of PEAR_Exception for Crypt_HMAC2. * * PHP version 5 * * LICENSE: * * Copyright (c) 2005-2007, Pádraic Brady <padraic.brady@yahoo.com> * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * * Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * The name of the author may not be used to endorse or promote products * derived from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * * @category Encryption * @package Crypt_HMAC2 * @author Pádraic Brady <padraic.brady@yahoo.com> * @license http://opensource.org/licenses/bsd-license.php New BSD License * @version $Id: Exception.php,v 1.1 2007/09/29 12:12:09 padraic Exp $ * @link http:// */ /** * PEAR_Exception */ require_once 'PEAR/Exception.php'; /** * Crypt_HMAC2_Exception * * @category Encryption * @package Crypt_HMAC2 * @author Pádraic Brady <padraic.brady@yahoo.com> * @copyright 2005-2007 Pádraic Brady * @license http://opensource.org/licenses/bsd-license.php New BSD License * @link http:// * @version @package_version@ * @access public */ class Crypt_HMAC2_Exception extends PEAR_Exception {} http://cvs.php.net/viewvc.cgi/pear/Crypt_HMAC2/docs/intro.xml?view=markup&rev=1.1 Index: pear/Crypt_HMAC2/docs/intro.xml +++ pear/Crypt_HMAC2/docs/intro.xml <refentry id="package.encryption.crypt-hmac2.intro"> <refnamediv> <refname>Introduction</refname> <refpurpose> Introduction to <classname>Crypt_HMAC2</classname> </refpurpose> </refnamediv> <refsect1 id="package.encryption.crypt-hmac2.intro.overview"> <title>Overview</title> <para> <classname>Crypt_HMAC2</classname> implements the Key-Message Authentication Code (HMAC) algorithm as defined in <ulink url="http://tools.ietf.org/html/rfc2104">RFC2104</ulink>. This is a type of mechanism for authenticating messages using cryptographic hash algorithms such as MD5, SHA1 or SHA256. It is not limited to these three and can use any iterated hash algorithm. Generally a HMAC's security is reliant on the underlying cryptographic hash algorithm. </para> </refsect1> <refsect1 id="package.encryption.crypt-hmac2.intro.supported-cryptographic-hashes"> <title>Supported Cryptographic Hashes</title> <para> The <classname>Crypt_HMAC2</classname> class is easy to use. It supports, at a minimum, MD5 and SHA1 as the cryptographic hash function since these are always available to PHP5. </para> <para> The range of cryptographic hash algorithms is extended when either of the hash or mhash extensions are enabled. This would, for example, allow for SHA256 HMACs which are gaining in popular usage. </para> </refsect1> <refsect1 id="package.encryption.crypt-hmac2.intro.examples"> <title>Examples</title> <para> All examples utilise a fairly arbitrary key which should not not be considered secure. You can generate a secure key with a second party using some other means, such as Diffie-Hellman Key Exchange. </para> <example> <title>HMAC with SHA1</title> <programlisting role="php"> require_once "Crypt/HMAC2.php"; $key = str_repeat("\xaa",80); $crypt = new Crypt_HMAC2($key, 'SHA1'); $data = 'Test Using Larger Than Block-Size Key and Larger Than One Block-Size Data'; $hmac = $crypt->hash($data); // e8e99d0f45237d786d6bbaa7965c7808bbff1a91 </programlisting> </example> <example> <title>HMAC with RIPEMD160</title> <programlisting role="php"> require_once "Crypt/HMAC2.php"; $key = str_repeat("\x0b", 20); $crypt = new Crypt_HMAC2($key, 'RIPEMD160'); $data = 'Hi There'; $hmac = $crypt->hash($data); // 24cb4bd67d20fc1a5d2ed7732dcc39377f0a5668 </programlisting> </example> </refsect1> </refentry> <!-- Keep this comment at the end of the file Local variables: mode: sgml sgml-omittag:t sgml-shorttag:t sgml-minimize-attributes:nil sgml-always-quote-attributes:t sgml-indent-step:1 sgml-indent-data:t sgml-parent-document:nil sgml-default-dtd-file:"../../../../manual.ced" sgml-exposed-tags:nil sgml-local-catalogs:nil sgml-local-ecat-files:nil End: vim600: syn=xml fen fdm=syntax fdl=2 si vim: et tw=78 syn=sgml vi: ts=1 sw=1 --> http://cvs.php.net/viewvc.cgi/pear/Crypt_HMAC2/tests/AllTests.php?view=markup&rev=1.1 Index: pear/Crypt_HMAC2/tests/AllTests.php +++ pear/Crypt_HMAC2/tests/AllTests.php <?php if (!defined('PHPUnit_MAIN_METHOD')) { define('PHPUnit_MAIN_METHOD', 'AllTests::main'); } require_once 'PHPUnit/Framework/TestSuite.php'; require_once 'PHPUnit/TextUI/TestRunner.php'; require_once 'TestHelper.php'; require_once 'HMAC2Test.php'; class AllTests { public static function main() { $parameters = array(); if (TESTS_GENERATE_REPORT && extension_loaded('xdebug')) { $parameters['reportDirectory'] = TESTS_GENERATE_REPORT_TARGET; } PHPUnit_TextUI_TestRunner::run(self::suite(), $parameters); } public static function suite() { $suite = new PHPUnit_Framework_TestSuite('PEAR - Crypt_HMAC2'); $suite->addTestSuite('Crypt_HMAC2Test'); return $suite; } } if (PHPUnit_MAIN_METHOD == 'AllTests::main') { AllTests::main(); } http://cvs.php.net/viewvc.cgi/pear/Crypt_HMAC2/tests/HMAC2Test.php?view=markup&rev=1.1 Index: pear/Crypt_HMAC2/tests/HMAC2Test.php +++ pear/Crypt_HMAC2/tests/HMAC2Test.php <?php require_once 'HMAC2.php'; require_once 'PHPUnit/Framework/TestCase.php'; /** * Outside the Internal Function tests, tests do not distinguish between hash and mhash * when available. All tests use Hashing algorithms both extensions implement. */ class Crypt_HMAC2Test extends PHPUnit_Framework_TestCase { // MD5 tests taken from RFC 2202 public function testHmacMD5_1() { $data = 'Hi There'; $key = str_repeat("\x0b", 16); $hmac = new Crypt_HMAC2($key, 'MD5'); $this->assertEquals('9294727a3638bb1c13f48ef8158bfc9d', $hmac->hash($data)); } public function testHmacMD5_2() { $data = 'what do ya want for nothing?'; $key = 'Jefe'; $hmac = new Crypt_HMAC2($key, 'MD5'); $this->assertEquals('750c783e6ab0b503eaa86e310a5db738', $hmac->hash($data)); } public function testHmacMD5_3() { $data = str_repeat("\xdd",50); $key = str_repeat("\xaa", 16); $hmac = new Crypt_HMAC2($key, 'MD5'); $this->assertEquals('56be34521d144c88dbb8c733f0e8b3f6', $hmac->hash($data)); } public function testHmacMD5_4() { $data = str_repeat("\xcd",50); $key = "\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19"; $hmac = new Crypt_HMAC2($key, 'MD5'); $this->assertEquals('697eaf0aca3a3aea3a75164746ffaa79', $hmac->hash($data)); } public function testHmacMD5_5() { $data = 'Test With Truncation'; $key = str_repeat("\x0c",16); $hmac = new Crypt_HMAC2($key, 'MD5'); $this->assertEquals('56461ef2342edc00f9bab995690efd4c', $hmac->hash($data)); } public function testHmacMD5_6() { $data = 'Test Using Larger Than Block-Size Key - Hash Key First'; $key = str_repeat("\xaa",80); $hmac = new Crypt_HMAC2($key, 'MD5'); $this->assertEquals('6b1ab7fe4bd7bf8f0b62e6ce61b9d0cd', $hmac->hash($data)); } public function testHmacMD5_7() { $data = 'Test Using Larger Than Block-Size Key and Larger Than One Block-Size Data'; $key = str_repeat("\xaa",80); $hmac = new Crypt_HMAC2($key, 'MD5'); $this->assertEquals('6f630fad67cda0ee1fb1f562db3aa53e', $hmac->hash($data)); } // SHA1 tests taken from RFC 2202 public function testHmacSHA1_1() { $data = 'Hi There'; $key = str_repeat("\x0b", 20); $hmac = new Crypt_HMAC2($key, 'SHA1'); $this->assertEquals('b617318655057264e28bc0b6fb378c8ef146be00', $hmac->hash($data)); } public function testHmacSHA1_2() { $data = 'what do ya want for nothing?'; $key = 'Jefe'; $hmac = new Crypt_HMAC2($key, 'SHA1'); $this->assertEquals('effcdf6ae5eb2fa2d27416d5f184df9c259a7c79', $hmac->hash($data)); } public function testHmacSHA1_3() { $data = str_repeat("\xdd",50); $key = str_repeat("\xaa", 20); $hmac = new Crypt_HMAC2($key, 'SHA1'); $this->assertEquals('125d7342b9ac11cd91a39af48aa17b4f63f175d3', $hmac->hash($data)); } public function testHmacSHA1_4() { $data = str_repeat("\xcd",50); $key = "\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19"; $hmac = new Crypt_HMAC2($key, 'SHA1'); $this->assertEquals('4c9007f4026250c6bc8414f9bf50c86c2d7235da', $hmac->hash($data)); } public function testHmacSHA1_5() { $data = 'Test With Truncation'; $key = str_repeat("\x0c",20); $hmac = new Crypt_HMAC2($key, 'SHA1'); $this->assertEquals('4c1a03424b55e07fe7f27be1d58bb9324a9a5a04', $hmac->hash($data)); } public function testHmacSHA1_6() { $data = 'Test Using Larger Than Block-Size Key - Hash Key First'; $key = str_repeat("\xaa",80); $hmac = new Crypt_HMAC2($key, 'SHA1'); $this->assertEquals('aa4ae5e15272d00e95705637ce8a3b55ed402112', $hmac->hash($data)); } public function testHmacSHA1_7() { $data = 'Test Using Larger Than Block-Size Key and Larger Than One Block-Size Data'; $key = str_repeat("\xaa",80); $hmac = new Crypt_HMAC2($key, 'SHA1'); $this->assertEquals('e8e99d0f45237d786d6bbaa7965c7808bbff1a91', $hmac->hash($data)); } // RIPEMD160 tests taken from RFC 2286 public function testHmacRIPEMD160_1() { $data = 'Hi There'; $key = str_repeat("\x0b", 20); $hmac = new Crypt_HMAC2($key, 'RIPEMD160'); $this->assertEquals('24cb4bd67d20fc1a5d2ed7732dcc39377f0a5668', $hmac->hash($data)); } public function testHmacRIPEMD160_2() { $data = 'what do ya want for nothing?'; $key = 'Jefe'; $hmac = new Crypt_HMAC2($key, 'RIPEMD160'); $this->assertEquals('dda6c0213a485a9e24f4742064a7f033b43c4069', $hmac->hash($data)); } public function testHmacRIPEMD160_3() { $data = str_repeat("\xdd",50); $key = str_repeat("\xaa", 20); $hmac = new Crypt_HMAC2($key, 'RIPEMD160'); $this->assertEquals('b0b105360de759960ab4f35298e116e295d8e7c1', $hmac->hash($data)); } public function testHmacRIPEMD160_4() { $data = str_repeat("\xcd",50); $key = "\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19"; $hmac = new Crypt_HMAC2($key, 'RIPEMD160'); $this->assertEquals('d5ca862f4d21d5e610e18b4cf1beb97a4365ecf4', $hmac->hash($data)); } public function testHmacRIPEMD160_5() { $data = 'Test With Truncation'; $key = str_repeat("\x0c",20); $hmac = new Crypt_HMAC2($key, 'RIPEMD160'); $this->assertEquals('7619693978f91d90539ae786500ff3d8e0518e39', $hmac->hash($data)); } public function testHmacRIPEMD160_6() { $data = 'Test Using Larger Than Block-Size Key - Hash Key First'; $key = str_repeat("\xaa",80); $hmac = new Crypt_HMAC2($key, 'RIPEMD160'); $this->assertEquals('6466ca07ac5eac29e1bd523e5ada7605b791fd8b', $hmac->hash($data)); } public function testHmacRIPEMD160_7() { $data = 'Test Using Larger Than Block-Size Key and Larger Than One Block-Size Data'; $key = str_repeat("\xaa",80); $hmac = new Crypt_HMAC2($key, 'RIPEMD160'); $this->assertEquals('69ea60798d71616cce5fd0871e23754cd75d5a0a', $hmac->hash($data)); } // hash() free testing (excludes ripemd160) // MD5 tests taken from RFC 2202 public function testHmac_InternalMD5_1() { $data = 'Hi There'; $key = str_repeat("\x0b", 16); $hmac = new Crypt_HMAC2($key, 'MD5'); $this->assertEquals('9294727a3638bb1c13f48ef8158bfc9d', $hmac->hash($data), true); } public function testHmac_InternalMD5_2() { $data = 'what do ya want for nothing?'; $key = 'Jefe'; $hmac = new Crypt_HMAC2($key, 'MD5'); $this->assertEquals('750c783e6ab0b503eaa86e310a5db738', $hmac->hash($data), true); } public function testHmac_InternalMD5_3() { $data = str_repeat("\xdd",50); $key = str_repeat("\xaa", 16); $hmac = new Crypt_HMAC2($key, 'MD5'); $this->assertEquals('56be34521d144c88dbb8c733f0e8b3f6', $hmac->hash($data), true); } public function testHmac_InternalMD5_4() { $data = str_repeat("\xcd",50); $key = "\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19"; $hmac = new Crypt_HMAC2($key, 'MD5'); $this->assertEquals('697eaf0aca3a3aea3a75164746ffaa79', $hmac->hash($data), true); } public function testHmac_InternalMD5_5() { $data = 'Test With Truncation'; $key = str_repeat("\x0c",16); $hmac = new Crypt_HMAC2($key, 'MD5'); $this->assertEquals('56461ef2342edc00f9bab995690efd4c', $hmac->hash($data), true); } public function testHmac_InternalMD5_6() { $data = 'Test Using Larger Than Block-Size Key - Hash Key First'; $key = str_repeat("\xaa",80); $hmac = new Crypt_HMAC2($key, 'MD5'); $this->assertEquals('6b1ab7fe4bd7bf8f0b62e6ce61b9d0cd', $hmac->hash($data), true); } public function testHmac_InternalMD5_7() { $data = 'Test Using Larger Than Block-Size Key and Larger Than One Block-Size Data'; $key = str_repeat("\xaa",80); $hmac = new Crypt_HMAC2($key, 'MD5'); $this->assertEquals('6f630fad67cda0ee1fb1f562db3aa53e', $hmac->hash($data), true); } // SHA1 tests taken from RFC 2202 public function testHmac_InternalSHA1_1() { $data = 'Hi There'; $key = str_repeat("\x0b", 20); $hmac = new Crypt_HMAC2($key, 'SHA1'); $this->assertEquals('b617318655057264e28bc0b6fb378c8ef146be00', $hmac->hash($data), true); } public function testHmac_InternalSHA1_2() { $data = 'what do ya want for nothing?'; $key = 'Jefe'; $hmac = new Crypt_HMAC2($key, 'SHA1'); $this->assertEquals('effcdf6ae5eb2fa2d27416d5f184df9c259a7c79', $hmac->hash($data), true); } public function testHmac_InternalSHA1_3() { $data = str_repeat("\xdd",50); $key = str_repeat("\xaa", 20); $hmac = new Crypt_HMAC2($key, 'SHA1'); $this->assertEquals('125d7342b9ac11cd91a39af48aa17b4f63f175d3', $hmac->hash($data), true); } public function testHmac_InternalSHA1_4() { $data = str_repeat("\xcd",50); $key = "\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19"; $hmac = new Crypt_HMAC2($key, 'SHA1'); $this->assertEquals('4c9007f4026250c6bc8414f9bf50c86c2d7235da', $hmac->hash($data), true); } public function testHmac_InternalSHA1_5() { $data = 'Test With Truncation'; $key = str_repeat("\x0c",20); $hmac = new Crypt_HMAC2($key, 'SHA1'); $this->assertEquals('4c1a03424b55e07fe7f27be1d58bb9324a9a5a04', $hmac->hash($data), true); } public function testHmac_InternalSHA1_6() { $data = 'Test Using Larger Than Block-Size Key - Hash Key First'; $key = str_repeat("\xaa",80); $hmac = new Crypt_HMAC2($key, 'SHA1'); $this->assertEquals('aa4ae5e15272d00e95705637ce8a3b55ed402112', $hmac->hash($data), true); } public function testHmac_InternalSHA1_7() { $data = 'Test Using Larger Than Block-Size Key and Larger Than One Block-Size Data'; $key = str_repeat("\xaa",80); $hmac = new Crypt_HMAC2($key, 'SHA1'); $this->assertEquals('e8e99d0f45237d786d6bbaa7965c7808bbff1a91', $hmac->hash($data), true); } } http://cvs.php.net/viewvc.cgi/pear/Crypt_HMAC2/tests/TestConfiguration.php.dist?view=markup&rev=1.1 Index: pear/Crypt_HMAC2/tests/TestConfiguration.php.dist +++ pear/Crypt_HMAC2/tests/TestConfiguration.php.dist <?php define('TESTS_BCMATH_ENABLED', true); define('TESTS_GMP_ENABLED', true); define('TESTS_BIGINT_ENABLED', true); define('TESTS_GENERATE_REPORT', false); define('TESTS_GENERATE_REPORT_TARGET', '/path/to/target'); http://cvs.php.net/viewvc.cgi/pear/Crypt_HMAC2/tests/TestHelper.php?view=markup&rev=1.1 Index: pear/Crypt_HMAC2/tests/TestHelper.php +++ pear/Crypt_HMAC2/tests/TestHelper.php <?php error_reporting( E_ALL | E_STRICT ); date_default_timezone_set('Europe/London'); $prRoot = dirname(dirname(__FILE__)); $prTests = $prRoot . DIRECTORY_SEPARATOR . 'tests'; set_include_path($prRoot . PATH_SEPARATOR . $prTests . PATH_SEPARATOR . get_include_path()); if (is_readable($prTests . DIRECTORY_SEPARATOR . 'TestConfiguration.php')) { require_once 'TestConfiguration.php'; } else { require_once 'TestConfiguration.php.dist'; }