Hash: Update incorrect documentation, use max line length 80.

This commit is contained in:
Andreas Fischer 2015-09-07 19:52:17 +02:00
parent 189b709afb
commit 7d3a1db469

View File

@ -1,23 +1,20 @@
<?php <?php
/** /**
* Pure-PHP implementations of keyed-hash message authentication codes (HMACs) and various cryptographic hashing functions. * 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.
* *
* Basically a wrapper for hash(). Currently supports the following: * If {@link \phpseclib\Crypt\Hash::setKey() setKey()} is called,
* * {@link \phpseclib\Crypt\Hash::hash() hash()} will return the HMAC as opposed
* md2, md5, md5-96, sha1, sha1-96, sha256, sha256-96, sha384, and sha512, sha512-96 * to the hash.
*
* 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.
*
* PHP version 5
* *
* Here's a short example of how to use this library: * Here's a short example of how to use this library:
* <code> * <code>
* <?php * <?php
* include 'vendor/autoload.php'; * include 'vendor/autoload.php';
* *
* $hash = new \phpseclib\Crypt\Hash('sha1'); * $hash = new \phpseclib\Crypt\Hash('sha512');
* *
* $hash->setKey('abcdefg'); * $hash->setKey('abcdefg');
* *
@ -28,7 +25,9 @@
* @category Crypt * @category Crypt
* @package Hash * @package Hash
* @author Jim Wigginton <terrafrost@php.net> * @author Jim Wigginton <terrafrost@php.net>
* @copyright 2007 Jim Wigginton * @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 * @license http://www.opensource.org/licenses/mit-license.html MIT License
* @link http://phpseclib.sourceforge.net * @link http://phpseclib.sourceforge.net
*/ */
@ -38,10 +37,9 @@ namespace phpseclib\Crypt;
use phpseclib\Exception\UnsupportedAlgorithmException; use phpseclib\Exception\UnsupportedAlgorithmException;
/** /**
* Pure-PHP implementations of keyed-hash message authentication codes (HMACs) and various cryptographic hashing functions.
*
* @package Hash * @package Hash
* @author Jim Wigginton <terrafrost@php.net> * @author Jim Wigginton <terrafrost@php.net>
* @author Andreas Fischer <bantu@phpbb.com>
* @access public * @access public
*/ */
class Hash class Hash
@ -153,17 +151,25 @@ class Hash
$this->length = 64; $this->length = 64;
break; break;
default: default:
// see if the hash isn't "officially" supported see if it can be "unofficially" supported and calculate the length accordingly // 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())) { if (in_array($hash, hash_algos())) {
$this->length = strlen(hash($hash, '', true)); $this->length = strlen(hash($hash, '', true));
break; break;
} }
// if the hash algorithm doens't exist maybe it's a truncated hash. eg. md5-96 or some such // if the hash algorithm doens't exist maybe it's a truncated
if (preg_match('#(-\d+)$#', $hash, $matches) && in_array($hash = substr($hash, 0, -strlen($matches[1])), hash_algos())) { // hash, e.g. whirlpool-12 or some such.
$this->length = abs($matches[1]) >> 3; if (preg_match('#(-\d+)$#', $hash, $matches)) {
break; $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"); throw new UnsupportedAlgorithmException(
"$hash is not a supported algorithm"
);
} }
$this->hash = $hash; $this->hash = $hash;
@ -182,7 +188,9 @@ class Hash
hash_hmac($this->hash, $text, $this->key, true) : hash_hmac($this->hash, $text, $this->key, true) :
hash($this->hash, $text, true); hash($this->hash, $text, true);
return strlen($output) > $this->length ? substr($output, 0, $this->length) : $output; return strlen($output) > $this->length
? substr($output, 0, $this->length)
: $output;
} }
/** /**