1
0
mirror of https://github.com/danog/tgseclib.git synced 2024-12-11 08:39:43 +01:00
tgseclib/phpseclib/Crypt/Hash.php

200 lines
4.8 KiB
PHP
Raw Normal View History

<?php
/**
* Pure-PHP implementations of keyed-hash message authentication codes (HMACs) and various cryptographic hashing functions.
*
* Basically a wrapper for hash(). Currently supports the following:
*
* md2, md5, md5-96, sha1, sha1-96, sha256, sha256-96, sha384, and sha512, sha512-96
*
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
* the hash. If no valid algorithm is provided, sha1 will be used.
*
2015-04-02 12:57:52 +02:00
* PHP version 5
*
* 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';
*
2014-12-17 01:16:54 +01:00
* $hash = new \phpseclib\Crypt\Hash('sha1');
*
* $hash->setKey('abcdefg');
*
* echo base64_encode($hash->hash('abcdefg'));
* ?>
* </code>
*
* @category Crypt
2014-12-17 01:16:54 +01:00
* @package Hash
* @author Jim Wigginton <terrafrost@php.net>
* @copyright 2007 Jim Wigginton
* @license http://www.opensource.org/licenses/mit-license.html MIT License
* @link http://phpseclib.sourceforge.net
*/
2014-12-17 01:16:54 +01:00
namespace phpseclib\Crypt;
use phpseclib\Exception\UnsupportedAlgorithmException;
2014-06-02 20:09:47 +02:00
/**
* Pure-PHP implementations of keyed-hash message authentication codes (HMACs) and various cryptographic hashing functions.
*
2014-12-17 01:16:54 +01:00
* @package Hash
* @author Jim Wigginton <terrafrost@php.net>
* @access public
*/
2014-12-17 01:16:54 +01:00
class Hash
{
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;
/**
* Byte-length of hash output (Internal HMAC)
*
2014-12-17 01:16:54 +01:00
* @see \phpseclib\Crypt\Hash::setHash()
2015-09-02 01:44:55 +02:00
* @var int
* @access private
*/
var $l = false;
/**
* 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
* @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
* @access private
*/
var $key = false;
/**
* 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
* @access public
*/
function __construct($hash = 'sha256')
{
$this->setHash($hash);
}
/**
* Sets the key for HMACs
*
* Keys can be of any length.
*
* @access public
2015-09-02 01:44:55 +02:00
* @param string $key
*/
function setKey($key = false)
{
$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;
}
/**
* Sets the hash function.
*
* @access public
2015-09-02 01:44:55 +02:00
* @param string $hash
*/
function setHash($hash)
{
2013-10-18 20:24:06 +02:00
$this->hashParam = $hash = strtolower($hash);
switch ($hash) {
case 'md5-96':
case 'sha1-96':
case 'sha256-96':
case 'sha512-96':
$hash = substr($hash, 0, -3);
$this->l = 12; // 96 / 8 = 12
break;
case 'md2':
case 'md5':
$this->l = 16;
break;
case 'sha1':
$this->l = 20;
break;
case 'sha256':
$this->l = 32;
break;
case 'sha384':
$this->l = 48;
break;
case 'sha512':
$this->l = 64;
break;
default:
// 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;
}
// 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;
}
throw new UnsupportedAlgorithmException("$hash is not a supported algorithm");
}
$this->hash = $hash;
}
/**
* Compute the HMAC.
*
* @access public
2015-09-02 01:44:55 +02:00
* @param string $text
* @return string
*/
function hash($text)
{
$output = !empty($this->key) || is_string($this->key) ?
hash_hmac($this->hash, $text, $this->key, true) :
hash($this->hash, $text, true);
return strlen($output) > $this->l ? substr($output, 0, $this->l) : $output;
}
/**
* Returns the hash length (in bytes)
*
* @access public
2015-09-02 01:44:55 +02:00
* @return int
*/
function getLength()
{
return $this->l;
}
}