key = $key; $this->comparator = $comparator; if (!$data) { throw new \InvalidArgumentException('$data cannot be empty'); } if (!is_string($data)) { $data = serialize($data); } $this->data = $data; $this->algo = $algo; } /** * Get the HMAC token in Base64URL encoding * * @return string */ public function getToken() { $bytes = hash_hmac($this->algo, $this->data, $this->key, true); return strtr(rtrim(base64_encode($bytes), '='), '+/', '-_'); } /** * Does the MAC match the given token? * * @param string $token HMAC token in Base64URL encoding * @return bool */ public function matchesToken($token) { $expected_token = $this->getToken(); return call_user_func($this->comparator, $expected_token, $token); } }