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

207 lines
4.9 KiB
PHP
Raw Normal View History

<?php
/**
* Wrapper around hash() and hash_hmac() functions supporting truncated hashes
* such as sha256-96. Any hash algorithm returned by hash_algos() (and
* truncated versions thereof) are supported.
*
* If {@link \phpseclib\Crypt\Hash::setKey() setKey()} is called,
* {@link \phpseclib\Crypt\Hash::hash() hash()} will return the HMAC as opposed
* to the hash.
*
* 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';
*
* $hash = new \phpseclib\Crypt\Hash('sha512');
*
* $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 2015 Jim Wigginton
* @author Andreas Fischer <bantu@phpbb.com>
* @copyright 2015 Andreas Fischer
* @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
/**
2014-12-17 01:16:54 +01:00
* @package Hash
* @author Jim Wigginton <terrafrost@php.net>
* @author Andreas Fischer <bantu@phpbb.com>
* @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 $length;
/**
* 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
* @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);
2015-09-07 19:36:03 +02:00
$this->length = 12; // 96 / 8 = 12
break;
case 'md2':
case 'md5':
2015-09-07 19:36:03 +02:00
$this->length = 16;
break;
case 'sha1':
2015-09-07 19:36:03 +02:00
$this->length = 20;
break;
case 'sha256':
2015-09-07 19:36:03 +02:00
$this->length = 32;
break;
case 'sha384':
2015-09-07 19:36:03 +02:00
$this->length = 48;
break;
case 'sha512':
2015-09-07 19:36:03 +02:00
$this->length = 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())) {
2015-09-07 19:36:03 +02:00
$this->length = strlen(hash($hash, '', true));
break;
}
// if the hash algorithm doens't exist maybe it's a truncated
// hash, e.g. whirlpool-12 or some such.
if (preg_match('#(-\d+)$#', $hash, $matches)) {
$hash = substr($hash, 0, -strlen($matches[1]));
if (in_array($hash, hash_algos())) {
$this->length = 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->length
? substr($output, 0, $this->length)
: $output;
}
/**
* Returns the hash length (in bytes)
*
* @access public
2015-09-02 01:44:55 +02:00
* @return int
*/
function getLength()
{
2015-09-07 19:36:03 +02:00
return $this->length;
}
}