3 * A Hash-Base MAC generator
7 * @category PHPCryptLib
9 * @subpackage Implementation
10 * @author Anthony Ferrara <ircmaxell@ircmaxell.com>
11 * @copyright 2011 The Authors
12 * @license http://www.opensource.org/licenses/mit-license.html MIT License
13 * @version Build @@version@@
15 namespace CryptLib\MAC\Implementation
;
17 use \CryptLib\Hash\Hash
;
20 * A Hash-Base MAC generator
22 * @category PHPCryptLib
24 * @subpackage Implementation
26 class HMAC
extends \CryptLib\MAC\AbstractMAC
{
29 * @var array The stored options for this instance
31 protected $options = array(
36 * Generate the MAC using the supplied data
38 * @param string $data The data to use to generate the MAC with
39 * @param string $key The key to generate the MAC
40 * @param int $size The size of the output to return
42 * @return string The generated MAC of the appropriate size
44 public function generate($data, $key, $size = 0) {
45 $hash = $this->options
['hash'];
46 $outputSize = Hash
::getHashSize($hash);
50 if ($size > $outputSize) {
51 throw new \
OutOfRangeException(
53 'The size is too big for the hash primitive [%d:%d]',
59 $return = hash_hmac($hash, $data, $key, true);
60 return substr($return, 0, $size);