cvs: pear /Crypt_HMAC HMAC.php package.xml /Crypt_HMAC/tests test.php phpweb/distributions php-4.3.1-installer.exe
| From: | Derick Rethans | Date: | Sun, 16 Feb 2003 20:34:16 +0000 |
| Subject: | cvs: pear /Crypt_HMAC HMAC.php package.xml /Crypt_HMAC/tests test.php phpweb/distributions php-4.3.1-installer.exe | ||
| Groups: | php.pear.cvs | ||
| Request: | Send a blank email to pear-cvs+get-8897@lists.php.net to get a copy of this message | ||
derick Sun Feb 16 15:34:16 2003 EDT
Added files:
/phpweb/distributions php-4.3.1-installer.exe
/pear/Crypt_HMAC HMAC.php package.xml
/pear/Crypt_HMAC/tests test.php
Log:
- Added the Crypt_HMAC package which calculates RFC 2104 compliant HMACs.
Index: pear/Crypt_HMAC/HMAC.php
+++ pear/Crypt_HMAC/HMAC.php
<?php
/* vim: set expandtab tabstop=4 shiftwidth=4: */
// +----------------------------------------------------------------------+
// | PHP Version 4 |
// +----------------------------------------------------------------------+
// | Copyright (c) 1997-2003 The PHP Group |
// +----------------------------------------------------------------------+
// | This source file is subject to version 2.0 of the PHP license, |
// | that is bundled with this package in the file LICENSE, and is |
// | available at through the world-wide-web at |
// | http://www.php.net/license/2_02.txt.
|
// | If you did not receive a copy of the PHP license and are unable to |
// | obtain it through the world-wide-web, please send a note to |
// | license@php.net so we can mail you a copy immediately. |
// +----------------------------------------------------------------------+
// | Authors: Derick Rethans <d.rethans@jdimedia.nl> |
// +----------------------------------------------------------------------+
//
// $Id: HMAC.php,v 1.1 2003/02/16 20:34:16 derick Exp $
/**
* Calculates RFC 2104 compliant HMACs
*
* @version $Revision: 1.1 $
* @access public
* @package Crypt
* @author Derick Rethans <d.rethans@jdimedia.nl>
*/
class Crypt_HMAC {
/**
* Hash function to use
* @var string
*/
var $_func;
/**
* Inner padded key
* @var string
*/
var $_ipad;
/**
* Outer padded key
* @var string
*/
var $_opad;
/**
* Constructor
* Pass method as first parameter
*
* @param string method - Hash function used for the calculation
* @return void
* @access public
*/
function Crypt_HMAC($key, $method = 'md5')
{
if (!in_array($method, array('sha1', 'md5'))) {
die("Unsupported hash function '$method'.");
}
$this->_func = $method;
/* Pad the key as the RFC wishes */
if (strlen($key) > 64) {
$key = pack('H32', $method($key));
}
if (strlen($key) < 64) {
$key = str_pad($key, 64, chr(0));
}
/* Calculate the padded keys and save them */
$this->_ipad = (substr($key, 0, 64) ^ str_repeat(chr(0x36), 64));
$this->_opad = (substr($key, 0, 64) ^ str_repeat(chr(0x5C), 64));
}
/**
* Hashing function
*
* @param string data - string that will encrypted
* @return string
* @access public
*/
function hash($data)
{
$func = $this->_func;
$inner = pack('H32', $func($this->_ipad . $data));
$digest = $func($this->_opad . $inner);
return $digest;
}
}
?>
Index: pear/Crypt_HMAC/package.xml
+++ pear/Crypt_HMAC/package.xml
<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE package SYSTEM "../package.dtd">
<!-- do not use the "Type" attribute here, that one is only for
generated package.xml files -->
<package version="1.0">
<name>Crypt_HMAC</name>
<summary>
A class to calculate RFC 2104 compliant hashes.
</summary>
<description>
A class to calculate RFC 2104 compliant hashes.
</description>
<maintainers>
<maintainer>
<user>derick</user>
<name>Derick Rethans</name>
<email>d.rethans@jdimedia.nl</email>
</maintainer>
</maintainers>
<release>
<version>0.9</version>
<state>beta</state>
<license>BSD</license>
<date>2003-02-16</date>
<notes>
Initial release
</notes>
<filelist>
<dir name="/" baseinstalldir="Crypt">
<dir name="tests">
<file role="test">test.php</file>
</dir>
<file role="php">HMAC.php</file>
</dir>
</filelist>
</release>
</package>
Index: pear/Crypt_HMAC/tests/test.php
+++ pear/Crypt_HMAC/tests/test.php
<?php
/* This test file implements the three test vectors as described in
* RFC 2104 (http://rfc.net/rfc2104.html) */
require_once 'Crypt/HMAC.php';
$key = str_repeat(chr(0x0b), 16);
$crypt1 = new Crypt_HMAC($key, 'md5');
echo $crypt1->hash('Hi There')."\n";
$key = 'Jefe';
$crypt2 = new Crypt_HMAC($key, 'md5');
echo $crypt2->hash('what do ya want for nothing?')."\n";
$key = str_repeat(chr(0xaa), 16);
$data = str_repeat(chr(0xdd), 50);
$crypt3 = new Crypt_HMAC($key, 'md5');
echo $crypt3->hash($data)."\n";
?>