2007-07-23 07:21:39 +02:00
|
|
|
<?php
|
2007-07-25 23:56:14 +02:00
|
|
|
|
|
|
|
/**
|
2009-02-16 23:22:13 +01:00
|
|
|
* Pure-PHP implementations of keyed-hash message authentication codes (HMACs) and various cryptographic hashing functions.
|
2007-07-25 23:56:14 +02:00
|
|
|
*
|
2015-09-06 09:59:12 +02:00
|
|
|
* Basically a wrapper for hash(). Currently supports the following:
|
2009-11-23 20:06:07 +01:00
|
|
|
*
|
2014-08-06 06:27:11 +02:00
|
|
|
* md2, md5, md5-96, sha1, sha1-96, sha256, sha256-96, sha384, and sha512, sha512-96
|
2009-11-23 20:06:07 +01:00
|
|
|
*
|
2014-12-17 01:16:54 +01:00
|
|
|
* If {@link \phpseclib\Crypt\Hash::setKey() setKey()} is called, {@link \phpseclib\Crypt\Hash::hash() hash()} will return the HMAC as opposed to
|
2009-11-23 20:06:07 +01:00
|
|
|
* the hash. If no valid algorithm is provided, sha1 will be used.
|
2007-07-25 23:56:14 +02:00
|
|
|
*
|
2015-04-02 12:57:52 +02:00
|
|
|
* PHP version 5
|
2007-07-25 23:56:14 +02:00
|
|
|
*
|
2009-02-16 23:22:13 +01:00
|
|
|
* Here's a short example of how to use this library:
|
|
|
|
* <code>
|
|
|
|
* <?php
|
2014-12-17 01:16:54 +01:00
|
|
|
* include 'vendor/autoload.php';
|
2009-02-16 23:22:13 +01:00
|
|
|
*
|
2014-12-17 01:16:54 +01:00
|
|
|
* $hash = new \phpseclib\Crypt\Hash('sha1');
|
2009-02-16 23:22:13 +01:00
|
|
|
*
|
|
|
|
* $hash->setKey('abcdefg');
|
|
|
|
*
|
|
|
|
* echo base64_encode($hash->hash('abcdefg'));
|
|
|
|
* ?>
|
|
|
|
* </code>
|
|
|
|
*
|
2013-12-10 20:10:37 +01:00
|
|
|
* @category Crypt
|
2014-12-17 01:16:54 +01:00
|
|
|
* @package Hash
|
2013-12-10 20:10:37 +01:00
|
|
|
* @author Jim Wigginton <terrafrost@php.net>
|
2014-12-10 00:02:44 +01:00
|
|
|
* @copyright 2007 Jim Wigginton
|
2013-12-10 20:10:37 +01:00
|
|
|
* @license http://www.opensource.org/licenses/mit-license.html MIT License
|
|
|
|
* @link http://phpseclib.sourceforge.net
|
2007-07-25 23:56:14 +02:00
|
|
|
*/
|
2007-07-23 07:21:39 +02:00
|
|
|
|
2014-12-17 01:16:54 +01:00
|
|
|
namespace phpseclib\Crypt;
|
|
|
|
|
2015-09-06 09:59:12 +02:00
|
|
|
use phpseclib\Exception\UnsupportedAlgorithmException;
|
2014-06-02 20:09:47 +02:00
|
|
|
|
2007-07-23 07:21:39 +02:00
|
|
|
/**
|
2009-02-16 23:22:13 +01:00
|
|
|
* Pure-PHP implementations of keyed-hash message authentication codes (HMACs) and various cryptographic hashing functions.
|
2007-07-23 07:21:39 +02:00
|
|
|
*
|
2014-12-17 01:16:54 +01:00
|
|
|
* @package Hash
|
2007-07-23 07:21:39 +02:00
|
|
|
* @author Jim Wigginton <terrafrost@php.net>
|
|
|
|
* @access public
|
|
|
|
*/
|
2014-12-17 01:16:54 +01:00
|
|
|
class Hash
|
2013-12-03 19:34:41 +01:00
|
|
|
{
|
2013-10-18 20:24:06 +02:00
|
|
|
/**
|
|
|
|
* Hash Parameter
|
|
|
|
*
|
2014-12-17 01:16:54 +01:00
|
|
|
* @see \phpseclib\Crypt\Hash::setHash()
|
2015-09-02 01:44:55 +02:00
|
|
|
* @var int
|
2013-10-18 20:24:06 +02:00
|
|
|
* @access private
|
|
|
|
*/
|
|
|
|
var $hashParam;
|
|
|
|
|
2007-07-23 07:21:39 +02:00
|
|
|
/**
|
2009-02-16 23:22:13 +01:00
|
|
|
* Byte-length of hash output (Internal HMAC)
|
2007-07-23 07:21:39 +02:00
|
|
|
*
|
2014-12-17 01:16:54 +01:00
|
|
|
* @see \phpseclib\Crypt\Hash::setHash()
|
2015-09-02 01:44:55 +02:00
|
|
|
* @var int
|
2007-07-23 07:21:39 +02:00
|
|
|
* @access private
|
|
|
|
*/
|
2009-11-23 20:06:07 +01:00
|
|
|
var $l = false;
|
2007-07-23 07:21:39 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Hash Algorithm
|
|
|
|
*
|
2014-12-17 01:16:54 +01:00
|
|
|
* @see \phpseclib\Crypt\Hash::setHash()
|
2015-09-02 01:44:55 +02:00
|
|
|
* @var string
|
2007-07-23 07:21:39 +02:00
|
|
|
* @access private
|
|
|
|
*/
|
|
|
|
var $hash;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Key
|
|
|
|
*
|
2014-12-17 01:16:54 +01:00
|
|
|
* @see \phpseclib\Crypt\Hash::setKey()
|
2015-09-02 01:44:55 +02:00
|
|
|
* @var string
|
2007-07-23 07:21:39 +02:00
|
|
|
* @access private
|
|
|
|
*/
|
2012-08-23 15:59:49 +02:00
|
|
|
var $key = false;
|
2007-07-23 07:21:39 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Default Constructor.
|
|
|
|
*
|
2015-09-02 01:44:55 +02:00
|
|
|
* @param string $hash
|
2014-12-17 01:16:54 +01:00
|
|
|
* @return \phpseclib\Crypt\Hash
|
2007-07-23 07:21:39 +02:00
|
|
|
* @access public
|
|
|
|
*/
|
2015-09-06 09:59:12 +02:00
|
|
|
function __construct($hash = 'sha256')
|
2007-07-23 07:21:39 +02:00
|
|
|
{
|
2009-02-16 23:22:13 +01:00
|
|
|
$this->setHash($hash);
|
2007-07-23 07:21:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2009-02-16 23:22:13 +01:00
|
|
|
* Sets the key for HMACs
|
|
|
|
*
|
|
|
|
* Keys can be of any length.
|
2007-07-23 07:21:39 +02:00
|
|
|
*
|
|
|
|
* @access public
|
2015-09-02 01:44:55 +02:00
|
|
|
* @param string $key
|
2007-07-23 07:21:39 +02:00
|
|
|
*/
|
2012-08-23 15:59:49 +02:00
|
|
|
function setKey($key = false)
|
2007-07-23 07:21:39 +02:00
|
|
|
{
|
|
|
|
$this->key = $key;
|
|
|
|
}
|
|
|
|
|
2013-10-18 20:24:06 +02:00
|
|
|
/**
|
|
|
|
* Gets the hash function.
|
|
|
|
*
|
|
|
|
* As set by the constructor or by the setHash() method.
|
|
|
|
*
|
|
|
|
* @access public
|
2015-09-02 01:44:55 +02:00
|
|
|
* @return string
|
2013-10-18 20:24:06 +02:00
|
|
|
*/
|
|
|
|
function getHash()
|
|
|
|
{
|
|
|
|
return $this->hashParam;
|
|
|
|
}
|
|
|
|
|
2007-07-23 07:21:39 +02:00
|
|
|
/**
|
|
|
|
* Sets the hash function.
|
|
|
|
*
|
|
|
|
* @access public
|
2015-09-02 01:44:55 +02:00
|
|
|
* @param string $hash
|
2007-07-23 07:21:39 +02:00
|
|
|
*/
|
|
|
|
function setHash($hash)
|
|
|
|
{
|
2013-10-18 20:24:06 +02:00
|
|
|
$this->hashParam = $hash = strtolower($hash);
|
2007-09-23 06:41:39 +02:00
|
|
|
switch ($hash) {
|
|
|
|
case 'md5-96':
|
|
|
|
case 'sha1-96':
|
2014-08-06 06:27:11 +02:00
|
|
|
case 'sha256-96':
|
|
|
|
case 'sha512-96':
|
|
|
|
$hash = substr($hash, 0, -3);
|
2009-02-16 23:22:13 +01:00
|
|
|
$this->l = 12; // 96 / 8 = 12
|
|
|
|
break;
|
2009-11-23 20:06:07 +01:00
|
|
|
case 'md2':
|
2009-02-16 23:22:13 +01:00
|
|
|
case 'md5':
|
|
|
|
$this->l = 16;
|
|
|
|
break;
|
|
|
|
case 'sha1':
|
|
|
|
$this->l = 20;
|
2009-11-23 20:06:07 +01:00
|
|
|
break;
|
|
|
|
case 'sha256':
|
|
|
|
$this->l = 32;
|
|
|
|
break;
|
|
|
|
case 'sha384':
|
|
|
|
$this->l = 48;
|
|
|
|
break;
|
|
|
|
case 'sha512':
|
|
|
|
$this->l = 64;
|
|
|
|
break;
|
|
|
|
default:
|
2015-09-06 09:59:12 +02:00
|
|
|
// see if the hash isn't "officially" supported see if it can be "unofficially" supported and calculate the length accordingly
|
|
|
|
if (in_array($hash, hash_algos())) {
|
|
|
|
$this->l = strlen(hash($hash, '', true));
|
|
|
|
break;
|
2007-07-23 07:21:39 +02:00
|
|
|
}
|
2015-09-06 09:59:12 +02:00
|
|
|
// if the hash algorithm doens't exist maybe it's a truncated hash. eg. md5-96 or some such
|
|
|
|
if (preg_match('#(-\d+)$#', $hash, $matches) && in_array($hash = substr($hash, 0, -strlen($matches[1])), hash_algos())) {
|
|
|
|
$this->l = abs($matches[1]) >> 3;
|
|
|
|
break;
|
2007-07-23 07:21:39 +02:00
|
|
|
}
|
2015-09-06 09:59:12 +02:00
|
|
|
throw new UnsupportedAlgorithmException("$hash is not a supported algorithm");
|
2007-07-23 07:21:39 +02:00
|
|
|
}
|
|
|
|
|
2015-09-06 09:59:12 +02:00
|
|
|
$this->hash = $hash;
|
2007-07-23 07:21:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Compute the HMAC.
|
|
|
|
*
|
|
|
|
* @access public
|
2015-09-02 01:44:55 +02:00
|
|
|
* @param string $text
|
|
|
|
* @return string
|
2007-07-23 07:21:39 +02:00
|
|
|
*/
|
2009-02-16 23:22:13 +01:00
|
|
|
function hash($text)
|
2007-07-23 07:21:39 +02:00
|
|
|
{
|
2015-09-06 09:59:12 +02:00
|
|
|
$output = !empty($this->key) || is_string($this->key) ?
|
|
|
|
hash_hmac($this->hash, $text, $this->key, true) :
|
|
|
|
hash($this->hash, $text, true);
|
2007-07-23 07:21:39 +02:00
|
|
|
|
2015-09-06 09:59:12 +02:00
|
|
|
return strlen($output) > $this->l ? substr($output, 0, $this->l) : $output;
|
2007-07-23 07:21:39 +02:00
|
|
|
}
|
2009-11-23 20:06:07 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the hash length (in bytes)
|
|
|
|
*
|
2010-08-08 23:29:39 +02:00
|
|
|
* @access public
|
2015-09-02 01:44:55 +02:00
|
|
|
* @return int
|
2009-11-23 20:06:07 +01:00
|
|
|
*/
|
|
|
|
function getLength()
|
|
|
|
{
|
|
|
|
return $this->l;
|
|
|
|
}
|
2012-06-11 10:34:07 +02:00
|
|
|
}
|