protected function verifySignature($msg, $signature, $key, $method = 'HS256') 
  {
    if (empty(self::$methods[$method])) {
      throw new \DomainException('Algorithm not supported');
    }
    list($function, $algo) = self::$methods[$method];
    switch($function) {
      case 'openssl':
        $success = openssl_verify($msg, $signature, $key, $algo);
        if(!$success) {
          throw new \DomainException("OpenSSL unable to verify data: " . openssl_error_string());
        }
        return $signature;
        
      case 'hash_hmac':
      default:
        return $signature === hash_hmac($algo, $msg, $key, true);
    }
  }