Elgg  Version 1.11
Hmac.php
Go to the documentation of this file.
1 <?php
2 namespace Elgg\Security;
3 
7 class Hmac {
8 
12  private $key;
13 
17  private $comparator;
18 
22  private $data;
23 
27  private $algo;
28 
37  public function __construct($key, callable $comparator, $data, $algo = 'sha256') {
38  $this->key = $key;
39  $this->comparator = $comparator;
40  if (!$data) {
41  throw new \InvalidArgumentException('$data cannot be empty');
42  }
43  if (!is_string($data)) {
44  $data = serialize($data);
45  }
46  $this->data = $data;
47  $this->algo = $algo;
48  }
49 
55  public function getToken() {
56  $bytes = hash_hmac($this->algo, $this->data, $this->key, true);
57  return strtr(rtrim(base64_encode($bytes), '='), '+/', '-_');
58  }
59 
66  public function matchesToken($token) {
67  $expected_token = $this->getToken();
68  return call_user_func($this->comparator, $expected_token, $token);
69  }
70 }
matchesToken($token)
Does the MAC match the given token?
Definition: Hmac.php:66
getToken()
Get the HMAC token in Base64URL encoding.
Definition: Hmac.php:55
Component for creating HMAC tokens.
Definition: Hmac.php:7
$token
__construct($key, callable $comparator, $data, $algo= 'sha256')
Constructor.
Definition: Hmac.php:37